Java Code Examples for javassist.bytecode.annotation.Annotation#getMemberNames()

The following examples show how to use javassist.bytecode.annotation.Annotation#getMemberNames() . 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: JApiAnnotation.java    From japicmp with Apache License 2.0 6 votes vote down vote up
private Map<String, Optional<MemberValue>> buildMemberValueMap(Annotation annotation) {
	Map<String, Optional<MemberValue>> map = new HashMap<>();
	@SuppressWarnings("unchecked")
	Set<String> memberNames = annotation.getMemberNames();
	if (memberNames != null) {
		for (String memberName : memberNames) {
			MemberValue memberValue = annotation.getMemberValue(memberName);
			if (memberValue == null) {
				map.put(memberName, Optional.<MemberValue>absent());
			} else {
				map.put(memberName, Optional.of(memberValue));
			}
		}
	}
	return map;
}
 
Example 2
Source File: JavaAssistClass.java    From smart-testing with Apache License 2.0 5 votes vote down vote up
private void addAnnotations(Collection<String> imports, AnnotationsAttribute annotations) {
    if (annotations != null) {
        for (Annotation each : annotations.getAnnotations()) {
            imports.add(each.getTypeName());

            final Set<String> memberNames = each.getMemberNames();

            if (memberNames != null) {
                for (String memberName : memberNames) {
                    imports.addAll(addSubtypes(each, memberName));
                }
            }
        }
    }
}
 
Example 3
Source File: JavassistUtils.java    From geowave with Apache License 2.0 4 votes vote down vote up
/**
 * This function will take the given annotations attribute and create a new attribute, cloning all
 * the annotations and specified values within the attribute. The annotations attribute can then
 * be set on a method, class, or field.
 */
public static AnnotationsAttribute cloneAnnotationsAttribute(
    final ConstPool constPool,
    final AnnotationsAttribute attr,
    final ElementType validElementType) {

  // We can use system class loader here because the annotations for
  // Target
  // are part of the Java System.
  final ClassLoader cl = ClassLoader.getSystemClassLoader();

  final AnnotationsAttribute attrNew =
      new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);

  if (attr != null) {
    for (final Annotation annotation : attr.getAnnotations()) {
      final Annotation newAnnotation = new Annotation(annotation.getTypeName(), constPool);

      // If this must target a certain type of field, then ensure we
      // only
      // copy over annotations that can target that type of field.
      // For instances, a METHOD annotation can't be applied to a
      // FIELD or TYPE.
      Class<?> annoClass;
      try {
        annoClass = cl.loadClass(annotation.getTypeName());
        final Target target = annoClass.getAnnotation(Target.class);
        if ((target != null) && !Arrays.asList(target.value()).contains(validElementType)) {
          continue;
        }
      } catch (final ClassNotFoundException e) {
        // Cannot apply this annotation because its type cannot be
        // found.
        LOGGER.error("Cannot apply this annotation because it's type cannot be found", e);
        continue;
      }

      // Copy over the options for this annotation. For example:
      // @Parameter(names = "-blah")
      // For this, a member value would be "names" which would be a
      // StringMemberValue
      if (annotation.getMemberNames() != null) {
        for (final Object memberName : annotation.getMemberNames()) {
          final MemberValue memberValue = annotation.getMemberValue((String) memberName);
          if (memberValue != null) {
            newAnnotation.addMemberValue((String) memberName, memberValue);
          }
        }
      }
      attrNew.addAnnotation(newAnnotation);
    }
  }
  return attrNew;
}