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

The following examples show how to use javassist.bytecode.ClassFile#getAttribute() . 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: ClassFileArchiveEntryHandler.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private ClassDescriptor toClassDescriptor(ClassFile classFile, ArchiveEntry entry) {
	ClassDescriptor.Categorization categorization = ClassDescriptor.Categorization.OTHER;;

	final AnnotationsAttribute visibleAnnotations = (AnnotationsAttribute) classFile.getAttribute( AnnotationsAttribute.visibleTag );
	if ( visibleAnnotations != null ) {
		if ( visibleAnnotations.getAnnotation( Entity.class.getName() ) != null
				|| visibleAnnotations.getAnnotation( MappedSuperclass.class.getName() ) != null
				|| visibleAnnotations.getAnnotation( Embeddable.class.getName() ) != null ) {
			categorization = ClassDescriptor.Categorization.MODEL;
		}
		else if ( visibleAnnotations.getAnnotation( Converter.class.getName() ) != null ) {
			categorization = ClassDescriptor.Categorization.CONVERTER;
		}
	}

	return new ClassDescriptorImpl( classFile.getName(), categorization, entry.getStreamAccess() );
}
 
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 scanClass(ClassFile cf) {
    String className = cf.getName();
    AnnotationsAttribute visible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.visibleTag);
    AnnotationsAttribute invisible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.invisibleTag);
    if (visible != null)
        populate(visible.getAnnotations(), className);
    if (invisible != null)
        populate(invisible.getAnnotations(), className);
}
 
Example 5
Source File: CheckForConcept.java    From anno4j with Apache License 2.0 4 votes vote down vote up
protected boolean isAnnotationPresent(ClassFile cf) {
	// concept with an annotation
	AnnotationsAttribute attr = (AnnotationsAttribute) cf
			.getAttribute(AnnotationsAttribute.visibleTag);
	return isAnnotationPresent(attr);
}
 
Example 6
Source File: TransformationUtil.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
static
private void updatePropOrder(CtClass ctClass, String name){
	ClassFile classFile = ctClass.getClassFile();

	AnnotationsAttribute annotations = (AnnotationsAttribute)classFile.getAttribute(AnnotationsAttribute.visibleTag);

	Annotation xmlTypeAnnotation = annotations.getAnnotation("javax.xml.bind.annotation.XmlType");

	ArrayMemberValue propOrderValue = (ArrayMemberValue)xmlTypeAnnotation.getMemberValue("propOrder");

	removeValue(propOrderValue, name);

	annotations.addAnnotation(xmlTypeAnnotation);
}
 
Example 7
Source File: JavassistAnnotationsHelper.java    From jadira with Apache License 2.0 3 votes vote down vote up
public static Annotation[] getAnnotationsForClass(ClassFile classFile) {

		AnnotationsAttribute visible = (AnnotationsAttribute) classFile.getAttribute(AnnotationsAttribute.visibleTag);
		AnnotationsAttribute invisible = (AnnotationsAttribute) classFile.getAttribute(AnnotationsAttribute.invisibleTag);

		Set<Annotation> retVal = new HashSet<Annotation>();

		retVal.addAll(findAnnotationsForAnnotationsAttribute(visible));
		retVal.addAll(findAnnotationsForAnnotationsAttribute(invisible));

		return retVal.toArray(new Annotation[retVal.size()]);
	}