Search in sources :

Example 61 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 62 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)
            return new Pair<>(false, i);
        if (calleeSte != null && i.suspendableCallSiteNames() != null) {
            // check by callsite name (fails for bootstrapped lambdas)
            final Member callee = calleeSte.getMethod();
            if (callee == null) {
                final String methodName = "." + calleeSte.getMethodName() + "(";
                for (String callsite : i.suspendableCallSiteNames()) {
                    if (callsite.contains(methodName)) {
                        return new Pair(true, i);
                    }
                }
            } else {
                final String nameAndDescSuffix = "." + callee.getName() + ASMUtil.getDescriptor(callee);
                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 (declareInCommonAncestor(nameAndDescSuffix, owner, callsiteOwner)) {
                                return new Pair(true, i);
                            }
                        }
                    }
                }
            }
        }
        if (bci >= 0) {
            // check by bci; may be brittle
            final int[] scs = i.suspendableCallSitesOffsetsAfterInstr();
            for (int j : scs) {
                if (j == bci)
                    return new Pair<>(true, i);
            }
        } else if (sourceLine >= 0) {
            // check by source line
            final int[] scs = i.suspendableCallSites();
            for (int j : scs) {
                if (j == sourceLine)
                    return new Pair<>(true, i);
            }
        }
        return new Pair<>(false, i);
    }
}
Also used : Instrumented(co.paralleluniverse.fibers.Instrumented) Member(java.lang.reflect.Member) ExtendedStackTraceElement(co.paralleluniverse.common.util.ExtendedStackTraceElement) Pair(co.paralleluniverse.common.util.Pair)

Example 63 with Member

use of java.lang.reflect.Member in project elasticsearch by elastic.

the class Errors method formatSource.

public static void formatSource(Formatter formatter, Object source) {
    if (source instanceof Dependency) {
        Dependency<?> dependency = (Dependency<?>) source;
        InjectionPoint injectionPoint = dependency.getInjectionPoint();
        if (injectionPoint != null) {
            formatInjectionPoint(formatter, dependency, injectionPoint);
        } else {
            formatSource(formatter, dependency.getKey());
        }
    } else if (source instanceof InjectionPoint) {
        formatInjectionPoint(formatter, null, (InjectionPoint) source);
    } else if (source instanceof Class) {
        formatter.format("  at %s%n", StackTraceElements.forType((Class<?>) source));
    } else if (source instanceof Member) {
        formatter.format("  at %s%n", StackTraceElements.forMember((Member) source));
    } else if (source instanceof TypeLiteral) {
        formatter.format("  while locating %s%n", source);
    } else if (source instanceof Key) {
        Key<?> key = (Key<?>) source;
        formatter.format("  while locating %s%n", convert(key));
    } else {
        formatter.format("  at %s%n", source);
    }
}
Also used : TypeLiteral(org.elasticsearch.common.inject.TypeLiteral) InjectionPoint(org.elasticsearch.common.inject.spi.InjectionPoint) Dependency(org.elasticsearch.common.inject.spi.Dependency) Member(java.lang.reflect.Member) Key(org.elasticsearch.common.inject.Key)

Example 64 with Member

use of java.lang.reflect.Member in project elasticsearch by elastic.

the class Errors method formatInjectionPoint.

public static void formatInjectionPoint(Formatter formatter, Dependency<?> dependency, InjectionPoint injectionPoint) {
    Member member = injectionPoint.getMember();
    Class<? extends Member> memberType = MoreTypes.memberType(member);
    if (memberType == Field.class) {
        dependency = injectionPoint.getDependencies().get(0);
        formatter.format("  while locating %s%n", convert(dependency.getKey()));
        formatter.format("    for field at %s%n", StackTraceElements.forMember(member));
    } else if (dependency != null) {
        formatter.format("  while locating %s%n", convert(dependency.getKey()));
        formatter.format("    for parameter %s at %s%n", dependency.getParameterIndex(), StackTraceElements.forMember(member));
    } else {
        formatSource(formatter, injectionPoint.getMember());
    }
}
Also used : Member(java.lang.reflect.Member)

Example 65 with Member

use of java.lang.reflect.Member in project hackpad by dropbox.

the class ScriptableObject method buildClassCtor.

