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));
}
}
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;
}
Aggregations