KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > jpa > verification > common > Utilities


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 /**
37  *
38  * @author Tomasz.Slota@Sun.COM
39  */

40 public class Utilities {
41     public static AnnotationMirror findAnnotation(Element element, String JavaDoc 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     /**
52      * A convenience method, returns true if findAnnotation(...) != null
53      */

54     public static boolean hasAnnotation(Element element, String JavaDoc annClass){
55         AnnotationMirror annEntity = findAnnotation(element, annClass);
56         return annEntity != null;
57     }
58     
59     /**
60      * @return the value of annotation attribute, null if the attribute
61      * was not found or when ann was null
62      */

63     public static AnnotationValue getAnnotationAttrValue(AnnotationMirror ann, String JavaDoc 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     /**
76      * This method returns the part of the syntax tree to be highlighted.
77      * It will be usually the class/method/variable identifier.
78      */

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     /**
122      * Represents a span of text
123      */

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