~ubuntu-branches/ubuntu/trusty/netbeans/trusty

« back to all changes in this revision

Viewing changes to j2ee/ejbverification/src/org/netbeans/modules/j2ee/ejbverification/HintsUtils.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
package org.netbeans.modules.j2ee.ejbverification;
 
42
 
 
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;
 
59
 
 
60
/**
 
61
 *
 
62
 * @author Tomasz.Slota@Sun.COM
 
63
 */
 
64
public class HintsUtils {
 
65
    public static ErrorDescription createProblem(Element subject, CompilationInfo cinfo,
 
66
            String description){
 
67
        return createProblem(subject, cinfo, description, Severity.ERROR, Collections.<Fix>emptyList());
 
68
    }
 
69
    
 
70
    public static ErrorDescription createProblem(Element subject, CompilationInfo cinfo,
 
71
            String description, Severity severity){
 
72
        return createProblem(subject, cinfo, description, severity, Collections.<Fix>emptyList());
 
73
    }
 
74
    
 
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));
 
78
    }
 
79
    
 
80
    public static ErrorDescription createProblem(Element subject, CompilationInfo cinfo, String description, Fix fix){
 
81
        return createProblem(subject, cinfo, description, Severity.ERROR, Collections.singletonList(fix));
 
82
    }
 
83
    
 
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;
 
88
        
 
89
        // by default place error annotation on the element being checked
 
90
        Tree elementTree = cinfo.getTrees().getTree(subject);
 
91
        
 
92
        if (elementTree != null){
 
93
            TextSpan underlineSpan = getUnderlineSpan(cinfo, elementTree);
 
94
            
 
95
            err = ErrorDescriptionFactory.createErrorDescription(
 
96
                    severity, description, fixList, cinfo.getFileObject(),
 
97
                    underlineSpan.getStartOffset(), underlineSpan.getEndOffset());
 
98
            
 
99
        } else{
 
100
            // report problem
 
101
        }
 
102
        
 
103
        return err;
 
104
    }
 
105
    
 
106
    /**
 
107
     * This method returns the part of the syntax tree to be highlighted.
 
108
     * It will be usually the class/method/variable identifier.
 
109
     */
 
110
    public static TextSpan getUnderlineSpan(CompilationInfo info, Tree tree){
 
111
        SourcePositions srcPos = info.getTrees().getSourcePositions();
 
112
        
 
113
        int startOffset = (int) srcPos.getStartPosition(info.getCompilationUnit(), tree);
 
114
        int endOffset = (int) srcPos.getEndPosition(info.getCompilationUnit(), tree);
 
115
        
 
116
        Tree startSearchingForNameIndentifierBehindThisTree = null;
 
117
        
 
118
        if (tree.getKind() == Tree.Kind.CLASS){
 
119
            startSearchingForNameIndentifierBehindThisTree = ((ClassTree)tree).getModifiers();
 
120
            
 
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();
 
125
        }
 
126
        
 
127
        if (startSearchingForNameIndentifierBehindThisTree != null){
 
128
            int searchStart = (int) srcPos.getEndPosition(info.getCompilationUnit(),
 
129
                    startSearchingForNameIndentifierBehindThisTree);
 
130
            
 
131
            TokenSequence tokenSequence = info.getTreeUtilities().tokensFor(tree);
 
132
            
 
133
            if (tokenSequence != null){
 
134
                boolean eob = false;
 
135
                tokenSequence.move(searchStart);
 
136
                
 
137
                do{
 
138
                    eob = !tokenSequence.moveNext();
 
139
                }
 
140
                while (!eob && tokenSequence.token().id() != JavaTokenId.IDENTIFIER);
 
141
                
 
142
                if (!eob){
 
143
                    Token identifier = tokenSequence.token();
 
144
                    startOffset = identifier.offset(info.getTokenHierarchy());
 
145
                    endOffset = startOffset + identifier.length();
 
146
                }
 
147
            }
 
148
        }
 
149
        
 
150
        return new TextSpan(startOffset, endOffset);
 
151
    }
 
152
    
 
153
    /**
 
154
     * Represents a span of text
 
155
     */
 
156
    public static class TextSpan{
 
157
        private int startOffset;
 
158
        private int endOffset;
 
159
        
 
160
        public TextSpan(int startOffset, int endOffset){
 
161
            this.startOffset = startOffset;
 
162
            this.endOffset = endOffset;
 
163
        }
 
164
        
 
165
        public int getStartOffset(){
 
166
            return startOffset;
 
167
        }
 
168
        
 
169
        public int getEndOffset(){
 
170
            return endOffset;
 
171
        }
 
172
    }
 
173
}