Skip to content

Instantly share code, notes, and snippets.

@Tholian
Created September 22, 2021 14:49
Show Gist options
  • Save Tholian/811a8895942628d353d41bcbb51ed63b to your computer and use it in GitHub Desktop.
Save Tholian/811a8895942628d353d41bcbb51ed63b to your computer and use it in GitHub Desktop.
Java compiler breach
public class GenericVarArgTest {
private static InnerClass myInnerClass;
@Test
public void testMethodInvocation()
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
doSomething();
System.out.println("Done .. all fine");
}
private void doSomething() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
myInnerClass = this.new InnerClass();
Method[] methods = myInnerClass.getClass().getMethods();
for (Method aktMethod : methods) {
if (aktMethod.getName().equals("printInteger")) {
// this will always be okay
// Integer myParam = getSomething();
// aktMethod.invoke(myInnerClass, myParam);
// this is just ok, for source / target level <= 1.7
aktMethod.invoke(myInnerClass, getSomething());
}
}
}
public static <T> T getSomething() {
final T result = (T) Integer.valueOf(1);
System.out.println("getSomething will return <T> : " + result);
return result;
}
public class InnerClass {
public String printInteger(final Integer aInteger) {
final String result = "Integer: " + aInteger;
System.out.println("Will return : " + result);
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment