Java Code Examples for javassist.bytecode.AnnotationsAttribute#setAnnotation()

The following examples show how to use javassist.bytecode.AnnotationsAttribute#setAnnotation() . 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: CtMethodBuilder.java    From japicmp with Apache License 2.0 6 votes vote down vote up
public CtMethod addToClass(CtClass declaringClass) throws CannotCompileException {
	if (this.returnType == null) {
		this.returnType = declaringClass;
	}
	CtMethod ctMethod = CtNewMethod.make(this.modifier, this.returnType, this.name, this.parameters, this.exceptions, this.body, declaringClass);
	ctMethod.setModifiers(this.modifier);
	declaringClass.addMethod(ctMethod);
	for (String annotation : annotations) {
		ClassFile classFile = declaringClass.getClassFile();
		ConstPool constPool = classFile.getConstPool();
		AnnotationsAttribute attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
		Annotation annot = new Annotation(annotation, constPool);
		attr.setAnnotation(annot);
		ctMethod.getMethodInfo().addAttribute(attr);
	}
	return ctMethod;
}
 
Example 2
Source File: CtClassBuilder.java    From japicmp with Apache License 2.0 6 votes vote down vote up
public CtClass addToClassPool(ClassPool classPool) {
	CtClass ctClass;
	if (this.superclass.isPresent()) {
		ctClass = classPool.makeClass(this.name, this.superclass.get());
	} else {
		ctClass = classPool.makeClass(this.name);
	}
	ctClass.setModifiers(this.modifier);
	for (String annotation : annotations) {
		ClassFile classFile = ctClass.getClassFile();
		ConstPool constPool = classFile.getConstPool();
		AnnotationsAttribute attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
		Annotation annot = new Annotation(annotation, constPool);
		attr.setAnnotation(annot);
		ctClass.getClassFile2().addAttribute(attr);
	}
	for (CtClass interfaceCtClass : interfaces) {
		ctClass.addInterface(interfaceCtClass);
	}
	return ctClass;
}
 
Example 3
Source File: CtMethodBuilder.java    From japicmp with Apache License 2.0 6 votes vote down vote up
public CtMethod addToClass(CtClass declaringClass) throws CannotCompileException {
	if (this.returnType == null) {
		this.returnType = declaringClass;
	}
	CtMethod ctMethod = CtNewMethod.make(this.modifier, this.returnType, this.name, this.parameters, this.exceptions, this.body, declaringClass);
	ctMethod.setModifiers(this.modifier);
	declaringClass.addMethod(ctMethod);
	for (String annotation : annotations) {
		ClassFile classFile = declaringClass.getClassFile();
		ConstPool constPool = classFile.getConstPool();
		AnnotationsAttribute attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
		Annotation annot = new Annotation(annotation, constPool);
		attr.setAnnotation(annot);
		ctMethod.getMethodInfo().addAttribute(attr);
	}
	return ctMethod;
}
 
Example 4
Source File: CtClassBuilder.java    From japicmp with Apache License 2.0 6 votes vote down vote up
public CtClass addToClassPool(ClassPool classPool) {
	CtClass ctClass;
	if (this.superclass.isPresent()) {
		ctClass = classPool.makeClass(this.name, this.superclass.get());
	} else {
		ctClass = classPool.makeClass(this.name);
	}
	ctClass.setModifiers(this.modifier);
	for (String annotation : annotations) {
		ClassFile classFile = ctClass.getClassFile();
		ConstPool constPool = classFile.getConstPool();
		AnnotationsAttribute attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
		Annotation annot = new Annotation(annotation, constPool);
		attr.setAnnotation(annot);
		ctClass.getClassFile2().addAttribute(attr);
	}
	for (CtClass interfaceCtClass : interfaces) {
		ctClass.addInterface(interfaceCtClass);
	}
	return ctClass;
}
 
Example 5
Source File: JCommanderTranslationMap.java    From geowave with Apache License 2.0 5 votes vote down vote up
/**
 * Iterate the annotations, look for a 'required' parameter, and set it to false.
 */
private void disableBooleanMember(final String booleanMemberName, final CtField field) {

  // This is the JCommander package name
  final String packageName = JCommander.class.getPackage().getName();

  final AnnotationsAttribute fieldAttributes =
      (AnnotationsAttribute) field.getFieldInfo().getAttribute(AnnotationsAttribute.visibleTag);

  // Look for annotations that have a 'names' attribute, and whose package
  // starts with the expected JCommander package.
  for (final Annotation annotation : fieldAttributes.getAnnotations()) {
    if (annotation.getTypeName().startsWith(packageName)) {
      // See if it has a 'names' member variable.
      final MemberValue requiredMember = annotation.getMemberValue(booleanMemberName);

      // We have a names member!!!
      if (requiredMember != null) {
        final BooleanMemberValue booleanRequiredMember = (BooleanMemberValue) requiredMember;

        // Set it to not required.
        booleanRequiredMember.setValue(false);

        // This is KEY! For some reason, the existing annotation
        // will not be modified unless
        // you call 'setAnnotation' here. I'm guessing
        // 'getAnnotation()' creates a copy.
        fieldAttributes.setAnnotation(annotation);

        // Finished processing names.
        break;
      }
    }
  }
}
 
Example 6
Source File: CtFieldBuilder.java    From japicmp with Apache License 2.0 5 votes vote down vote up
public CtField addToClass(CtClass ctClass) throws CannotCompileException {
	CtField ctField = new CtField(this.type, this.name, ctClass);
	ctField.setModifiers(this.modifier);
	if (constantValue != null) {
		if (constantValue instanceof Boolean) {
			ctClass.addField(ctField, CtField.Initializer.constant((Boolean) constantValue));
		} else if (constantValue instanceof Integer) {
			ctClass.addField(ctField, CtField.Initializer.constant((Integer) constantValue));
		} else if (constantValue instanceof Long) {
			ctClass.addField(ctField, CtField.Initializer.constant((Long) constantValue));
		} else if (constantValue instanceof String) {
			ctClass.addField(ctField, CtField.Initializer.constant((String) constantValue));
		} else {
			throw new IllegalArgumentException("Provided constant value for field is of unsupported type: " + constantValue.getClass().getName());
		}
	} else {
		ctClass.addField(ctField);
	}
	for (String annotation : annotations) {
		ClassFile classFile = ctClass.getClassFile();
		ConstPool constPool = classFile.getConstPool();
		AnnotationsAttribute attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
		Annotation annot = new Annotation(annotation, constPool);
		attr.setAnnotation(annot);
		ctField.getFieldInfo().addAttribute(attr);
	}
	return ctField;
}
 
Example 7
Source File: CtFieldBuilder.java    From japicmp with Apache License 2.0 5 votes vote down vote up
public CtField addToClass(CtClass ctClass) throws CannotCompileException {
	CtField ctField = new CtField(this.type, this.name, ctClass);
	ctField.setModifiers(this.modifier);
	if (constantValue != null) {
		if (constantValue instanceof Boolean) {
			ctClass.addField(ctField, CtField.Initializer.constant((Boolean) constantValue));
		} else if (constantValue instanceof Integer) {
			ctClass.addField(ctField, CtField.Initializer.constant((Integer) constantValue));
		} else if (constantValue instanceof Long) {
			ctClass.addField(ctField, CtField.Initializer.constant((Long) constantValue));
		} else if (constantValue instanceof String) {
			ctClass.addField(ctField, CtField.Initializer.constant((String) constantValue));
		} else {
			throw new IllegalArgumentException("Provided constant value for field is of unsupported type: " + constantValue.getClass().getName());
		}
	} else {
		ctClass.addField(ctField);
	}
	for (String annotation : annotations) {
		ClassFile classFile = ctClass.getClassFile();
		ConstPool constPool = classFile.getConstPool();
		AnnotationsAttribute attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
		Annotation annot = new Annotation(annotation, constPool);
		attr.setAnnotation(annot);
		ctField.getFieldInfo().addAttribute(attr);
	}
	return ctField;
}
 
Example 8
Source File: DefaultRepositoryGenerator.java    From business with Mozilla Public License 2.0 4 votes vote down vote up
private AnnotationsAttribute createQualifierAttribute(ConstPool constPool,
        java.lang.annotation.Annotation qualifier) throws NotFoundException {
    AnnotationsAttribute attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
    attr.setAnnotation(copyAnnotation(constPool, qualifier));
    return attr;
}
 
Example 9
Source File: DefaultRepositoryGenerator.java    From business with Mozilla Public License 2.0 4 votes vote down vote up
private void addInjectAnnotation(ConstPool constPool, CtConstructor cc) {
    AnnotationsAttribute attribute = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
    attribute.setAnnotation(createAnnotation(constPool, Inject.class));
    cc.getMethodInfo().addAttribute(attribute);
}
 
Example 10
Source File: JCommanderTranslationMap.java    From geowave with Apache License 2.0 4 votes vote down vote up
/**
 * Iterate the annotations, look for a 'names' parameter, and override it to prepend the given
 * prefix.
 */
private void overrideParameterPrefixes(final CtField field, final String[] names) {

  // This is the JCommander package name
  final String packageName = JCommander.class.getPackage().getName();

  final AnnotationsAttribute fieldAttributes =
      (AnnotationsAttribute) field.getFieldInfo().getAttribute(AnnotationsAttribute.visibleTag);

  // Look for annotations that have a 'names' attribute, and whose package
  // starts with the expected JCommander package.
  for (final Annotation annotation : fieldAttributes.getAnnotations()) {
    if (annotation.getTypeName().startsWith(packageName)) {
      // See if it has a 'names' member variable.
      final MemberValue namesMember = annotation.getMemberValue(NAMES_MEMBER);

      // We have a names member!!!
      if (namesMember != null) {
        final ArrayMemberValue arrayNamesMember = (ArrayMemberValue) namesMember;

        // Iterate and transform each item in 'names()' list and
        // transform it.
        final MemberValue[] newMemberValues = new MemberValue[names.length];
        for (int i = 0; i < names.length; i++) {
          newMemberValues[i] =
              new StringMemberValue(names[i], field.getFieldInfo2().getConstPool());
        }

        // Override the member values in nameMember with the new
        // one's we've generated
        arrayNamesMember.setValue(newMemberValues);

        // This is KEY! For some reason, the existing annotation
        // will not be modified unless
        // you call 'setAnnotation' here. I'm guessing
        // 'getAnnotation()' creates a copy.
        fieldAttributes.setAnnotation(annotation);

        // Finished processing names.
        break;
      }
    }
  }
}