2 * Copyright 2000-2016 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.jetbrains.jps.javac.ast;
18 import com.sun.source.tree.*;
19 import com.sun.source.util.TreeScanner;
20 import com.sun.tools.javac.code.Symbol;
21 import com.sun.tools.javac.code.Type;
22 import com.sun.tools.javac.tree.JCTree;
23 import org.jetbrains.jps.javac.ast.api.JavacDefSymbol;
24 import org.jetbrains.jps.javac.ast.api.JavacRefSymbol;
26 import javax.lang.model.element.ElementKind;
27 import javax.lang.model.type.TypeKind;
29 class JavacTreeRefScanner extends TreeScanner<Tree, JavacTreeScannerSink> {
31 public Tree visitCompilationUnit(CompilationUnitTree node, JavacTreeScannerSink sink) {
32 scan(node.getPackageAnnotations(), sink);
33 scan(node.getTypeDecls(), sink);
38 public Tree visitIdentifier(IdentifierTree node, JavacTreeScannerSink sink) {
39 final JCTree.JCIdent javacIdentifier = (JCTree.JCIdent)node;
40 final Type type = javacIdentifier.type;
44 if (type.getKind() == TypeKind.PACKAGE) {
47 final Symbol sym = javacIdentifier.sym;
48 if (sym.getKind() == ElementKind.PARAMETER ||
49 sym.getKind() == ElementKind.LOCAL_VARIABLE ||
50 sym.getKind() == ElementKind.FIELD ||
51 sym.getKind() == ElementKind.EXCEPTION_PARAMETER ||
52 sym.getKind() == ElementKind.TYPE_PARAMETER) {
55 sink.sinkReference(new JavacRefSymbol(sym, Tree.Kind.IDENTIFIER));
60 public Tree visitVariable(VariableTree node, JavacTreeScannerSink sink) {
61 final Symbol.VarSymbol sym = ((JCTree.JCVariableDecl)node).sym;
62 if (sym.getKind() == ElementKind.FIELD) {
63 sink.sinkReference(new JavacRefSymbol(sym, Tree.Kind.VARIABLE));
65 return super.visitVariable(node, sink);
69 public Tree visitMemberSelect(MemberSelectTree node, JavacTreeScannerSink sink) {
70 final Symbol sym = ((JCTree.JCFieldAccess)node).sym;
71 if (sym.getKind() != ElementKind.PACKAGE) {
72 sink.sinkReference(new JavacRefSymbol(sym, Tree.Kind.MEMBER_SELECT));
74 return super.visitMemberSelect(node, sink);
78 public Tree visitMethod(MethodTree node, JavacTreeScannerSink sink) {
79 final Symbol.MethodSymbol sym = ((JCTree.JCMethodDecl)node).sym;
80 sink.sinkReference(new JavacRefSymbol(sym, Tree.Kind.METHOD));
81 return super.visitMethod(node, sink);
86 public Tree visitClass(ClassTree node, JavacTreeScannerSink sink) {
87 JCTree.JCClassDecl classDecl = (JCTree.JCClassDecl)node;
88 Symbol.ClassSymbol sym = classDecl.sym;
89 sink.sinkReference(new JavacRefSymbol(sym, Tree.Kind.CLASS));
90 sink.sinkDeclaration(new JavacDefSymbol(sym, Tree.Kind.CLASS, classDecl.pos));
91 return super.visitClass(node, sink);
94 static JavacTreeRefScanner createASTScanner() {
96 Class aClass = Class.forName("org.jetbrains.jps.javac.ast.Javac8RefScanner");
97 return (JavacTreeRefScanner) aClass.newInstance();
99 catch (Throwable ignored) {
100 return new JavacTreeRefScanner();