001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.test.java.gui.parser;
043:
044: import com.sun.source.tree.CompilationUnitTree;
045: import com.sun.source.tree.Tree;
046: import com.sun.source.util.SourcePositions;
047: import com.sun.source.util.Trees;
048: import java.io.PrintStream;
049: import javax.lang.model.element.Element;
050: import javax.lang.model.element.ExecutableElement;
051: import javax.lang.model.element.PackageElement;
052: import javax.lang.model.element.TypeElement;
053: import javax.lang.model.element.TypeParameterElement;
054: import javax.lang.model.element.VariableElement;
055: import javax.lang.model.util.ElementScanner6;
056: import org.netbeans.api.java.source.CompilationController;
057:
058: /**
059: *
060: * @author Jiri Prox
061: */
062: public class ElementVisitor extends ElementScanner6<Void, PrintStream> {
063:
064: private CompilationController cc;
065:
066: private boolean canceled = false;
067:
068: private String text;
069:
070: void cancel() {
071: canceled = true;
072: }
073:
074: public ElementVisitor(CompilationController cc, String text) {
075: this .text = text;
076: this .cc = cc;
077: }
078:
079: @Override
080: public Void visitPackage(PackageElement arg0, PrintStream arg1) {
081: if (!canceled) {
082: arg1.print("Package Element: ");
083: arg1.print(arg0.getSimpleName());
084: long[] pos = getPosition(arg0);
085: arg1.println(" " + pos[0] + " " + pos[1]);
086: if (pos[1] != -1)
087: arg1
088: .println(text.substring((int) pos[0],
089: (int) pos[1]));
090: return super .visitPackage(arg0, arg1);
091: }
092: return null;
093: }
094:
095: @Override
096: public Void visitType(TypeElement arg0, PrintStream arg1) {
097: if (!canceled) {
098: arg1.print("Type Element: ");
099: arg1.print(arg0.getSimpleName());
100: long[] pos = getPosition(arg0);
101: arg1.println(" " + pos[0] + " " + pos[1]);
102: if (pos[1] != -1)
103: arg1
104: .println(text.substring((int) pos[0],
105: (int) pos[1]));
106: return super .visitType(arg0, arg1);
107: }
108: return null;
109:
110: }
111:
112: @Override
113: public Void visitVariable(VariableElement arg0, PrintStream arg1) {
114: if (!canceled) {
115: arg1.print("Variable Element: ");
116: arg1.print(arg0.getSimpleName());
117: long[] pos = getPosition(arg0);
118: arg1.println(" " + pos[0] + " " + pos[1]);
119: if (pos[1] != -1)
120: arg1
121: .println(text.substring((int) pos[0],
122: (int) pos[1]));
123: return super .visitVariable(arg0, arg1);
124: }
125: return null;
126: }
127:
128: @Override
129: public Void visitExecutable(ExecutableElement arg0, PrintStream arg1) {
130: if (!canceled) {
131: arg1.print("Executable Element: ");
132: arg1.print(arg0.getSimpleName());
133: long[] pos = getPosition(arg0);
134: arg1.println(" " + pos[0] + " " + pos[1]);
135: if (pos[1] != -1)
136: arg1
137: .println(text.substring((int) pos[0],
138: (int) pos[1]));
139: return super .visitExecutable(arg0, arg1);
140: }
141: return null;
142: }
143:
144: @Override
145: public Void visitTypeParameter(TypeParameterElement arg0,
146: PrintStream arg1) {
147: if (!canceled) {
148: arg1.print("TypeParameter Element: ");
149: arg1.print(arg0.getSimpleName());
150: long[] pos = getPosition(arg0);
151: arg1.println(" " + pos[0] + " " + pos[1]);
152: if (pos[1] != -1)
153: arg1
154: .println(text.substring((int) pos[0],
155: (int) pos[1]));
156: return super .visitTypeParameter(arg0, arg1);
157: }
158: return null;
159: }
160:
161: private long[] getPosition(Element e) {
162: Trees trees = cc.getTrees();
163: CompilationUnitTree cut = cc.getCompilationUnit();
164: Tree t = trees.getTree(e);
165: if (t == null) {
166: return new long[] { -1, -1 };
167: }
168: SourcePositions sourcePositions = trees.getSourcePositions();
169: return new long[] { sourcePositions.getStartPosition(cut, t),
170: sourcePositions.getEndPosition(cut, t) };
171: }
172:
173: }
|