List of usage examples for org.eclipse.jdt.core.util IMethodInfo getAttributes
IClassFileAttribute[] getAttributes();
From source file:com.inepex.classtemplater.plugin.logic.Annotation.java
License:Eclipse Public License
public Annotation(IAnnotation jdtAnnotation, ICompilationUnit compilationUnit) throws Exception { this.compilationUnit = compilationUnit; name = jdtAnnotation.getElementName(); //default values aren't found in JDT so using AST to get them String[][] type = compilationUnit.findPrimaryType().resolveType(jdtAnnotation.getElementName()); if (type != null) { IType annType = jdtAnnotation.getJavaProject().findType(type[0][0] + "." + type[0][1]); //hint to read annotation default value from a classfile // if (annType.getCompilationUnit() != null) { AnnotationASTVisitor annASTVisitor = new AnnotationASTVisitor(); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource(annType.getCompilationUnit()); parser.setResolveBindings(true); CompilationUnit aParser = (CompilationUnit) parser.createAST(null); aParser.accept(annASTVisitor); Map<String, Object> defaultValues = annASTVisitor.getDefaultValueObjects(); for (Entry<String, Object> entry : defaultValues.entrySet()) { paramObjects.put(entry.getKey(), entry.getValue()); params.put(entry.getKey(), String.valueOf(entry.getValue())); }/*from w w w . j a v a2 s . c om*/ } else { //read annotation default value from .class file IClassFileReader reader = ToolFactory.createDefaultClassFileReader(annType.getClassFile(), IClassFileReader.ALL); if (reader != null) { for (IMethodInfo methodInfo : reader.getMethodInfos()) { if (methodInfo.getAttributes().length > 0 && methodInfo.getAttributes()[0] instanceof AnnotationDefaultAttribute) { String name = new String(methodInfo.getName()); Object value = parseDefaultObjectValueFromAnnotationDefaultAttribute( (AnnotationDefaultAttribute) (methodInfo.getAttributes()[0]), new String(methodInfo.getDescriptor())); if (value != null) { paramObjects.put(name, value); params.put(name, String.valueOf(value)); } } // System.out.println(methodInfo.getName()); } } } } for (IMemberValuePair pair : jdtAnnotation.getMemberValuePairs()) { try { params.put(pair.getMemberName(), String.valueOf(pair.getValue())); paramObjects.put(pair.getMemberName(), pair.getValue()); } catch (ClassCastException e) { Log.log("Could not cast value of annotation-attribute: " + name + ", " + pair.getMemberName() + ". \n" + "Only string values can be used for annotation-attribute"); } } }