Search in sources :

Example 6 with Member

use of java.lang.reflect.Member in project jersey by jersey.

the class CdiComponentProvider method processInjectionTarget.

@SuppressWarnings("unused")
private void processInjectionTarget(@Observes final ProcessInjectionTarget event) {
    final InjectionTarget it = event.getInjectionTarget();
    final Class<?> componentClass = event.getAnnotatedType().getJavaClass();
    final Set<InjectionPoint> cdiInjectionPoints = filterHk2InjectionPointsOut(it.getInjectionPoints());
    for (final InjectionPoint injectionPoint : cdiInjectionPoints) {
        final Member member = injectionPoint.getMember();
        if (member instanceof Field) {
            addInjecteeToSkip(componentClass, fieldsToSkip, (Field) member);
        } else if (member instanceof Method) {
            addInjecteeToSkip(componentClass, methodsToSkip, (Method) member);
        }
    }
    InjectionManagerInjectedCdiTarget target = null;
    if (isJerseyOrDependencyType(componentClass)) {
        target = new InjectionManagerInjectedCdiTarget(it) {

            @Override
            public Set<InjectionPoint> getInjectionPoints() {
                // CDI will not treat these classes as CDI beans (as they are not).
                return Collections.emptySet();
            }
        };
    } else if (isJaxRsComponentType(componentClass) || jaxrsInjectableTypes.contains(event.getAnnotatedType().getBaseType())) {
        target = new InjectionManagerInjectedCdiTarget(it) {

            @Override
            public Set<InjectionPoint> getInjectionPoints() {
                // Inject CDI beans into JAX-RS resources/providers/application.
                return cdiInjectionPoints;
            }
        };
    }
    if (target != null) {
        notify(target);
        //noinspection unchecked
        event.setInjectionTarget(target);
    }
}
Also used : Field(java.lang.reflect.Field) Set(java.util.Set) HashSet(java.util.HashSet) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) InjectionTarget(javax.enterprise.inject.spi.InjectionTarget) ProcessInjectionTarget(javax.enterprise.inject.spi.ProcessInjectionTarget) Method(java.lang.reflect.Method) Member(java.lang.reflect.Member)

Example 7 with Member

use of java.lang.reflect.Member in project quasar by puniverse.

the class ExtendedStackTrace method getMethod.

protected /*Executable*/
Member getMethod(final ExtendedStackTraceElement este) {
    if (este.getDeclaringClass() == null)
        return null;
    Member[] ms = getMethods(este.getDeclaringClass());
    Member method = null;
    for (Member m : ms) {
        if (este.getMethodName().equals(m.getName())) {
            if (method == null)
                method = m;
            else {
                // more than one match
                method = null;
                break;
            }
        }
    }
    if (method == null && este.getLineNumber() >= 0) {
        try {
            final AtomicReference<String> descriptor = new AtomicReference<>();
            ASMUtil.accept(este.getDeclaringClass(), ClassReader.SKIP_FRAMES, new ClassVisitor(Opcodes.ASM5) {

                @Override
                public MethodVisitor visitMethod(int access, String name, final String desc, String signature, String[] exceptions) {
                    MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
                    if (descriptor.get() == null && este.getMethodName().equals(name)) {
                        mv = new MethodVisitor(api, mv) {

                            int minLine = Integer.MAX_VALUE, maxLine = Integer.MIN_VALUE;

                            @Override
                            public void visitLineNumber(int line, Label start) {
                                if (line < minLine)
                                    minLine = line;
                                if (line > maxLine)
                                    maxLine = line;
                            }

                            @Override
                            public void visitEnd() {
                                if (minLine <= este.getLineNumber() && maxLine >= este.getLineNumber())
                                    descriptor.set(desc);
                                super.visitEnd();
                            }
                        };
                    }
                    return mv;
                }
            });
            if (descriptor.get() != null) {
                final String desc = descriptor.get();
                for (Member m : ms) {
                    if (este.getMethodName().equals(getName(m)) && desc.equals(getDescriptor(m))) {
                        method = m;
                        break;
                    }
                }
            }
        } catch (Exception e) {
            if (!(e instanceof UnsupportedOperationException))
                e.printStackTrace();
        }
    }
    return method;
}
Also used : Label(org.objectweb.asm.Label) AtomicReference(java.util.concurrent.atomic.AtomicReference) ClassVisitor(org.objectweb.asm.ClassVisitor) MethodVisitor(org.objectweb.asm.MethodVisitor) Member(java.lang.reflect.Member)

Example 8 with Member

use of java.lang.reflect.Member in project quasar by puniverse.

the class Fiber method checkInstrumentation.

