do not add private members references to index + add functional expressions to index
[idea/community.git] / jps / jps-builders / src / org / jetbrains / jps / javac / ast / JavacTreeRefScanner.java
1 /*
2  * Copyright 2000-2016 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.jetbrains.jps.javac.ast;
17
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;
25
26 import javax.lang.model.element.ElementKind;
27 import javax.lang.model.type.TypeKind;
28
29 class JavacTreeRefScanner extends TreeScanner<Tree, JavacTreeScannerSink> {
30   @Override
31   public Tree visitCompilationUnit(CompilationUnitTree node, JavacTreeScannerSink sink) {
32     scan(node.getPackageAnnotations(), sink);
33     scan(node.getTypeDecls(), sink);
34     return node;
35   }
36
37   @Override
38   public Tree visitIdentifier(IdentifierTree node, JavacTreeScannerSink sink) {
39     final JCTree.JCIdent javacIdentifier = (JCTree.JCIdent)node;
40     final Type type = javacIdentifier.type;
41     if (type == null) {
42       return null;
43     }
44     if (type.getKind() == TypeKind.PACKAGE) {
45       return null;
46     }
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) {
53       return null;
54     }
55     sink.sinkReference(new JavacRefSymbol(sym, Tree.Kind.IDENTIFIER));
56     return null;
57   }
58
59   @Override
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));
64     }
65     return super.visitVariable(node, sink);
66   }
67
68   @Override
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));
73     }
74     return super.visitMemberSelect(node, sink);
75   }
76
77   @Override
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);
82   }
83   
84   
85   @Override
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);
92   }
93
94   static JavacTreeRefScanner createASTScanner() {
95     try {
96       Class aClass = Class.forName("org.jetbrains.jps.javac.ast.Javac8RefScanner");
97       return (JavacTreeRefScanner) aClass.newInstance();
98     }
99     catch (Throwable ignored) {
100       return new JavacTreeRefScanner();
101     }
102   }
103 }