1 19 20 package org.netbeans.modules.j2ee.jpa.verification.common; 21 22 import com.sun.source.tree.ClassTree; 23 import com.sun.source.tree.MethodTree; 24 import com.sun.source.tree.Tree; 25 import com.sun.source.tree.VariableTree; 26 import com.sun.source.util.SourcePositions; 27 import javax.lang.model.element.AnnotationMirror; 28 import javax.lang.model.element.AnnotationValue; 29 import javax.lang.model.element.Element; 30 import javax.lang.model.element.ExecutableElement; 31 import org.netbeans.api.java.lexer.JavaTokenId; 32 import org.netbeans.api.java.source.CompilationInfo; 33 import org.netbeans.api.lexer.Token; 34 import org.netbeans.api.lexer.TokenSequence; 35 36 40 public class Utilities { 41 public static AnnotationMirror findAnnotation(Element element, String annotationClass){ 42 for (AnnotationMirror ann : element.getAnnotationMirrors()){ 43 if (annotationClass.equals(ann.getAnnotationType().toString())){ 44 return ann; 45 } 46 } 47 48 return null; 49 } 50 51 54 public static boolean hasAnnotation(Element element, String annClass){ 55 AnnotationMirror annEntity = findAnnotation(element, annClass); 56 return annEntity != null; 57 } 58 59 63 public static AnnotationValue getAnnotationAttrValue(AnnotationMirror ann, String attrName){ 64 if (ann != null){ 65 for (ExecutableElement attr : ann.getElementValues().keySet()){ 66 if (attrName.equals(attr.getSimpleName().toString())){ 67 return ann.getElementValues().get(attr); 68 } 69 } 70 } 71 72 return null; 73 } 74 75 79 public static TextSpan getUnderlineSpan(CompilationInfo info, Tree tree){ 80 SourcePositions srcPos = info.getTrees().getSourcePositions(); 81 82 int startOffset = (int) srcPos.getStartPosition(info.getCompilationUnit(), tree); 83 int endOffset = (int) srcPos.getEndPosition(info.getCompilationUnit(), tree); 84 Tree modifiersTree = null; 85 86 if (tree.getKind() == Tree.Kind.CLASS){ 87 modifiersTree = ((ClassTree)tree).getModifiers(); 88 } else 89 if (tree.getKind() == Tree.Kind.VARIABLE){ 90 modifiersTree = ((VariableTree)tree).getModifiers(); 91 } else 92 if (tree.getKind() == Tree.Kind.METHOD){ 93 modifiersTree = ((MethodTree)tree).getModifiers(); 94 } 95 96 97 if (modifiersTree != null){ 98 int searchStart = (int) srcPos.getEndPosition(info.getCompilationUnit(), modifiersTree); 99 100 TokenSequence tokenSequence = info.getTreeUtilities().tokensFor(tree); 101 102 if (tokenSequence != null){ 103 boolean eob = false; 104 tokenSequence.move(searchStart); 105 106 do{ 107 eob = !tokenSequence.moveNext(); 108 } 109 while (!eob && tokenSequence.token().id() != JavaTokenId.IDENTIFIER); 110 111 if (!eob){ 112 Token identifier = tokenSequence.token(); 113 startOffset = identifier.offset(info.getTokenHierarchy()); 114 endOffset = startOffset + identifier.length(); 115 } 116 } 117 } 118 return new TextSpan(startOffset, endOffset); 119 } 120 121 124 public static class TextSpan{ 125 private int startOffset; 126 private int endOffset; 127 128 public TextSpan(int startOffset, int endOffset){ 129 this.startOffset = startOffset; 130 this.endOffset = endOffset; 131 } 132 133 public int getStartOffset(){ 134 return startOffset; 135 } 136 137 public int getEndOffset(){ 138 return endOffset; 139 } 140 } 141 } 142 | Popular Tags |