static <T extends Scriptable> BaseFunction buildClassCtor(Scriptable scope, Class<T> clazz, boolean sealed, boolean mapInheritance) throws IllegalAccessException, InstantiationException, InvocationTargetException {
    Method[] methods = FunctionObject.getMethodList(clazz);
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        if (!method.getName().equals("init"))
            continue;
        Class<?>[] parmTypes = method.getParameterTypes();
        if (parmTypes.length == 3 && parmTypes[0] == ScriptRuntime.ContextClass && parmTypes[1] == ScriptRuntime.ScriptableClass && parmTypes[2] == Boolean.TYPE && Modifier.isStatic(method.getModifiers())) {
            Object[] args = { Context.getContext(), scope, sealed ? Boolean.TRUE : Boolean.FALSE };
            method.invoke(null, args);
            return null;
        }
        if (parmTypes.length == 1 && parmTypes[0] == ScriptRuntime.ScriptableClass && Modifier.isStatic(method.getModifiers())) {
            Object[] args = { scope };
            method.invoke(null, args);
            return null;
        }
    }
    // If we got here, there isn't an "init" method with the right
    // parameter types.
    Constructor<?>[] ctors = clazz.getConstructors();
    Constructor<?> protoCtor = null;
    for (int i = 0; i < ctors.length; i++) {
        if (ctors[i].getParameterTypes().length == 0) {
            protoCtor = ctors[i];
            break;
        }
    }
    if (protoCtor == null) {
        throw Context.reportRuntimeError1("msg.zero.arg.ctor", clazz.getName());
    }
    Scriptable proto = (Scriptable) protoCtor.newInstance(ScriptRuntime.emptyArgs);
    String className = proto.getClassName();
    // Set the prototype's prototype, trying to map Java inheritance to JS
    // prototype-based inheritance if requested to do so.
    Scriptable superProto = null;
    if (mapInheritance) {
        Class<? super T> superClass = clazz.getSuperclass();
        if (ScriptRuntime.ScriptableClass.isAssignableFrom(superClass) && !Modifier.isAbstract(superClass.getModifiers())) {
            Class<? extends Scriptable> superScriptable = extendsScriptable(superClass);
            String name = ScriptableObject.defineClass(scope, superScriptable, sealed, mapInheritance);
            if (name != null) {
                superProto = ScriptableObject.getClassPrototype(scope, name);
            }
        }
    }
    if (superProto == null) {
        superProto = ScriptableObject.getObjectPrototype(scope);
    }
    proto.setPrototype(superProto);
    // Find out whether there are any methods that begin with
    // "js". If so, then only methods that begin with special
    // prefixes will be defined as JavaScript entities.
    final String functionPrefix = "jsFunction_";
    final String staticFunctionPrefix = "jsStaticFunction_";
    final String getterPrefix = "jsGet_";
    final String setterPrefix = "jsSet_";
    final String ctorName = "jsConstructor";
    Member ctorMember = findAnnotatedMember(methods, JSConstructor.class);
    if (ctorMember == null) {
        ctorMember = findAnnotatedMember(ctors, JSConstructor.class);
    }
    if (ctorMember == null) {
        ctorMember = FunctionObject.findSingleMethod(methods, ctorName);
    }
    if (ctorMember == null) {
        if (ctors.length == 1) {
            ctorMember = ctors[0];
        } else if (ctors.length == 2) {
            if (ctors[0].getParameterTypes().length == 0)
                ctorMember = ctors[1];
            else if (ctors[1].getParameterTypes().length == 0)
                ctorMember = ctors[0];
        }
        if (ctorMember == null) {
            throw Context.reportRuntimeError1("msg.ctor.multiple.parms", clazz.getName());
        }
    }
    FunctionObject ctor = new FunctionObject(className, ctorMember, scope);
    if (ctor.isVarArgsMethod()) {
        throw Context.reportRuntimeError1("msg.varargs.ctor", ctorMember.getName());
    }
    ctor.initAsConstructor(scope, proto);
    Method finishInit = null;
    HashSet<String> staticNames = new HashSet<String>(), instanceNames = new HashSet<String>();
    for (Method method : methods) {
        if (method == ctorMember) {
            continue;
        }
        String name = method.getName();
        if (name.equals("finishInit")) {
            Class<?>[] parmTypes = method.getParameterTypes();
            if (parmTypes.length == 3 && parmTypes[0] == ScriptRuntime.ScriptableClass && parmTypes[1] == FunctionObject.class && parmTypes[2] == ScriptRuntime.ScriptableClass && Modifier.isStatic(method.getModifiers())) {
                finishInit = method;
                continue;
            }
        }
        // ignore any compiler generated methods.
        if (name.indexOf('$') != -1)
            continue;
        if (name.equals(ctorName))
            continue;
        Annotation annotation = null;
        String prefix = null;
        if (method.isAnnotationPresent(JSFunction.class)) {
            annotation = method.getAnnotation(JSFunction.class);
        } else if (method.isAnnotationPresent(JSStaticFunction.class)) {
            annotation = method.getAnnotation(JSStaticFunction.class);
        } else if (method.isAnnotationPresent(JSGetter.class)) {
            annotation = method.getAnnotation(JSGetter.class);
        } else if (method.isAnnotationPresent(JSSetter.class)) {
            continue;
        }
        if (annotation == null) {
            if (name.startsWith(functionPrefix)) {
                prefix = functionPrefix;
            } else if (name.startsWith(staticFunctionPrefix)) {
                prefix = staticFunctionPrefix;
            } else if (name.startsWith(getterPrefix)) {
                prefix = getterPrefix;
            } else if (annotation == null) {
                // we deal with that when we see the getter
                continue;
            }
        }
        boolean isStatic = annotation instanceof JSStaticFunction || prefix == staticFunctionPrefix;
        HashSet<String> names = isStatic ? staticNames : instanceNames;
        String propName = getPropertyName(name, prefix, annotation);
        if (names.contains(propName)) {
            throw Context.reportRuntimeError2("duplicate.defineClass.name", name, propName);
        }
        names.add(propName);
        name = propName;
        if (annotation instanceof JSGetter || prefix == getterPrefix) {
            if (!(proto instanceof ScriptableObject)) {
                throw Context.reportRuntimeError2("msg.extend.scriptable", proto.getClass().toString(), name);
            }
            Method setter = findSetterMethod(methods, name, setterPrefix);
            int attr = ScriptableObject.PERMANENT | ScriptableObject.DONTENUM | (setter != null ? 0 : ScriptableObject.READONLY);
            ((ScriptableObject) proto).defineProperty(name, null, method, setter, attr);
            continue;
        }
        if (isStatic && !Modifier.isStatic(method.getModifiers())) {
            throw Context.reportRuntimeError("jsStaticFunction must be used with static method.");
        }
        FunctionObject f = new FunctionObject(name, method, proto);
        if (f.isVarArgsConstructor()) {
            throw Context.reportRuntimeError1("msg.varargs.fun", ctorMember.getName());
        }
        defineProperty(isStatic ? ctor : proto, name, f, DONTENUM);
        if (sealed) {
            f.sealObject();
        }
    }
    // Call user code to complete initialization if necessary.
    if (finishInit != null) {
        Object[] finishArgs = { scope, ctor, proto };
        finishInit.invoke(null, finishArgs);
    }
    // Seal the object if necessary.
    if (sealed) {
        ctor.sealObject();
        if (proto instanceof ScriptableObject) {
            ((ScriptableObject) proto).sealObject();
        }
    }
    return ctor;
}
Also used : JSConstructor(org.mozilla.javascript.annotations.JSConstructor) JSStaticFunction(org.mozilla.javascript.annotations.JSStaticFunction) JSSetter(org.mozilla.javascript.annotations.JSSetter) JSFunction(org.mozilla.javascript.annotations.JSFunction) Member(java.lang.reflect.Member) JSGetter(org.mozilla.javascript.annotations.JSGetter) HashSet(java.util.HashSet) Constructor(java.lang.reflect.Constructor) JSConstructor(org.mozilla.javascript.annotations.JSConstructor) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) AccessibleObject(java.lang.reflect.AccessibleObject) DebuggableObject(org.mozilla.javascript.debug.DebuggableObject)

Aggregations

Member (java.lang.reflect.Member)135 Method (java.lang.reflect.Method)41 Field (java.lang.reflect.Field)30 ArrayList (java.util.ArrayList)13 AccessibleObject (java.lang.reflect.AccessibleObject)12 Type (java.lang.reflect.Type)12 Annotation (java.lang.annotation.Annotation)11 Constructor (java.lang.reflect.Constructor)10 TypeVariable (java.lang.reflect.TypeVariable)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 ParameterizedType (java.lang.reflect.ParameterizedType)7 Map (java.util.Map)7 MetaInfo (org.qi4j.api.common.MetaInfo)7 InjectionPoint (com.google.inject.spi.InjectionPoint)6 AnnotatedElement (java.lang.reflect.AnnotatedElement)6 LinkedHashSet (java.util.LinkedHashSet)6 Optional (org.qi4j.api.common.Optional)6 ValueConstraintsInstance (org.qi4j.runtime.composite.ValueConstraintsInstance)6 ValueConstraintsModel (org.qi4j.runtime.composite.ValueConstraintsModel)6 HashSet (java.util.HashSet)5