2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
41
package org.netbeans.modules.j2ee.ejbverification;
43
import com.sun.source.tree.ClassTree;
44
import com.sun.source.tree.MethodTree;
45
import com.sun.source.tree.Tree;
46
import com.sun.source.tree.VariableTree;
47
import com.sun.source.util.SourcePositions;
48
import java.util.Collections;
49
import java.util.List;
50
import javax.lang.model.element.Element;
51
import org.netbeans.api.java.source.CompilationInfo;
52
import org.netbeans.spi.editor.hints.ErrorDescription;
53
import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
54
import org.netbeans.spi.editor.hints.Fix;
55
import org.netbeans.api.lexer.Token;
56
import org.netbeans.api.lexer.TokenSequence;
57
import org.netbeans.api.java.lexer.JavaTokenId;
58
import org.netbeans.spi.editor.hints.Severity;
62
* @author Tomasz.Slota@Sun.COM
64
public class HintsUtils {
65
public static ErrorDescription createProblem(Element subject, CompilationInfo cinfo,
67
return createProblem(subject, cinfo, description, Severity.ERROR, Collections.<Fix>emptyList());
70
public static ErrorDescription createProblem(Element subject, CompilationInfo cinfo,
71
String description, Severity severity){
72
return createProblem(subject, cinfo, description, severity, Collections.<Fix>emptyList());
75
public static ErrorDescription createProblem(Element subject, CompilationInfo cinfo, String description,
76
Severity severity, Fix fix){
77
return createProblem(subject, cinfo, description, severity, Collections.singletonList(fix));
80
public static ErrorDescription createProblem(Element subject, CompilationInfo cinfo, String description, Fix fix){
81
return createProblem(subject, cinfo, description, Severity.ERROR, Collections.singletonList(fix));
84
public static ErrorDescription createProblem(Element subject, CompilationInfo cinfo,
85
String description, Severity severity, List<Fix> fixes){
86
ErrorDescription err = null;
87
List<Fix> fixList = fixes == null ? Collections.<Fix>emptyList() : fixes;
89
// by default place error annotation on the element being checked
90
Tree elementTree = cinfo.getTrees().getTree(subject);
92
if (elementTree != null){
93
TextSpan underlineSpan = getUnderlineSpan(cinfo, elementTree);
95
err = ErrorDescriptionFactory.createErrorDescription(
96
severity, description, fixList, cinfo.getFileObject(),
97
underlineSpan.getStartOffset(), underlineSpan.getEndOffset());
107
* This method returns the part of the syntax tree to be highlighted.
108
* It will be usually the class/method/variable identifier.
110
public static TextSpan getUnderlineSpan(CompilationInfo info, Tree tree){
111
SourcePositions srcPos = info.getTrees().getSourcePositions();
113
int startOffset = (int) srcPos.getStartPosition(info.getCompilationUnit(), tree);
114
int endOffset = (int) srcPos.getEndPosition(info.getCompilationUnit(), tree);
116
Tree startSearchingForNameIndentifierBehindThisTree = null;
118
if (tree.getKind() == Tree.Kind.CLASS){
119
startSearchingForNameIndentifierBehindThisTree = ((ClassTree)tree).getModifiers();
121
} else if (tree.getKind() == Tree.Kind.METHOD){
122
startSearchingForNameIndentifierBehindThisTree = ((MethodTree)tree).getReturnType();
123
} else if (tree.getKind() == Tree.Kind.VARIABLE){
124
startSearchingForNameIndentifierBehindThisTree = ((VariableTree)tree).getType();
127
if (startSearchingForNameIndentifierBehindThisTree != null){
128
int searchStart = (int) srcPos.getEndPosition(info.getCompilationUnit(),
129
startSearchingForNameIndentifierBehindThisTree);
131
TokenSequence tokenSequence = info.getTreeUtilities().tokensFor(tree);
133
if (tokenSequence != null){
135
tokenSequence.move(searchStart);
138
eob = !tokenSequence.moveNext();
140
while (!eob && tokenSequence.token().id() != JavaTokenId.IDENTIFIER);
143
Token identifier = tokenSequence.token();
144
startOffset = identifier.offset(info.getTokenHierarchy());
145
endOffset = startOffset + identifier.length();
150
return new TextSpan(startOffset, endOffset);
154
* Represents a span of text
156
public static class TextSpan{
157
private int startOffset;
158
private int endOffset;
160
public TextSpan(int startOffset, int endOffset){
161
this.startOffset = startOffset;
162
this.endOffset = endOffset;
165
public int getStartOffset(){
169
public int getEndOffset(){