@SuppressWarnings("null")
private static boolean checkInstrumentation(ExtendedStackTrace st, boolean fromUncaughtExc) {
    if (fromUncaughtExc && st.get().length > 0 && st.get()[0] != null) {
        final ExtendedStackTraceElement first = st.get()[0];
        if (!first.getDeclaringClass().equals(ClassCastException.class) && !(first.getDeclaringClass().equals(NullPointerException.class) && first.getDeclaringClass().getName().startsWith("co.paralleluniverse.fibers")))
            return true;
    }
    boolean ok = true;
    StringBuilder stackTrace = null;
    final ExtendedStackTraceElement[] stes = st.get();
    for (int i = 0; i < stes.length; i++) {
        final ExtendedStackTraceElement ste = stes[i];
        if (ste.getClassName().equals(Thread.class.getName()) && ste.getMethodName().equals("getStackTrace"))
            continue;
        if (ste.getClassName().equals(ExtendedStackTrace.class.getName()))
            continue;
        if (!ok)
            printTraceLine(stackTrace, ste);
        if (ste.getClassName().contains("$$Lambda$"))
            continue;
        if (!ste.getClassName().equals(Fiber.class.getName()) && !ste.getClassName().startsWith(Fiber.class.getName() + '$') && !ste.getClassName().equals(Stack.class.getName()) && !SuspendableHelper.isWaiver(ste.getClassName(), ste.getMethodName())) {
            final Class<?> clazz = ste.getDeclaringClass();
            final boolean classInstrumented = SuspendableHelper.isInstrumented(clazz);
            final Member /*Executable*/
            m = SuspendableHelper.lookupMethod(ste);
            if (m != null) {
                final boolean methodInstrumented = SuspendableHelper.isInstrumented(m);
                final Pair<Boolean, Instrumented> callSiteInstrumented = SuspendableHelper.isCallSiteInstrumented(m, ste.getLineNumber(), ste.getBytecodeIndex(), stes, i);
                if (!classInstrumented || !methodInstrumented || !callSiteInstrumented.getFirst()) {
                    if (ok)
                        stackTrace = initTrace(i, stes);
                    if (!classInstrumented || !methodInstrumented)
                        stackTrace.append(" **");
                    else if (!callSiteInstrumented.getFirst())
                        stackTrace.append(" !! (instrumented suspendable calls at: ").append(callSitesString(callSiteInstrumented.getSecond())).append(")");
                    ok = false;
                }
            } else {
                if (ok)
                    stackTrace = initTrace(i, stes);
                // Methods can only be found via source lines in @Instrumented annotations
                stackTrace.append(" **");
                ok = false;
            }
        } else if (ste.getClassName().equals(Fiber.class.getName()) && ste.getMethodName().equals("run1")) {
            if (!ok) {
                final String str = "Uninstrumented whole methods ('**') or single calls ('!!') detected: " + stackTrace;
                if (Debug.isUnitTest())
                    throw new VerifyInstrumentationException(str);
                System.err.println("WARNING: " + str);
            }
            return ok;
        }
    }
    throw new IllegalStateException("Not run through Fiber.exec(). (trace: " + Arrays.toString(stes) + ")");
}
Also used : ExtendedStackTrace(co.paralleluniverse.common.util.ExtendedStackTrace) Member(java.lang.reflect.Member) ExtendedStackTraceElement(co.paralleluniverse.common.util.ExtendedStackTraceElement)

Example 9 with Member

use of java.lang.reflect.Member in project quasar by puniverse.

the class ExtendedStackTraceTest method testAll.

@Test
public void testAll() {
    ExtendedStackTraceElement[] plain = new ExtendedStackTrace(new Throwable()).get();
    ExtendedStackTraceElement[] hotspot = isHotSpotSupported() ? new ExtendedStackTraceHotSpot(new Throwable()).get() : null;
    ExtendedStackTraceElement[] security = new ExtendedStackTraceClassContext().get();
    int length = plain.length;
    if (hotspot != null)
        assertEquals(length, hotspot.length);
    assertEquals(length, security.length);
    for (int i = 0; i < plain.length; i++) {
        if (hotspot != null)
            assertNotNull(hotspot[i].getMethodName(), hotspot[i].getMethodName());
        if (!ExtendedStackTraceClassContext.skipSTE(security[i].getStackTraceElement()))
            assertNotNull(security[i].getMethodName(), security[i].getMethodName());
        if (!skipJunit(plain[i]))
            assertNotNull(plain[i].getMethodName(), plain[i].getMethodName());
        Member m = null;
        if (hotspot != null)
            m = hotspot[i].getMethod();
        else if (!ExtendedStackTraceClassContext.skipSTE(security[i].getStackTraceElement()))
            m = security[i].getMethod();
        else if (!skipJunit(plain[i]))
            m = plain[i].getMethod();
        if (m != null) {
            if (!skipJunit(plain[i]))
                assertEquals("" + i, m, plain[i].getMethod());
            if (!ExtendedStackTraceClassContext.skipSTE(security[i].getStackTraceElement()))
                assertEquals("" + i, m, security[i].getMethod());
        }
    }
}
Also used : Member(java.lang.reflect.Member) Test(org.junit.Test)

