├── .gitignore ├── README.md ├── build.gradle.kts ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src ├── anonymous_lambda ├── README.md └── java │ └── codes │ └── som │ └── oof4j │ └── anonymouslambda │ ├── AnonymousLambda.java │ └── CustomStringSupplier.java ├── enum_constants ├── README.md └── java │ └── codes │ └── som │ └── oof4j │ └── enumconstants │ ├── EnumConstants.java │ └── MyEnum.java ├── fizzbuzz_basic ├── README.md └── java │ └── codes │ └── som │ └── oof4j │ └── fizzbuzz │ └── basic │ └── BasicFizzBuzz.java ├── fizzbuzz_enterprise ├── README.md └── java │ └── codes │ └── som │ └── oof4j │ └── fizzbuzz │ └── enterprise │ ├── EnterpriseFizzBuzzCommandLineEntryPoint.java │ ├── EnterpriseFizzBuzzCommandLineResultSubscriber.java │ ├── EnterpriseFizzBuzzExecutionEnvironment.java │ ├── computation │ ├── EnterpriseFizzBuzzComputationFactory.java │ ├── EnterpriseFizzBuzzComputationTask.java │ └── unit │ │ ├── FizzBuzzComputationUnit.java │ │ ├── FizzBuzzComputationUnitLookupTableEntry.java │ │ ├── FizzBuzzRuleApplicator.java │ │ └── Flags.java │ └── results │ ├── FizzBuzzResult.java │ ├── FizzBuzzResultPublisher.java │ ├── FizzBuzzResultSubscriber.java │ └── internal │ ├── AbstractFizzBuzzResultPublisherImpl.java │ └── FizzBuzzResultImpl.java ├── hello_world ├── README.md └── java │ └── codes │ └── som │ └── oof4j │ └── helloworld │ └── HelloWorld.java └── interface_overlap ├── README.md └── java └── codes └── som └── oof4j └── interfaceoverlap ├── InterfaceA.java ├── InterfaceB.java └── InterfaceOverlap.java /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | /.vscode 3 | 4 | /.gradle 5 | /build 6 | /out 7 | 8 | *.iml 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # oof 2 | 3 | The 'Obfuscation of Fickleties' dataset contains some self-contained Java applications which demonstrate an edge case to consider when obfuscating. 4 | 5 | This is the dataset used to test [Paramorphism](https://paramorphism.serenity.enterprises/) - An obfuscator for Java and Kotlin supporting the JVM from versions Java SE 8 and up. 6 | 7 | ## Build 8 | 9 | With `./gradlew `, each sample will produce its own JAR file in the `build/libs` directory. 10 | 11 | e.g: 12 | ```sh 13 | $ ./gradlew fizzbuzz_basic 14 | $ # use build/libs/fizzbuzz_basic.jar 15 | ``` 16 | -------------------------------------------------------------------------------- /build.gradle.kts: -------------------------------------------------------------------------------- 1 | import org.jetbrains.kotlin.gradle.tasks.KotlinCompile 2 | 3 | plugins { 4 | java 5 | kotlin("jvm") version "1.3.21" 6 | } 7 | 8 | group = "codes.som" 9 | version = "1.0.0" 10 | 11 | repositories { 12 | mavenCentral() 13 | } 14 | 15 | for ((sourceSetId, main) in mapOf( 16 | "fizzbuzz_basic" to "codes.som.oof4j.fizzbuzz.basic.BasicFizzBuzz", 17 | "hello_world" to "codes.som.oof4j.helloworld.HelloWorld", 18 | "anonymous_lambda" to "codes.som.oof4j.anonymouslambda.AnonymousLambda", 19 | "fizzbuzz_enterprise" to "codes.som.oof4j.fizzbuzz.enterprise.EnterpriseFizzBuzzCommandLineEntryPoint", 20 | "enum_constants" to "codes.som.oof4j.enumconstants.EnumConstants", 21 | "interface_overlap" to "codes.som.oof4j.interfaceoverlap.InterfaceOverlap" 22 | )) { 23 | project.sourceSets.create(sourceSetId) { 24 | val sourceSet = this 25 | tasks.register(sourceSetId) { 26 | archiveName = "$sourceSetId.jar" 27 | manifest { 28 | attributes(mapOf( 29 | "Main-Class" to main 30 | )) 31 | } 32 | 33 | from(sourceSet.output) 34 | } 35 | } 36 | } 37 | 38 | dependencies { 39 | implementation("org.codehaus.groovy:groovy-all:2.3.11") 40 | implementation(kotlin("stdlib-jdk8")) 41 | 42 | testCompile("junit", "junit", "4.12") 43 | } 44 | 45 | configure { 46 | sourceCompatibility = JavaVersion.VERSION_1_8 47 | } 48 | 49 | tasks.withType { 50 | kotlinOptions.jvmTarget = "1.8" 51 | } 52 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/char/oof-jvm/5ca968c0f5df7ed9acacef488b8dea1889d612d5/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'oof-jvm' 2 | 3 | -------------------------------------------------------------------------------- /src/anonymous_lambda/README.md: -------------------------------------------------------------------------------- 1 | # Anonymous Lambda 2 | 3 | This example demonstrates an annoying edge case for renaming methods. 4 | 5 | Lambda expressions which replace anonymous classes have to be also updated correctly to prevent breakage of the polymorphic link. 6 | -------------------------------------------------------------------------------- /src/anonymous_lambda/java/codes/som/oof4j/anonymouslambda/AnonymousLambda.java: -------------------------------------------------------------------------------- 1 | package codes.som.oof4j.anonymouslambda; 2 | 3 | import java.util.function.*; 4 | 5 | public class AnonymousLambda { 6 | public static void main(String[] args) { 7 | System.out.println("Built-in string supplier:"); 8 | Supplier builtInSupplier = () -> "Hello, world!"; 9 | System.out.println(builtInSupplier.get()); 10 | 11 | System.out.println("\nCustom supplier:"); 12 | CustomStringSupplier customSupplier = () -> "Hello, world!"; 13 | System.out.println(customSupplier.supply()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/anonymous_lambda/java/codes/som/oof4j/anonymouslambda/CustomStringSupplier.java: -------------------------------------------------------------------------------- 1 | package codes.som.oof4j.anonymouslambda; 2 | 3 | public interface CustomStringSupplier { 4 | String supply(); 5 | } 6 | -------------------------------------------------------------------------------- /src/enum_constants/README.md: -------------------------------------------------------------------------------- 1 | # Enum Constants 2 | 3 | This example shows the edge case where the field `$VALUES` and the method `values()` in an enum class are remapped, 4 | causing `getEnumConstants()` in `java/lang/Class` to fail. 5 | -------------------------------------------------------------------------------- /src/enum_constants/java/codes/som/oof4j/enumconstants/EnumConstants.java: -------------------------------------------------------------------------------- 1 | package codes.som.oof4j.enumconstants; 2 | 3 | public class EnumConstants { 4 | public static void main(String[] args) { 5 | for (MyEnum e : MyEnum.class.getEnumConstants()) { 6 | System.out.println(e); 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/enum_constants/java/codes/som/oof4j/enumconstants/MyEnum.java: -------------------------------------------------------------------------------- 1 | package codes.som.oof4j.enumconstants; 2 | 3 | public enum MyEnum { 4 | EVERYTHING, 5 | IS, 6 | WORKING, 7 | FINE 8 | } 9 | -------------------------------------------------------------------------------- /src/fizzbuzz_basic/README.md: -------------------------------------------------------------------------------- 1 | # FizzBuzz Basic 2 | 3 | This is the simplest example in `oof4j` 4 | 5 | It still raises one obfuscation snag - Method inheritance: 6 | The `BasicFizzBuzz` class implements `java.lang.Runnable`, and overrides its `run()V` method. 7 | This means that an obfuscator needs to keep the `run` name intact for the class to work. 8 | -------------------------------------------------------------------------------- /src/fizzbuzz_basic/java/codes/som/oof4j/fizzbuzz/basic/BasicFizzBuzz.java: -------------------------------------------------------------------------------- 1 | package codes.som.oof4j.fizzbuzz.basic; 2 | 3 | public class BasicFizzBuzz implements Runnable { 4 | public static void main(String[] args) { 5 | Runnable application = new BasicFizzBuzz(); 6 | application.run(); 7 | } 8 | 9 | // Since this method overrides java.lang.Runnable's run, 10 | // it should not be remapped. 11 | public void run() { 12 | this.execute(); 13 | } 14 | 15 | // This method should be remapped. 16 | private void execute() { 17 | String[] lookupTable = new String[] { null, "Fizz", "Buzz", "Fizz Buzz" }; 18 | 19 | for (int i = 1; i <= 100; i++) { 20 | byte flags = 0; 21 | if (i % 3 == 0) 22 | flags |= 0x01; 23 | if (i % 5 == 0) 24 | flags |= 0x02; 25 | 26 | if (flags == 0) { 27 | System.out.println(i); 28 | } else { 29 | System.out.println(lookupTable[flags]); 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/fizzbuzz_enterprise/README.md: -------------------------------------------------------------------------------- 1 | # FizzBuzz Enterprise 2 | 3 | contrivance (noun): 4 | > an artistic or ingenious creation 5 | -------------------------------------------------------------------------------- /src/fizzbuzz_enterprise/java/codes/som/oof4j/fizzbuzz/enterprise/EnterpriseFizzBuzzCommandLineEntryPoint.java: -------------------------------------------------------------------------------- 1 | package codes.som.oof4j.fizzbuzz.enterprise; 2 | 3 | import codes.som.oof4j.fizzbuzz.enterprise.computation.EnterpriseFizzBuzzComputationFactory; 4 | import codes.som.oof4j.fizzbuzz.enterprise.results.FizzBuzzResultPublisher; 5 | 6 | public class EnterpriseFizzBuzzCommandLineEntryPoint { 7 | public static void main(String[] args) { 8 | if (args.length == 1) { 9 | try { 10 | int iterations = Integer.parseInt(args[0]); 11 | if (iterations < 1) 12 | throw new NumberFormatException(); 13 | 14 | EnterpriseFizzBuzzExecutionEnvironment.getInstance().setIterations(iterations); 15 | } catch (NumberFormatException ignored) { 16 | System.err.println("Invalid number of iterations specified: " + args[0]); 17 | System.exit(1); 18 | } 19 | } 20 | 21 | Runnable task = EnterpriseFizzBuzzComputationFactory.buildComputationTask(); 22 | ((FizzBuzzResultPublisher) task).registerSubscriber(new EnterpriseFizzBuzzCommandLineResultSubscriber(System.out)); 23 | 24 | task.run(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/fizzbuzz_enterprise/java/codes/som/oof4j/fizzbuzz/enterprise/EnterpriseFizzBuzzCommandLineResultSubscriber.java: -------------------------------------------------------------------------------- 1 | package codes.som.oof4j.fizzbuzz.enterprise; 2 | 3 | import codes.som.oof4j.fizzbuzz.enterprise.results.FizzBuzzResult; 4 | import codes.som.oof4j.fizzbuzz.enterprise.results.FizzBuzzResultSubscriber; 5 | 6 | import java.io.*; 7 | 8 | public class EnterpriseFizzBuzzCommandLineResultSubscriber implements FizzBuzzResultSubscriber { 9 | private final PrintStream outputStream; 10 | 11 | public EnterpriseFizzBuzzCommandLineResultSubscriber(PrintStream outputStream) { 12 | this.outputStream = outputStream; 13 | } 14 | 15 | 16 | @Override 17 | public void onResult(FizzBuzzResult result) { 18 | outputStream.println(result.getAsString()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/fizzbuzz_enterprise/java/codes/som/oof4j/fizzbuzz/enterprise/EnterpriseFizzBuzzExecutionEnvironment.java: -------------------------------------------------------------------------------- 1 | package codes.som.oof4j.fizzbuzz.enterprise; 2 | 3 | public class EnterpriseFizzBuzzExecutionEnvironment { 4 | private static final EnterpriseFizzBuzzExecutionEnvironment INSTANCE = new EnterpriseFizzBuzzExecutionEnvironment(); 5 | public static EnterpriseFizzBuzzExecutionEnvironment getInstance() { 6 | return INSTANCE; 7 | } 8 | 9 | private int iterations = 100; 10 | 11 | public int getIterations() { 12 | return iterations; 13 | } 14 | 15 | public void setIterations(int iterations) { 16 | this.iterations = iterations; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/fizzbuzz_enterprise/java/codes/som/oof4j/fizzbuzz/enterprise/computation/EnterpriseFizzBuzzComputationFactory.java: -------------------------------------------------------------------------------- 1 | package codes.som.oof4j.fizzbuzz.enterprise.computation; 2 | 3 | import codes.som.oof4j.fizzbuzz.enterprise.EnterpriseFizzBuzzExecutionEnvironment; 4 | 5 | public class EnterpriseFizzBuzzComputationFactory { 6 | public static EnterpriseFizzBuzzComputationTask buildComputationTask() { 7 | return new EnterpriseFizzBuzzComputationTask(EnterpriseFizzBuzzExecutionEnvironment.getInstance().getIterations()); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/fizzbuzz_enterprise/java/codes/som/oof4j/fizzbuzz/enterprise/computation/EnterpriseFizzBuzzComputationTask.java: -------------------------------------------------------------------------------- 1 | package codes.som.oof4j.fizzbuzz.enterprise.computation; 2 | 3 | import codes.som.oof4j.fizzbuzz.enterprise.computation.unit.*; 4 | import codes.som.oof4j.fizzbuzz.enterprise.results.internal.*; 5 | 6 | public class EnterpriseFizzBuzzComputationTask extends AbstractFizzBuzzResultPublisherImpl implements Runnable { 7 | private final int iterations; 8 | 9 | public EnterpriseFizzBuzzComputationTask(int iterations) { 10 | this.iterations = iterations; 11 | } 12 | 13 | @Override 14 | public void run() { 15 | this.execute(); 16 | } 17 | 18 | private final void execute() { 19 | for (int iteration = 1; iteration <= iterations; iteration++) { 20 | FizzBuzzComputationUnit unit = new FizzBuzzComputationUnit(iteration); 21 | publishResult(unit.calculate()); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/fizzbuzz_enterprise/java/codes/som/oof4j/fizzbuzz/enterprise/computation/unit/FizzBuzzComputationUnit.java: -------------------------------------------------------------------------------- 1 | package codes.som.oof4j.fizzbuzz.enterprise.computation.unit; 2 | 3 | import codes.som.oof4j.fizzbuzz.enterprise.results.FizzBuzzResult; 4 | import codes.som.oof4j.fizzbuzz.enterprise.results.internal.*; 5 | 6 | public class FizzBuzzComputationUnit { 7 | private final int iteration; 8 | 9 | public FizzBuzzComputationUnit(int iteration) { 10 | this.iteration = iteration; 11 | } 12 | 13 | public final FizzBuzzResult calculate() { 14 | Flags flags = new Flags(); 15 | FizzBuzzRuleApplicator.applyAllRules(iteration, flags); 16 | 17 | String message = FizzBuzzComputationUnitLookupTableEntry.values()[flags.getValue()].toString(); 18 | if (message == null) { 19 | message = String.valueOf(iteration); 20 | } 21 | 22 | return new FizzBuzzResultImpl(iteration, message); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/fizzbuzz_enterprise/java/codes/som/oof4j/fizzbuzz/enterprise/computation/unit/FizzBuzzComputationUnitLookupTableEntry.java: -------------------------------------------------------------------------------- 1 | package codes.som.oof4j.fizzbuzz.enterprise.computation.unit; 2 | 3 | public enum FizzBuzzComputationUnitLookupTableEntry { 4 | N_LITERAL(null), 5 | FIZZ("Fizz"), 6 | BUZZ("Buzz"), 7 | FIZZ_BUZZ("Fizz Buzz"); 8 | 9 | private final String name; 10 | FizzBuzzComputationUnitLookupTableEntry(String name) { 11 | this.name = name; 12 | } 13 | 14 | @Override 15 | public String toString() { 16 | return name; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/fizzbuzz_enterprise/java/codes/som/oof4j/fizzbuzz/enterprise/computation/unit/FizzBuzzRuleApplicator.java: -------------------------------------------------------------------------------- 1 | package codes.som.oof4j.fizzbuzz.enterprise.computation.unit; 2 | 3 | public class FizzBuzzRuleApplicator { 4 | private static final int DIVISIBLE_BY_THREE = 1; 5 | private static final int DIVISIBLE_BY_FIVE = 1 << 1; 6 | 7 | public static void applyAllRules(int number, Flags flags) { 8 | if (number % 3 == 0) { 9 | flags.setValue(flags.getValue() | DIVISIBLE_BY_THREE); 10 | } 11 | 12 | if (number % 5 == 0) { 13 | flags.setValue(flags.getValue() | DIVISIBLE_BY_FIVE); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/fizzbuzz_enterprise/java/codes/som/oof4j/fizzbuzz/enterprise/computation/unit/Flags.java: -------------------------------------------------------------------------------- 1 | package codes.som.oof4j.fizzbuzz.enterprise.computation.unit; 2 | 3 | public class Flags { 4 | private int value = 0; 5 | 6 | public void setValue(int value) { 7 | this.value = value; 8 | } 9 | 10 | public int getValue() { 11 | return value; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/fizzbuzz_enterprise/java/codes/som/oof4j/fizzbuzz/enterprise/results/FizzBuzzResult.java: -------------------------------------------------------------------------------- 1 | package codes.som.oof4j.fizzbuzz.enterprise.results; 2 | 3 | public interface FizzBuzzResult { 4 | int getNumber(); 5 | String getAsString(); 6 | } 7 | -------------------------------------------------------------------------------- /src/fizzbuzz_enterprise/java/codes/som/oof4j/fizzbuzz/enterprise/results/FizzBuzzResultPublisher.java: -------------------------------------------------------------------------------- 1 | package codes.som.oof4j.fizzbuzz.enterprise.results; 2 | 3 | public interface FizzBuzzResultPublisher { 4 | void registerSubscriber(FizzBuzzResultSubscriber subscriber); 5 | void unregisterSubscriber(FizzBuzzResultSubscriber subscriber); 6 | } 7 | -------------------------------------------------------------------------------- /src/fizzbuzz_enterprise/java/codes/som/oof4j/fizzbuzz/enterprise/results/FizzBuzzResultSubscriber.java: -------------------------------------------------------------------------------- 1 | package codes.som.oof4j.fizzbuzz.enterprise.results; 2 | 3 | public interface FizzBuzzResultSubscriber { 4 | void onResult(FizzBuzzResult result); 5 | } 6 | -------------------------------------------------------------------------------- /src/fizzbuzz_enterprise/java/codes/som/oof4j/fizzbuzz/enterprise/results/internal/AbstractFizzBuzzResultPublisherImpl.java: -------------------------------------------------------------------------------- 1 | package codes.som.oof4j.fizzbuzz.enterprise.results.internal; 2 | 3 | import codes.som.oof4j.fizzbuzz.enterprise.results.FizzBuzzResult; 4 | import codes.som.oof4j.fizzbuzz.enterprise.results.FizzBuzzResultPublisher; 5 | import codes.som.oof4j.fizzbuzz.enterprise.results.FizzBuzzResultSubscriber; 6 | 7 | import java.util.*; 8 | import java.util.concurrent.*; 9 | 10 | public abstract class AbstractFizzBuzzResultPublisherImpl implements FizzBuzzResultPublisher { 11 | private final List subscribers = new CopyOnWriteArrayList<>(); 12 | 13 | @Override 14 | public void registerSubscriber(FizzBuzzResultSubscriber subscriber) { 15 | this.subscribers.add(subscriber); 16 | } 17 | 18 | @Override 19 | public void unregisterSubscriber(FizzBuzzResultSubscriber subscriber) { 20 | this.subscribers.remove(subscriber); 21 | } 22 | 23 | protected void publishResult(FizzBuzzResult result) { 24 | for (FizzBuzzResultSubscriber subscriber : this.subscribers) { 25 | subscriber.onResult(result); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/fizzbuzz_enterprise/java/codes/som/oof4j/fizzbuzz/enterprise/results/internal/FizzBuzzResultImpl.java: -------------------------------------------------------------------------------- 1 | package codes.som.oof4j.fizzbuzz.enterprise.results.internal; 2 | 3 | import codes.som.oof4j.fizzbuzz.enterprise.results.FizzBuzzResult; 4 | 5 | public class FizzBuzzResultImpl implements FizzBuzzResult { 6 | private final int flags; 7 | private final String representation; 8 | 9 | public FizzBuzzResultImpl(int number, String representation) { 10 | this.flags = number; 11 | this.representation = representation; 12 | } 13 | 14 | 15 | @Override 16 | public int getNumber() { 17 | return flags; 18 | } 19 | 20 | @Override 21 | public String getAsString() { 22 | return representation; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/hello_world/README.md: -------------------------------------------------------------------------------- 1 | # Hello World 2 | 3 | This is a 'sanity check' example. 4 | -------------------------------------------------------------------------------- /src/hello_world/java/codes/som/oof4j/helloworld/HelloWorld.java: -------------------------------------------------------------------------------- 1 | package codes.som.oof4j.helloworld; 2 | 3 | public class HelloWorld { 4 | public static void main(String[] args) { 5 | System.out.println("Hello, world!"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/interface_overlap/README.md: -------------------------------------------------------------------------------- 1 | # Interface Overlap 2 | 3 | This example shows the edge case where one class implements two or more interfaces that have methods with the same signature. 4 | 5 | When using an obfuscator that does not take this scenario into account, we can see strange behaviour: i.e. 6 | 7 | InterfaceA works fine: 8 | ```java 9 | public interface InterfaceA { 10 | void a(); // methodA 11 | void b(); // methodB 12 | } 13 | ``` 14 | 15 | However, when shared inheritance is not taken into account, we see InterfaceB failing: 16 | ```java 17 | public interface InterfaceB { 18 | void a(); // methodB 19 | } 20 | ``` 21 | 22 | This means that when we have an implementing object, the incorrect method can be called: 23 | ```java 24 | Object interfaceOverlap = // ... 25 | ((InterfaceB) interfaceOverlap).a(); // An attempt to call 'methodB' actually invokes the implementation of 'methodA' 26 | ``` 27 | -------------------------------------------------------------------------------- /src/interface_overlap/java/codes/som/oof4j/interfaceoverlap/InterfaceA.java: -------------------------------------------------------------------------------- 1 | package codes.som.oof4j.interfaceoverlap; 2 | 3 | public interface InterfaceA { 4 | void methodA(); 5 | void methodB(); 6 | } 7 | -------------------------------------------------------------------------------- /src/interface_overlap/java/codes/som/oof4j/interfaceoverlap/InterfaceB.java: -------------------------------------------------------------------------------- 1 | package codes.som.oof4j.interfaceoverlap; 2 | 3 | public interface InterfaceB { 4 | void methodB(); 5 | } 6 | -------------------------------------------------------------------------------- /src/interface_overlap/java/codes/som/oof4j/interfaceoverlap/InterfaceOverlap.java: -------------------------------------------------------------------------------- 1 | package codes.som.oof4j.interfaceoverlap; 2 | 3 | public class InterfaceOverlap implements InterfaceA, InterfaceB { 4 | public static void main(String[] args) { 5 | InterfaceOverlap overlap = new InterfaceOverlap(); 6 | overlap.methodA(); 7 | overlap.methodB(); 8 | } 9 | 10 | @Override 11 | public void methodA() { 12 | System.out.println("Method A"); 13 | } 14 | 15 | @Override 16 | public void methodB() { 17 | System.out.println("Method B"); 18 | } 19 | } 20 | --------------------------------------------------------------------------------