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

The following examples show how to use javassist.bytecode.AnnotationsAttribute#getAnnotation() . 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: SwaggerMoreDoclet.java    From swagger-more with Apache License 2.0 6 votes vote down vote up
private static void annotateApiAnn(ApiInfo info, AnnotationsAttribute attr, ConstPool constPool) {
    Annotation apiAnn = attr.getAnnotation(Api.class.getTypeName());
    MemberValue value;
    if (isNull(apiAnn)) {
        apiAnn = new Annotation(Api.class.getName(), constPool);
    }
    if (isNull(value = apiAnn.getMemberValue(ApiInfo.HIDDEN)) || !((BooleanMemberValue) value).getValue()) {
        apiAnn.addMemberValue(ApiInfo.HIDDEN, new BooleanMemberValue(info.hidden(), constPool));
    }
    ArrayMemberValue arrayMemberValue = (ArrayMemberValue) apiAnn.getMemberValue(TAGS);
    if (isNull(arrayMemberValue)) {
        arrayMemberValue = new ArrayMemberValue(constPool);
        arrayMemberValue.setValue(new MemberValue[1]);
    }
    StringMemberValue tagMemberValue = (StringMemberValue) arrayMemberValue.getValue()[0];
    if (isNull(tagMemberValue) || StringUtils.isEmpty(tagMemberValue.getValue())) {
        tagMemberValue = new StringMemberValue(info.tag(), constPool);
    }
    tagMemberValue.setValue(info.tag());
    arrayMemberValue.getValue()[0] = tagMemberValue;
    apiAnn.addMemberValue(TAGS, arrayMemberValue);
    attr.addAnnotation(apiAnn);
}
 
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 rapidoid with Apache License 2.0 6 votes vote down vote up
private static boolean isAnnotated(ClassFile cfile, Class<? extends Annotation>[] annotated) {
	List attributes = U.safe(cfile.getAttributes());

	for (Object attribute : attributes) {
		if (attribute instanceof AnnotationsAttribute) {
			AnnotationsAttribute annotations = (AnnotationsAttribute) attribute;

			for (Class<? extends Annotation> ann : annotated) {
				if (annotations.getAnnotation(ann.getName()) != null) {
					return true;
				}
			}
		}
	}

	return false;
}
 
Example 4
Source File: SwaggerMoreDoclet.java    From swagger-more with Apache License 2.0 5 votes vote down vote up
private static void annotateApiMethodAnn(ApiMethodInfo methodInfo, AnnotationsAttribute attr, ConstPool constPool) {
    Annotation apiMethodAnn = attr.getAnnotation(ApiMethod.class.getTypeName());
    MemberValue value;
    if (isNull(apiMethodAnn)) {
        apiMethodAnn = new Annotation(ApiMethod.class.getName(), constPool);
    }
    if (isNull(value = apiMethodAnn.getMemberValue(ApiMethodInfo.VALUE)) || StringUtils.isEmpty(((StringMemberValue) value).getValue())) {
        apiMethodAnn.addMemberValue(ApiMethodInfo.VALUE, new StringMemberValue(methodInfo.value(), constPool));
    }
    if (isNull(value = apiMethodAnn.getMemberValue(HIDDEN)) || !((BooleanMemberValue) value).getValue()) {
        apiMethodAnn.addMemberValue(HIDDEN, new BooleanMemberValue(methodInfo.hidden(), constPool));
    }
    if (isNull(value = apiMethodAnn.getMemberValue(NOTES)) || StringUtils.isEmpty(((StringMemberValue) value).getValue())) {
        apiMethodAnn.addMemberValue(NOTES, new StringMemberValue(methodInfo.notes(), constPool));
    }
    ArrayMemberValue arrayMemberValue = (ArrayMemberValue) apiMethodAnn.getMemberValue("params");
    if (isNull(arrayMemberValue)) {
        arrayMemberValue = new ArrayMemberValue(constPool);
        arrayMemberValue.setValue(new MemberValue[methodInfo.parameterCount()]);
    }
    AnnotationMemberValue annotationMemberValue;
    for (int i = 0; i < methodInfo.parameterCount(); i++) {
        if (isNull(annotationMemberValue = (AnnotationMemberValue) arrayMemberValue.getValue()[i])) {
            annotationMemberValue = new AnnotationMemberValue(new Annotation(ApiParam.class.getName(), constPool), constPool);
        }
        Annotation apiParamAnn = annotationMemberValue.getValue();
        if (isNull(value = apiParamAnn.getMemberValue(NAME)) || StringUtils.isEmpty(((StringMemberValue) value).getValue())) {
            apiParamAnn.addMemberValue(NAME, new StringMemberValue(methodInfo.param(i).name(), constPool));
        }
        if (isNull(value = apiParamAnn.getMemberValue(ApiMethodInfo.VALUE)) || StringUtils.isEmpty(((StringMemberValue) value).getValue())) {
            apiParamAnn.addMemberValue(ApiMethodInfo.VALUE, new StringMemberValue(methodInfo.param(i).value(), constPool));
        }
        arrayMemberValue.getValue()[i] = annotationMemberValue;

    }
    apiMethodAnn.addMemberValue(PARAMS, arrayMemberValue);
    attr.addAnnotation(apiMethodAnn);
}
 
Example 5
Source File: CallbacksUtil.java    From aion-germany with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Checks if annotation is present on method
 *
 * @param method
 *            Method to check
 * @param annotation
 *            Annotation to look for
 * @return result
 */
public static boolean isAnnotationPresent(CtMethod method,
		Class<? extends java.lang.annotation.Annotation> annotation) {
	for (Object o : method.getMethodInfo().getAttributes()) {
		if (o instanceof AnnotationsAttribute) {
			AnnotationsAttribute attribute = (AnnotationsAttribute) o;
			if (attribute.getAnnotation(annotation.getName()) != null) {
				return true;
			}
		}
	}

	return false;
}
 
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);
}