Search in sources :

Example 1 with SynchronizedFieldNode

use of com.newrelic.weave.utils.SynchronizedFieldNode in project newrelic-java-agent by newrelic.

the class Reference method addMethodsAndFields.

/**
 * Fetch all methods and fields of a class node including those inherited from supertypes+interfaces. The results
 * will be stored in the passed in methods and fields lists.
 *
 * @param classCache Cache to use to fetch supertype resources
 * @param classNode The node to scan
 * @param methods classNode's methods will be appended to this list.
 * @param fields classNode's fields will be appended to this list.
 */
private static void addMethodsAndFields(ClassCache classCache, ClassNode classNode, List<MethodNode> methods, List<FieldNode> fields) throws IOException {
    ClassInformation classInfo = classCache.getClassInformation(classNode.name);
    for (MemberInformation methodInfo : classInfo.getAllMethods(classCache)) {
        MethodNode methodNode = new SynchronizedMethodNode();
        methodNode.name = methodInfo.name;
        methodNode.desc = methodInfo.desc;
        methodNode.access = methodInfo.access;
        methods.add(methodNode);
    }
    for (MemberInformation fieldInfo : classInfo.getAllFields(classCache)) {
        fields.add(new SynchronizedFieldNode(WeaveUtils.ASM_API_LEVEL, fieldInfo.access, fieldInfo.name, fieldInfo.desc, null, null));
    }
}
Also used : ClassInformation(com.newrelic.weave.utils.ClassInformation) MethodNode(org.objectweb.asm.tree.MethodNode) SynchronizedMethodNode(com.newrelic.weave.utils.SynchronizedMethodNode) SynchronizedMethodNode(com.newrelic.weave.utils.SynchronizedMethodNode) MemberInformation(com.newrelic.weave.utils.ClassInformation.MemberInformation) SynchronizedFieldNode(com.newrelic.weave.utils.SynchronizedFieldNode)

Example 2 with SynchronizedFieldNode

use of com.newrelic.weave.utils.SynchronizedFieldNode in project newrelic-java-agent by newrelic.

the class ClassStructure method getClassStructure.

