use of java.lang.reflect.InvocationTargetException in project platform_frameworks_base by android.
the class TestLooper method messageQueueNext.
private Message messageQueueNext() {
try {
long now = SystemClock.uptimeMillis();
Message prevMsg = null;
Message msg = getMessageLinkedList();
if (msg != null && msg.getTarget() == null) {
// the queue.
do {
prevMsg = msg;
msg = (Message) MESSAGE_NEXT_FIELD.get(msg);
} while (msg != null && !msg.isAsynchronous());
}
if (msg != null) {
if (now >= msg.getWhen()) {
// Got a message.
if (prevMsg != null) {
MESSAGE_NEXT_FIELD.set(prevMsg, MESSAGE_NEXT_FIELD.get(msg));
} else {
MESSAGE_QUEUE_MESSAGES_FIELD.set(mLooper.getQueue(), MESSAGE_NEXT_FIELD.get(msg));
}
MESSAGE_NEXT_FIELD.set(msg, null);
MESSAGE_MARK_IN_USE_METHOD.invoke(msg);
return msg;
}
}
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException("Access failed in TestLooper", e);
}
return null;
}
use of java.lang.reflect.InvocationTargetException in project ninja by ninjaframework.
the class Lambdas method getSerializedLambda.
/**
* Tries to get a SerializedLambda from an Object by searching the class
* hierarchy for a <code>writeReplace</code> method. The lambda must
* be serializable in order for this method to return a value.
* @param lambda An object that is an instance of a functional interface.
* @return The SerializedLambda
*/
public static SerializedLambda getSerializedLambda(Object lambda) {
Objects.requireNonNull(lambda);
if (!(lambda instanceof java.io.Serializable)) {
throw new IllegalArgumentException("Functional object does not implement java.io.Serializable");
}
for (Class<?> clazz = lambda.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
try {
Method replaceMethod = clazz.getDeclaredMethod("writeReplace");
replaceMethod.setAccessible(true);
Object serializedForm = replaceMethod.invoke(lambda);
if (serializedForm instanceof SerializedLambda) {
return (SerializedLambda) serializedForm;
}
} catch (NoSuchMethodError e) {
// fall through the loop and try the next class
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException("Functional object is not a lambda");
} catch (SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new RuntimeException("Unable to cleanly serialize lambda", e);
}
}
throw new RuntimeException("writeReplace method not found");
}
use of java.lang.reflect.InvocationTargetException in project spring-loaded by spring-projects.
the class GroovyTests method methodCallTargetComingAndGoing.
@Ignore
// Are we just not having to because isCompilable is off?
@Test
public void methodCallTargetComingAndGoing() throws Exception {
binLoader = new TestClassloaderWithRewriting();
String t = "simple.BasicG";
String target = "simple.BasicGTarget";
TypeRegistry r = getTypeRegistry(t + "," + target);
ReloadableType rtype = r.addType(t, loadBytesForClass(t));
ReloadableType rtypeTarget = r.addType(target, loadBytesForClass(target));
// exist on BasicGTarget
try {
result = runUnguarded(rtype.getClazz(), "run");
fail();
} catch (InvocationTargetException ite) {
ite.printStackTrace();
assertCause(ite, "MissingMethodException");
}
// Now a version of BasicGTarget is loaded that does define the method
rtypeTarget.loadNewVersion("2", retrieveRename(target, target + "2"));
result = runUnguarded(rtype.getClazz(), "run");
assertEquals("hw", result.returnValue);
// Now load the original version so the method is gone again
// retrieveRename(target,
rtypeTarget.loadNewVersion("3", rtypeTarget.bytesInitial);
// "2"));
try {
result = runUnguarded(rtype.getClazz(), "run");
fail();
} catch (InvocationTargetException ite) {
ite.printStackTrace();
assertCause(ite, "MissingMethodException");
}
// Here is the stack when it fails with a NSME:
// java.lang.reflect.InvocationTargetException
// at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
// at
// sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
// at
// sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
// at java.lang.reflect.Method.invoke(Method.java:597)
// at
// org.springsource.loaded.test.SpringLoadedTests.runUnguarded(SpringLoadedTests.java:201)
// at
// org.springsource.loaded.test.GroovyTests.methodCallTargetComingAndGoing(GroovyTests.java:340)
// at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
// at
// sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
// at
// sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
// at java.lang.reflect.Method.invoke(Method.java:597)
// at
// org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
// at
// org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
// at
// org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
// at
// org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
// at
// org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
// at
// org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
// at
// org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
// at
// org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
// at
// org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
// at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
// at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
// at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
// at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
// at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
// at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
// at
// org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
// at
// org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
// at
// org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
// at
// org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
// at
// org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
// at
// org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
// Caused by: java.lang.NoSuchMethodError:
// BasicGTarget.foo()Ljava/lang/String;
// at
// org.springsource.loaded.TypeRegistry.istcheck(TypeRegistry.java:1090)
// at simple.BasicGTarget$foo.call(Unknown Source)
// at simple.BasicG.run(BasicG.groovy:6)
// ... 31 more
}
use of java.lang.reflect.InvocationTargetException in project spring-loaded by spring-projects.
the class MethodInvokerRewriterTests method invokevirtualNonCatchersErrorScenario.
@Test
public void invokevirtualNonCatchersErrorScenario() throws Exception {
String top = "virtual.FourTop";
String bot = "virtual.FourBot";
TypeRegistry typeRegistry = getTypeRegistry(top + "," + bot);
// The first top does not define foo()
// ReloadableType topR =
typeRegistry.addType(top, loadBytesForClass(top));
ReloadableType botR = typeRegistry.addType(bot, loadBytesForClass(bot));
result = runUnguarded(botR.getClazz(), "run");
assertEquals(42, result.returnValue);
// Dont load new Top, which means the new method that the subtype will call
// will not exist
// topR.loadNewVersion(retrieveRename(top, top + "2"));
botR.loadNewVersion(retrieveRename(bot, bot + "2", top + "2:" + top));
// introduced into FourTop
try {
result = runUnguarded(botR.getClazz(), "run");
fail("Should have failed!");
} catch (InvocationTargetException ite) {
assertTrue(ite.getCause() instanceof NoSuchMethodError);
assertEquals("FourBot.bar()I", ite.getCause().getMessage());
}
}
use of java.lang.reflect.InvocationTargetException in project spring-loaded by spring-projects.
the class MethodInvokerRewriterTests method rewriteInvokeInterface4_methodDeletion.
/**
* A method is removed from an interface.
*/
@Test
public void rewriteInvokeInterface4_methodDeletion() throws Exception {
TypeRegistry typeRegistry = getTypeRegistry("tgt.SimpleIClass,tgt.SimpleI");
ReloadableType intface = typeRegistry.addType("tgt.SimpleI", loadBytesForClass("tgt.SimpleI"));
typeRegistry.addType("tgt.SimpleIClass", loadBytesForClass("tgt.SimpleIClass"));
byte[] callerbytes = loadBytesForClass("tgt.StaticICaller");
byte[] rewrittenBytes = MethodInvokerRewriter.rewrite(typeRegistry, callerbytes);
Class<?> callerClazz = loadit("tgt.StaticICaller", rewrittenBytes);
Result result = runUnguarded(callerClazz, "run");
assertEquals(123, result.returnValue);
// new interface version has method removed
intface.loadNewVersion("2", retrieveRename("tgt.SimpleI", "tgt.SimpleI004"));
try {
// run the original working thing post-reload - check it is still ok
result = runUnguarded(callerClazz, "run");
fail("Method no longer exists, should not have been callable");
} catch (InvocationTargetException ite) {
assertTrue(ite.getCause() instanceof NoSuchMethodError);
assertEquals("SimpleI.toInt(Ljava/lang/String;)I", ite.getCause().getMessage());
}
// new interface version has method re-added
intface.loadNewVersion("3", retrieveRename("tgt.SimpleI", "tgt.SimpleI"));
result = runUnguarded(callerClazz, "run");
assertEquals(123, result.returnValue);
}
Aggregations