use of javassist.bytecode.annotation.Annotation in project BroadleafCommerce by BroadleafCommerce.
the class DirectCopyClassTransformer method reviewDirectCopyTransformAnnotations.
/**
* Retrieves {@link DirectCopyTransformTypes} that are placed as annotations on classes.
* @param clazz
* @param mySkipOverlaps
* @param myRenameMethodOverlaps
* @param matchedPatterns
* @return
*/
protected XFormParams reviewDirectCopyTransformAnnotations(CtClass clazz, boolean mySkipOverlaps, boolean myRenameMethodOverlaps, List<DirectCopyIgnorePattern> matchedPatterns) {
List<?> attributes = clazz.getClassFile().getAttributes();
Iterator<?> itr = attributes.iterator();
List<String> templates = new ArrayList<>();
List<Boolean> skips = new ArrayList<>();
List<Boolean> renames = new ArrayList<>();
XFormParams response = new XFormParams();
check: {
while (itr.hasNext()) {
Object object = itr.next();
if (AnnotationsAttribute.class.isAssignableFrom(object.getClass())) {
AnnotationsAttribute attr = (AnnotationsAttribute) object;
Annotation[] items = attr.getAnnotations();
for (Annotation annotation : items) {
String typeName = annotation.getTypeName();
if (typeName.equals(DirectCopyTransform.class.getName())) {
ArrayMemberValue arrayMember = (ArrayMemberValue) annotation.getMemberValue("value");
for (MemberValue arrayMemberValue : arrayMember.getValue()) {
AnnotationMemberValue member = (AnnotationMemberValue) arrayMemberValue;
Annotation memberAnnot = member.getValue();
ArrayMemberValue annot = (ArrayMemberValue) memberAnnot.getMemberValue("templateTokens");
List<String> addedTemplates = new ArrayList<>();
for (MemberValue memberValue : annot.getValue()) {
String val = ((StringMemberValue) memberValue).getValue();
addedTemplates.addAll(reviewTemplateTokens(matchedPatterns, val));
}
templates.addAll(addedTemplates);
BooleanMemberValue skipAnnot = (BooleanMemberValue) memberAnnot.getMemberValue("skipOverlaps");
if (skipAnnot != null) {
for (int j = 0; j < addedTemplates.size(); j++) {
skips.add(skipAnnot.getValue());
}
} else {
for (int j = 0; j < addedTemplates.size(); j++) {
skips.add(mySkipOverlaps);
}
}
BooleanMemberValue renameAnnot = (BooleanMemberValue) memberAnnot.getMemberValue("renameMethodOverlaps");
if (renameAnnot != null) {
for (int j = 0; j < addedTemplates.size(); j++) {
renames.add(renameAnnot.getValue());
}
} else {
for (int j = 0; j < addedTemplates.size(); j++) {
renames.add(myRenameMethodOverlaps);
}
}
}
response.setXformVals(templates.toArray(new String[templates.size()]));
response.setXformSkipOverlaps(skips.toArray(new Boolean[skips.size()]));
response.setXformRenameMethodOverlaps(renames.toArray(new Boolean[renames.size()]));
break check;
}
}
}
}
}
return response;
}
use of javassist.bytecode.annotation.Annotation in project play-cookbook by spinscale.
the class XmlEnhancer method enhanceThisClass.
@Override
public void enhanceThisClass(ApplicationClass applicationClass) throws Exception {
CtClass ctClass = makeClass(applicationClass);
if (!ctClass.subtypeOf(classPool.get("play.db.jpa.JPABase"))) {
return;
}
if (!hasAnnotation(ctClass, "javax.persistence.Entity")) {
return;
}
ConstPool constpool = ctClass.getClassFile().getConstPool();
AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
if (!hasAnnotation(ctClass, "javax.xml.bind.annotation.XmlAccessorType")) {
Annotation annot = new Annotation("javax.xml.bind.annotation.XmlAccessorType", constpool);
EnumMemberValue enumValue = new EnumMemberValue(constpool);
enumValue.setType("javax.xml.bind.annotation.XmlAccessType");
enumValue.setValue("FIELD");
annot.addMemberValue("value", enumValue);
attr.addAnnotation(annot);
ctClass.getClassFile().addAttribute(attr);
}
if (!hasAnnotation(ctClass, "javax.xml.bind.annotation.XmlRootElement")) {
Annotation annot = new Annotation("javax.xml.bind.annotation.XmlRootElement", constpool);
String entityName = ctClass.getName();
String entity = entityName.substring(entityName.lastIndexOf('.') + 1).toLowerCase();
annot.addMemberValue("name", new StringMemberValue(entity, constpool));
attr.addAnnotation(annot);
ctClass.getClassFile().addAttribute(attr);
}
applicationClass.enhancedByteCode = ctClass.toBytecode();
ctClass.defrost();
}
use of javassist.bytecode.annotation.Annotation in project audit4j-core by audit4j.
the class AnnotationDB method populate.
protected void populate(Annotation[] annotations, String className) {
if (annotations == null)
return;
Set<String> classAnnotations = classIndex.get(className);
for (Annotation ann : annotations) {
Set<String> classes = annotationIndex.get(ann.getTypeName());
if (classes == null) {
classes = new HashSet<String>();
annotationIndex.put(ann.getTypeName(), classes);
}
classes.add(className);
classAnnotations.add(ann.getTypeName());
}
}
use of javassist.bytecode.annotation.Annotation in project coprhd-controller by CoprHD.
the class DbSchemaChanger method addAnnotation.
/**
* add an annotation to a method
*
* @param methodName the method to which the annotation to be added
* @param annotationName the annotation name, it should be a full name
* @param values the attributes of the annotation
*/
public DbSchemaChanger addAnnotation(String methodName, String annotationName, Map<String, Object> values) throws Exception {
// looking for the method to apply the annotation on
CtMethod methodDescriptor = cc.getDeclaredMethod(methodName);
// create the annotation
ClassFile ccFile = cc.getClassFile();
ccFile.setVersionToJava5();
ConstPool constpool = ccFile.getConstPool();
MethodInfo minfo = methodDescriptor.getMethodInfo();
AnnotationsAttribute attr = (AnnotationsAttribute) minfo.getAttribute(AnnotationsAttribute.visibleTag);
if (attr == null) {
attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
}
Annotation annot = new Annotation(annotationName, constpool);
Set<Map.Entry<String, Object>> entries = values.entrySet();
for (Map.Entry<String, Object> entry : entries) {
String attrName = entry.getKey();
Object attrValue = entry.getValue();
if (attrValue instanceof String) {
annot.addMemberValue(attrName, new StringMemberValue((String) attrValue, ccFile.getConstPool()));
} else {
throw new RuntimeException(String.format("Unsupported attribute type %s of %s", attrName, attrValue));
}
}
attr.addAnnotation(annot);
// add the annotation to the method descriptor
minfo.addAttribute(attr);
log.info("add {} to method {}", attr, methodDescriptor);
return this;
}
use of javassist.bytecode.annotation.Annotation in project hibernate-orm by hibernate.
the class FieldWriter method addAnnotations.
/* --- */
private static void addAnnotations(FieldInfo fieldInfo, Class<?>[] annotations) {
AnnotationsAttribute annotationsAttribute = (AnnotationsAttribute) fieldInfo.getAttribute(AnnotationsAttribute.visibleTag);
if (annotationsAttribute == null) {
annotationsAttribute = new AnnotationsAttribute(fieldInfo.getConstPool(), AnnotationsAttribute.visibleTag);
fieldInfo.addAttribute(annotationsAttribute);
}
for (Class<?> annotation : annotations) {
annotationsAttribute.addAnnotation(new Annotation(annotation.getName(), fieldInfo.getConstPool()));
}
}
Aggregations