1 22 package org.jboss.aop.annotation.compiler; 23 24 import javassist.ClassPool; 25 import javassist.CtClass; 26 import javassist.NotFoundException; 27 import javassist.bytecode.ConstPool; 28 import javassist.bytecode.annotation.AnnotationMemberValue; 29 import javassist.bytecode.annotation.ArrayMemberValue; 30 import javassist.bytecode.annotation.CharMemberValue; 31 import javassist.bytecode.annotation.MemberValue; 32 import javassist.bytecode.annotation.StringMemberValue; 33 34 import org.jboss.aop.annotation.factory.duplicate.ast.ASTAnnotation; 35 import org.jboss.aop.annotation.factory.duplicate.ast.ASTChar; 36 import org.jboss.aop.annotation.factory.duplicate.ast.ASTIdentifier; 37 import org.jboss.aop.annotation.factory.duplicate.ast.ASTMemberValueArrayInitializer; 38 import org.jboss.aop.annotation.factory.duplicate.ast.ASTMemberValuePair; 39 import org.jboss.aop.annotation.factory.duplicate.ast.ASTMemberValuePairs; 40 import org.jboss.aop.annotation.factory.duplicate.ast.ASTSingleMemberValue; 41 import org.jboss.aop.annotation.factory.duplicate.ast.ASTStart; 42 import org.jboss.aop.annotation.factory.duplicate.ast.ASTString; 43 import org.jboss.aop.annotation.factory.duplicate.ast.AnnotationParserVisitor; 44 import org.jboss.aop.annotation.factory.duplicate.ast.SimpleNode; 45 46 import java.util.Iterator ; 47 import java.util.Set ; 48 49 55 public class AnnotationInfoCreator implements AnnotationParserVisitor 56 { 57 private ClassPool pool; 58 private ConstPool cp; 59 private javassist.bytecode.annotation.Annotation base; 60 61 public AnnotationInfoCreator(ClassPool pool, ConstPool cp, javassist.bytecode.annotation.Annotation base) 62 { 63 this.pool = pool; 64 this.cp = cp; 65 this.base = base; 66 } 67 68 public Object visit(ASTMemberValuePairs node, Object data) 69 { 70 node.childrenAccept(this, data); 71 return null; 72 } 73 74 public Object visit(ASTMemberValuePair node, Object data) 75 { 76 String name = node.getIdentifier().getValue(); 77 MemberValue mv = base.getMemberValue(name); 78 if (mv == null) 79 { 80 throw new RuntimeException ("member value is null for created annotation: " + base.getTypeName() + " for name: " + name); 81 } 82 node.getValue().jjtAccept(this, mv); 83 return data; 84 } 85 86 public Object visit(ASTSingleMemberValue node, Object data) 87 { 88 if (base.getMemberNames().size() > 1) throw new RuntimeException ("single value expected from annotation: " + base.getTypeName()); 89 90 Set set = base.getMemberNames(); 91 MemberValue mv = base.getMemberValue((String ) set.iterator().next()); 92 node.getValue().jjtAccept(this, mv); 93 return data; 94 } 95 96 public Object visit(ASTIdentifier node, Object data) 97 { 98 MemberValue mv = (MemberValue) data; 99 MemberValuePopulate mvc = new MemberValuePopulate(node.getValue()); 100 mv.accept(mvc); 101 return null; 102 } 103 104 public Object visit(ASTString node, Object data) 105 { 106 StringMemberValue mv = (StringMemberValue) data; 107 mv.setValue(node.getValue()); 108 return null; 109 } 110 111 public Object visit(ASTChar node, Object data) 112 { 113 CharMemberValue mv = (CharMemberValue) data; 114 mv.setValue(node.getValue()); 115 return null; 116 } 117 118 119 public Object visit(ASTMemberValueArrayInitializer node, Object data) 120 { 121 ArrayMemberValue mv = (ArrayMemberValue) data; 122 int size = node.jjtGetNumChildren(); 123 MemberValue[] elements = new MemberValue[size]; 124 MemberValueCreation mvc = new MemberValueCreation(cp); 125 for (int i = 0; i < size; i++) 126 { 127 mv.getType().accept(mvc); 128 elements[i] = mvc.value; 129 node.jjtGetChild(i).jjtAccept(this, elements[i]); 130 } 131 mv.setValue(elements); 132 return null; 133 } 134 135 public Object visit(ASTAnnotation node, Object data) 136 { 137 try 138 { 139 CtClass annotation = pool.get(node.getIdentifier()); 140 javassist.bytecode.annotation.Annotation info = new javassist.bytecode.annotation.Annotation(cp, annotation); 141 AnnotationInfoCreator creator = new AnnotationInfoCreator(pool, cp, info); 142 node.childrenAccept(creator, data); 143 AnnotationMemberValue mv = (AnnotationMemberValue) data; 144 mv.setValue(info); 145 } 146 catch (NotFoundException e) 147 { 148 throw new RuntimeException (e); } 150 return null; 151 } 152 153 155 public Object visit(SimpleNode node, Object data) 156 { 157 return null; 158 } 159 160 public Object visit(ASTStart node, Object data) 161 { 162 return null; 163 } 164 165 public static javassist.bytecode.annotation.Annotation createAnnotationInfo(ClassPool pool, ConstPool cp, ASTAnnotation node) throws Exception 166 { 167 CtClass annotation = pool.get(node.getIdentifier()); 168 javassist.bytecode.annotation.Annotation info = new javassist.bytecode.annotation.Annotation(cp, annotation); 169 AnnotationInfoCreator creator = new AnnotationInfoCreator(pool, cp, info); 170 if (node.jjtGetNumChildren() > 0) 171 { 172 node.jjtGetChild(0).jjtAccept(creator, null); 173 } 174 return info; 175 } 176 177 } 178 | Popular Tags |