Skip to content

Commit

Permalink
Feedback and updates
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleAure committed Oct 26, 2023
1 parent 3ef810a commit e68b1d7
Showing 1 changed file with 49 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.objectweb.asm.ClassReader;
Expand Down Expand Up @@ -71,6 +71,7 @@ public static void setup() {
majorCodeMap.put("19", 63);
majorCodeMap.put("20", 64);
majorCodeMap.put("21", 65);
majorCodeMap.put("22", 65);
}

/**
Expand Down Expand Up @@ -190,6 +191,7 @@ public void testJava6BasicInjection() {

transformedClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}

Expand All @@ -207,6 +209,7 @@ public void testJava7BasicInjection() {

transformedClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}

Expand All @@ -224,6 +227,7 @@ public void testJava7StaticInit() {

transformedClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}

Expand All @@ -242,6 +246,7 @@ public void testJava8Basic() {

transformedClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}

Expand All @@ -259,6 +264,7 @@ public void testJava8Lambdas() {

transformedClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}

Expand All @@ -276,6 +282,7 @@ public void testJava8StaticInit() {

transformedClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}

Expand Down Expand Up @@ -303,11 +310,9 @@ public void testJava8StaticInitLambdasWithInnerClass() {

Class<?> loadedClassPreTransform = testLoaderPreTransform.loadClass(classUnderTest);
loadedClassPreTransform.newInstance();



byte[] classUnderTestBytes = transformedClassBytes(classUnderTest, classPath);
byte[] classUnderTestBytesInnerClass = transformedClassBytes(classUnderTestInnerClass, classPath + "$Converter.class");
byte[] classUnderTestBytes = transformedClassBytes(classUnderTest, classPath, preTransformBytes);
byte[] classUnderTestBytesInnerClass = transformedClassBytes(classUnderTestInnerClass, classPath + "$Converter.class", preTransformBytesInnerClass);

Map<String, byte[]> classDefs = new HashMap<String, byte[]>();
classDefs.put(classUnderTest, classUnderTestBytes);
Expand All @@ -321,6 +326,7 @@ public void testJava8StaticInitLambdasWithInnerClass() {


} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}

Expand All @@ -340,12 +346,13 @@ public void testJava8LambdaInjection() {


} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}

}

@Test //TODO expand this test to actually test Java 11 features
@Test //TODO expand this test to actually test Java 11 features https://openjdk.org/jeps/323
public void testJava11Basic() {
assumeTrue(Integer.parseInt(System.getProperty("java.specification.version")) >= 11);
String classPath = "test/test data/com/ibm/example/bytecode/HelloWorldJava11.class";
Expand All @@ -357,12 +364,13 @@ public void testJava11Basic() {

transformedClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}

}

@Test //TODO expand this test to actually test Java 17 features
@Test //TODO expand this test to actually test Java 17 features https://openjdk.org/jeps/409
public void testJava17Basic() {
assumeTrue(Integer.parseInt(System.getProperty("java.specification.version")) >= 17);
String classPath = "test/test data/com/ibm/example/bytecode/HelloWorldJava17.class";
Expand All @@ -374,13 +382,15 @@ public void testJava17Basic() {

transformedClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}

}

//TODO update transformer to support Java 21
// @Test //TODO expand this test to actually test Java 21 features
//FIXME - This test is currently failing because isTransformPossible returns false
//TODO expand this test to actually test Java 21 features https://openjdk.org/jeps/441
//@Test
public void testJava21Basic() {
assumeTrue(Integer.parseInt(System.getProperty("java.specification.version")) >= 21);
String classPath = "test/test data/com/ibm/example/bytecode/HelloWorldJava21.class";
Expand All @@ -392,88 +402,33 @@ public void testJava21Basic() {

transformedClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}

}


private void writeStringToFile(String toWrite, String path) {
PrintWriter file;
try {
file = new PrintWriter(path);
File dir = new File(path).getParentFile();
dir.mkdirs();

try (PrintWriter file = new PrintWriter(path)) {
file.println(toWrite);
file.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

// private void writeBytesToFile(byte[] toWrite, String path) {
// try {
// FileOutputStream fos = new FileOutputStream(path);
// fos.write(toWrite);
// fos.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }


//turn this on to get detailed ASM output in the project folder.
public final boolean dumpTrace = false;

// private byte[] transformedClassBytesJava8Injector(String className, String path) {
// byte[] transformedClass = null;
//
// try {
// //LibertyRuntimeTransformer.setInjectAtTransform(false);
//
// String classUnderTest = className;
// byte[] pre = makeBytesFromFile(path);
// byte[] staticInject = staticTransform.transform(pre);
// byte[] postDynamic = LibertyRuntimeTransformer.transform(pre, false);
//
// //Case that tries to fully emulate what the runtime ends up doing.
// if (staticInject == null) {
// staticInject = pre;
// }
// byte[] post = LibertyRuntimeTransformer.transform(staticInject, false);
//
// String preTransform = dumpClassViaASM(pre);
// //System.out.println("Pre transform class: " + preTransform);
//
// String postStaticTransform = dumpClassViaASM(staticInject);
// //System.out.println("Static transform class: " + postStaticTransform);
//
// String postDynamicTransform = dumpClassViaASM(postDynamic);
// //System.out.println("Post transform class: " + postTransform);
//
// String postStaticAndDynamicTransform = dumpClassViaASM(post);
//
// if (dumpTrace) {
// writeStringToFile(preTransform, "Debug-Pre-"+className);
// writeStringToFile(postStaticTransform, "Debug-Static-"+className);
// writeStringToFile(postDynamicTransform, "Debug-PostDyn-"+className);
// writeStringToFile(postStaticAndDynamicTransform, "Debug-PostStaticDyn-"+className);
// }
// transformedClass = post;
// } catch (Exception e) {
// System.out.println("Exception: " + e.getStackTrace().toString());
// Assert.assertTrue(false);
// }
//
// return transformedClass;
//
// }

private byte[] transformedClassBytes(String className, String path) {
private byte[] transformedClassBytes(String className, String path, byte[] pre) {
byte[] transformedClass = null;

try {
//LibertyRuntimeTransformer.setInjectAtTransform(false);

byte[] pre = makeBytesFromFile(path);
byte[] staticInject = staticTransform.transform(pre);
byte[] postDynamic = LibertyRuntimeTransformer.transform(pre, false);

Expand All @@ -495,15 +450,15 @@ private byte[] transformedClassBytes(String className, String path) {
String postStaticAndDynamicTransform = dumpClassViaASM(post);

if (dumpTrace) {
writeStringToFile(preTransform, "Debug-Pre-"+className);
writeStringToFile(postStaticTransform, "Debug-Static-"+className);
writeStringToFile(postDynamicTransform, "Debug-PostDyn-"+className);
writeStringToFile(postStaticAndDynamicTransform, "Debug-PostStaticDyn-"+className);
writeStringToFile(preTransform, "build/results/Debug-Pre-"+className);
writeStringToFile(postStaticTransform, "build/results/Debug-Static-"+className);
writeStringToFile(postDynamicTransform, "build/results/Debug-PostDyn-"+className);
writeStringToFile(postStaticAndDynamicTransform, "build/results/Debug-PostStaticDyn-"+className);
}
transformedClass = post;
} catch (Exception e) {
System.out.println("Exception: " + e.getStackTrace().toString());
Assert.assertTrue(false);
e.printStackTrace();
fail(e.getMessage());
}

return transformedClass;
Expand All @@ -514,14 +469,29 @@ private Class<?> transformClass(String className, String path) {
Class<?> transformedClass = null;

try {
transformedClass = makeAClassFromBytes(className, transformedClassBytes(className, path), "com.ibm.example.bytecode");
byte[] pre = makeBytesFromFile(path);
assertTrue("isTransformPossible returned false, likely the bytecode level is not supported yet and needs to be tested",
invokeIsTransformPossible(pre));
transformedClass = makeAClassFromBytes(className, transformedClassBytes(className, path, pre), "com.ibm.example.bytecode");
} catch (Exception e) {
System.out.println("Exception: " + e.getStackTrace().toString());
Assert.assertTrue(false);
e.printStackTrace();
fail(e.getMessage());
}

return transformedClass;
}

/**
* Invokes LibertyRuntimeTransformer.isTransformPossible() which is a private method not accessable from child class.
*
* @return the result of invoking the method
* @throws Exception if method cannot be invoked
*/
private boolean invokeIsTransformPossible(byte[] bytes) throws Exception {
Method isTransformPossible = LibertyRuntimeTransformer.class.getDeclaredMethod("isTransformPossible", byte[].class);
isTransformPossible.setAccessible(true);
return (boolean) isTransformPossible.invoke(null, bytes);
}

}

0 comments on commit e68b1d7

Please sign in to comment.