Example 10 with Member

use of java.lang.reflect.Member in project quasar by puniverse.

the class SuspendableHelper method isCallSiteInstrumented.

public static Pair<Boolean, Instrumented> isCallSiteInstrumented(/*Executable*/
Member m, int sourceLine, int bci, ExtendedStackTraceElement[] stes, int currentSteIdx) {
    if (m == null)
        return new Pair<>(false, null);
    if (isSyntheticAndNotLambda(m))
        return new Pair<>(true, null);
    final ExtendedStackTraceElement calleeSte = currentSteIdx - 1 >= 0 ? stes[currentSteIdx - 1] : null;
    if (calleeSte != null && // `verifySuspend` and `popMethod` calls are not suspendable call sites, not verifying them.
    ((calleeSte.getClassName().equals(Fiber.class.getName()) && calleeSte.getMethodName().equals("verifySuspend")) || (calleeSte.getClassName().equals(Stack.class.getName()) && calleeSte.getMethodName().equals("popMethod")))) {
        return new Pair<>(true, null);
    } else {
        final Instrumented i = getAnnotation(m, Instrumented.class);
        if (i != null) {
            if (calleeSte != null && i.suspendableCallSiteNames() != null) {
                final Member callee = calleeSte.getMethod();
                if (callee == null) {
                    boolean ok = false;
                    final String methodName = "." + calleeSte.getMethodName() + "(";
                    for (String callsite : i.suspendableCallSiteNames()) {
                        if (callsite.contains(methodName)) {
                            ok = true;
                            break;
                        }
                    }
                    return new Pair(ok, i);
                } else {
                    final String nameAndDescSuffix = "." + callee.getName() + ASMUtil.getDescriptor(callee);
                    boolean ok = false;
                    final String[] callsites = i.suspendableCallSiteNames();
                    for (String callsite : callsites) {
                        if (callsite.endsWith(nameAndDescSuffix)) {
                            Class<?> callsiteOwner = null;
                            try {
                                callsiteOwner = Class.forName(getCallsiteOwner(callsite));
                            } catch (ClassNotFoundException e) {
                            }
                            if (callsiteOwner != null) {
                                final Class<?> owner = callee.getDeclaringClass();
                                if (callsiteOwner.isAssignableFrom(owner)) {
                                    ok = true;
                                    break;
                                }
                            }
                        }
                    }
                    return new Pair(ok, i);
                }
            } else //                } 
            if (sourceLine >= 0) {
                final int[] scs = i.suspendableCallSites();
                for (int j : scs) {
                    if (j == sourceLine)
                        return new Pair<>(true, i);
                }
            }
            return new Pair<>(false, i);
        }
        return new Pair<>(false, null);
    }
}
Also used : Instrumented(co.paralleluniverse.fibers.Instrumented) Member(java.lang.reflect.Member) ExtendedStackTraceElement(co.paralleluniverse.common.util.ExtendedStackTraceElement) Pair(co.paralleluniverse.common.util.Pair)

Aggregations

Member (java.lang.reflect.Member)59 Method (java.lang.reflect.Method)13 Annotation (java.lang.annotation.Annotation)8 AccessibleObject (java.lang.reflect.AccessibleObject)7 MetaInfo (org.qi4j.api.common.MetaInfo)7 Field (java.lang.reflect.Field)6 Optional (org.qi4j.api.common.Optional)6 ValueConstraintsInstance (org.qi4j.runtime.composite.ValueConstraintsInstance)6 ValueConstraintsModel (org.qi4j.runtime.composite.ValueConstraintsModel)6 TypeVariable (java.lang.reflect.TypeVariable)5 NodeId (com.google.inject.grapher.NodeId)4 Constructor (java.lang.reflect.Constructor)4 Type (java.lang.reflect.Type)4 ArrayList (java.util.ArrayList)4 ManyAssociation (org.qi4j.api.association.ManyAssociation)4 NamedAssociation (org.qi4j.api.association.NamedAssociation)4 QualifiedName (org.qi4j.api.common.QualifiedName)4 ManyAssociationModel (org.qi4j.runtime.association.ManyAssociationModel)4 NamedAssociationModel (org.qi4j.runtime.association.NamedAssociationModel)4 HashSet (java.util.HashSet)3