Search in sources :

Example 36 with InvocationTargetException

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;
}
Also used : Message(android.os.Message) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 37 with InvocationTargetException

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");
}
Also used : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) SerializedLambda(java.lang.invoke.SerializedLambda)

Example 38 with InvocationTargetException

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
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TestClassloaderWithRewriting(org.springsource.loaded.test.infra.TestClassloaderWithRewriting) TypeRegistry(org.springsource.loaded.TypeRegistry) InvocationTargetException(java.lang.reflect.InvocationTargetException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 39 with InvocationTargetException

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());
    }
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test)

Example 40 with InvocationTargetException

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);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) InvocationTargetException(java.lang.reflect.InvocationTargetException) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)2006 Method (java.lang.reflect.Method)902 IOException (java.io.IOException)233 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)179 ArrayList (java.util.ArrayList)163 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)157 Test (org.junit.Test)136 Constructor (java.lang.reflect.Constructor)133 File (java.io.File)110 Field (java.lang.reflect.Field)99 CoreException (org.eclipse.core.runtime.CoreException)93 HashMap (java.util.HashMap)88 ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)87 List (java.util.List)78 Map (java.util.Map)76 IStatus (org.eclipse.core.runtime.IStatus)66 Status (org.eclipse.core.runtime.Status)50 IFile (org.eclipse.core.resources.IFile)37 AccessibleObject (java.lang.reflect.AccessibleObject)34 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)34