public static ClassStructure getClassStructure(final Class<?> clazz, final int flags) {
    int access = 0;
    int modifiers = clazz.getModifiers();
    String superName = null;
    if (clazz.isAnnotation()) {
        access |= Opcodes.ACC_ANNOTATION | Opcodes.ACC_INTERFACE;
        // the access modifiers for class visibility are wonky for Annotations
        if (!Modifier.isPrivate(modifiers)) {
            access |= Opcodes.ACC_PUBLIC;
        }
        superName = "java/lang/Object";
    } else if (clazz.isInterface()) {
        access |= Opcodes.ACC_INTERFACE;
        superName = "java/lang/Object";
    } else if (clazz.isEnum()) {
        access |= Opcodes.ACC_ENUM | Opcodes.ACC_SUPER;
    } else {
        access |= Opcodes.ACC_SUPER;
    }
    if (Modifier.isAbstract(modifiers)) {
        access |= Opcodes.ACC_ABSTRACT;
    }
    if (!clazz.isAnnotation()) {
        if (Modifier.isPublic(modifiers)) {
            access |= Opcodes.ACC_PUBLIC;
        } else if (Modifier.isPrivate(modifiers)) {
            access |= Opcodes.ACC_PRIVATE;
        } else if (Modifier.isProtected(modifiers)) {
            access |= Opcodes.ACC_PROTECTED;
        }
    }
    if (Modifier.isFinal(modifiers)) {
        access |= Opcodes.ACC_FINAL;
    }
    if (clazz.getSuperclass() != null) {
        superName = Type.getType(clazz.getSuperclass()).getInternalName();
    }
    // int ACC_SYNTHETIC = 0x1000; // class, field, method
    // int ACC_ANNOTATION = 0x2000; // class
    // int ACC_ENUM = 0x4000; // class(?) field inner
    String[] interfaces = new String[clazz.getInterfaces().length];
    for (int i = 0; i < interfaces.length; i++) {
        interfaces[i] = Type.getType(clazz.getInterfaces()[i]).getInternalName();
    }
    final ClassStructure structure = new ClassStructure(Type.getType(clazz).getInternalName(), access, superName, interfaces);
    if ((flags & CLASS_ANNOTATIONS) == CLASS_ANNOTATIONS) {
        Annotation[] annotations = clazz.getAnnotations();
        if (annotations.length > 0) {
            structure.classAnnotations = new HashMap<>();
            for (Annotation annotation : annotations) {
                AnnotationDetails annotationDetails = getAnnotationDetails(annotation);
                structure.classAnnotations.put(annotationDetails.desc, annotationDetails);
            }
        }
    }
    if (structure.classAnnotations == null) {
        structure.classAnnotations = Collections.emptyMap();
    }
    if (isFieldFlagSet(flags)) {
        structure.fields = new HashMap<>();
        Field[] declaredFields = ClassReflection.getDeclaredFields(clazz);
        for (Field f : declaredFields) {
            FieldNode field = new SynchronizedFieldNode(0, f.getName(), Type.getDescriptor(f.getDeclaringClass()), null, null);
            structure.fields.put(f.getName(), field);
        }
    } else {
        structure.fields = ImmutableMap.of();
    }
    if (isMethodFlagSet(flags)) {
        structure.methods = new HashMap<>();
        try {
            AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {

                @Override
                public Object run() {
                    java.lang.reflect.Method[] methods = ClassReflection.getDeclaredMethods(clazz);
                    for (java.lang.reflect.Method m : methods) {
                        structure.methods.put(Method.getMethod(m), getMethodDetails(m, flags, Modifier.isStatic(m.getModifiers())));
                    }
                    return null;
                }
            });
        } catch (Exception ex) {
            Agent.LOG.log(Level.FINEST, "Error getting methods of " + clazz.getName(), ex);
        }
        try {
            AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {

                @Override
                public Object run() {
                    Constructor<?>[] constructors = ClassReflection.getDeclaredConstructors(clazz);
                    for (Constructor<?> c : constructors) {
                        structure.methods.put(Method.getMethod(c), getMethodDetails(c, flags, false));
                    }
                    return null;
                }
            });
        } catch (Exception ex) {
            Agent.LOG.log(Level.FINEST, "Error getting constructors of " + clazz.getName(), ex);
        }
    }
    return structure;
}
Also used : SynchronizedFieldNode(com.newrelic.weave.utils.SynchronizedFieldNode) FieldNode(org.objectweb.asm.tree.FieldNode) Constructor(java.lang.reflect.Constructor) Method(org.objectweb.asm.commons.Method) Annotation(java.lang.annotation.Annotation) IOException(java.io.IOException) SynchronizedFieldNode(com.newrelic.weave.utils.SynchronizedFieldNode) Field(java.lang.reflect.Field) AccessibleObject(java.lang.reflect.AccessibleObject)

Aggregations

SynchronizedFieldNode (com.newrelic.weave.utils.SynchronizedFieldNode)2 ClassInformation (com.newrelic.weave.utils.ClassInformation)1 MemberInformation (com.newrelic.weave.utils.ClassInformation.MemberInformation)1 SynchronizedMethodNode (com.newrelic.weave.utils.SynchronizedMethodNode)1 IOException (java.io.IOException)1 Annotation (java.lang.annotation.Annotation)1 AccessibleObject (java.lang.reflect.AccessibleObject)1 Constructor (java.lang.reflect.Constructor)1 Field (java.lang.reflect.Field)1 Method (org.objectweb.asm.commons.Method)1 FieldNode (org.objectweb.asm.tree.FieldNode)1 MethodNode (org.objectweb.asm.tree.MethodNode)1