Java Code Examples for javassist.bytecode.ClassFile#getFields()

The following examples show how to use javassist.bytecode.ClassFile#getFields() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClassPathScanner.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public void scan(final Object cls) {
  final ClassFile classFile = (ClassFile)cls;
  AnnotationsAttribute annotations = ((AnnotationsAttribute)classFile.getAttribute(AnnotationsAttribute.visibleTag));
  if (annotations != null) {
    boolean isAnnotated = false;
    for (javassist.bytecode.annotation.Annotation a : annotations.getAnnotations()) {
      if (annotationsToScan.contains(a.getTypeName())) {
        isAnnotated = true;
      }
    }
    if (isAnnotated) {
      List<AnnotationDescriptor> classAnnotations = getAnnotationDescriptors(annotations);
      List<FieldInfo> classFields = classFile.getFields();
      List<FieldDescriptor> fieldDescriptors = new ArrayList<>(classFields.size());
      for (FieldInfo field : classFields) {
        String fieldName = field.getName();
        AnnotationsAttribute fieldAnnotations = ((AnnotationsAttribute) field.getAttribute(AnnotationsAttribute.visibleTag));
        fieldDescriptors.add(new FieldDescriptor(fieldName, field.getDescriptor(), getAnnotationDescriptors(fieldAnnotations)));
      }
      functions.add(new AnnotatedClassDescriptor(classFile.getName(), classAnnotations, fieldDescriptors));
    }
  }
}
 
Example 2
Source File: FieldTransformer.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void addReadWriteMethods(ClassFile classfile)
		throws CannotCompileException {
	List fields = classfile.getFields();
	for (Iterator field_iter = fields.iterator(); field_iter.hasNext();) {
		FieldInfo finfo = (FieldInfo) field_iter.next();
		if ((finfo.getAccessFlags() & AccessFlag.STATIC) == 0
		    && (!finfo.getName().equals(HANDLER_FIELD_NAME))) {
			// case of non-static field
			if (filter.handleRead(finfo.getDescriptor(), finfo
					.getName())) {
				addReadMethod(classfile, finfo);
				readableFields.put(finfo.getName(), finfo
						.getDescriptor());
			}
			if (filter.handleWrite(finfo.getDescriptor(), finfo
					.getName())) {
				addWriteMethod(classfile, finfo);
				writableFields.put(finfo.getName(), finfo
						.getDescriptor());
			}
		}
	}
}
 
Example 3
Source File: ClassPathScanner.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void scan(final Object cls) {
  final ClassFile classFile = (ClassFile)cls;
  AnnotationsAttribute annotations = ((AnnotationsAttribute)classFile.getAttribute(AnnotationsAttribute.visibleTag));
  if (annotations != null) {
    boolean isAnnotated = false;
    for (javassist.bytecode.annotation.Annotation a : annotations.getAnnotations()) {
      if (annotationsToScan.contains(a.getTypeName())) {
        isAnnotated = true;
      }
    }
    if (isAnnotated) {
      List<AnnotationDescriptor> classAnnotations = getAnnotationDescriptors(annotations);
      @SuppressWarnings("unchecked")
      List<FieldInfo> classFields = classFile.getFields();
      List<FieldDescriptor> fieldDescriptors = new ArrayList<>(classFields.size());
      for (FieldInfo field : classFields) {
        String fieldName = field.getName();
        AnnotationsAttribute fieldAnnotations = ((AnnotationsAttribute)field.getAttribute(AnnotationsAttribute.visibleTag));
        final List<AnnotationDescriptor> annotationDescriptors =
            (fieldAnnotations != null) ? getAnnotationDescriptors(fieldAnnotations) : Collections.<AnnotationDescriptor>emptyList();
        fieldDescriptors.add(new FieldDescriptor(fieldName, field.getDescriptor(), annotationDescriptors));
      }
      functions.add(new AnnotatedClassDescriptor(classFile.getName(), classAnnotations, fieldDescriptors));
    }
  }
}
 
Example 4
Source File: AnnotationDB.java    From audit4j-core with Apache License 2.0 5 votes vote down vote up
protected void scanFields(ClassFile cf) {
    List<ClassFile> fields = cf.getFields();
    if (fields == null)
        return;
    for (Object obj : fields) {
        FieldInfo field = (FieldInfo) obj;
        AnnotationsAttribute visible = (AnnotationsAttribute) field.getAttribute(AnnotationsAttribute.visibleTag);
        AnnotationsAttribute invisible = (AnnotationsAttribute) field
                .getAttribute(AnnotationsAttribute.invisibleTag);
        if (visible != null)
            populate(visible.getAnnotations(), cf.getName());
        if (invisible != null)
            populate(invisible.getAnnotations(), cf.getName());
    }
}
 
Example 5
Source File: ClassScanner.java    From startup-os with Apache License 2.0 4 votes vote down vote up
private ImmutableList<Field> getPackageFields(String packageName) throws IOException {
  ImmutableList.Builder<Field> result = ImmutableList.builder();
  Set<String> classes = new HashSet<>();
  String resourceName = resourceName(packageName);
  Enumeration<URL> urls = ClassLoader.getSystemClassLoader().getResources(resourceName);
  while (urls.hasMoreElements()) {
    URL url = urls.nextElement();
    JarFile jarFile = getJarFile(url);
    Enumeration<? extends ZipEntry> entries = jarFile.entries();
    while (entries.hasMoreElements()) {
      ClassFile classFile = getClassFile(jarFile, entries.nextElement());
      if (classFile == null) {
        continue;
      }
      if (!classes.add(classFile.getName())) {
        // We've already gone through this class - skip
        continue;
      }

      for (Object fieldInfoObject : classFile.getFields()) {
        FieldInfo fieldInfo = (FieldInfo) fieldInfoObject;
        AnnotationsAttribute annotationsAttribute =
            (AnnotationsAttribute) fieldInfo.getAttribute(AnnotationsAttribute.visibleTag);
        if (annotationsAttribute != null) {
          for (Annotation annotation : annotationsAttribute.getAnnotations()) {
            try {
              if (FlagDesc.class.getName().equals(annotation.getTypeName())) {
                Class clazz = ClassLoader.getSystemClassLoader().loadClass(classFile.getName());
                Field field = clazz.getDeclaredField(fieldInfo.getName());
                if (Flag.class.isAssignableFrom(field.getType())) {
                  result.add(field);
                } else {
                  throw new IllegalArgumentException(
                      "Field annotated with FlagDesc does not inherit from Flag " + field);
                }
              }
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        }
      }
    }
  }
  return result.build();
}
 
Example 6
Source File: JavassistAdapter.java    From panda with Apache License 2.0 4 votes vote down vote up
@Override
public List<FieldInfo> getFields(ClassFile cls) {
    return cls.getFields();
}