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.backwardRefs;
18 import com.sun.source.tree.Tree;
19 import com.sun.tools.javac.code.Symbol;
20 import com.sun.tools.javac.code.Type;
21 import org.jetbrains.jps.javac.ast.api.JavacDefSymbol;
22 import org.jetbrains.jps.javac.ast.api.JavacFileReferencesRegistrar;
23 import org.jetbrains.jps.javac.ast.api.JavacRefSymbol;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.List;
31 public class BackwardReferenceRegistrar implements JavacFileReferencesRegistrar {
32 private static final Symbol[] EMPTY_SYMBOL_ARRAY = new Symbol[0];
34 private BackwardReferenceIndexWriter myWriter;
37 public boolean initialize() {
38 myWriter = BackwardReferenceIndexWriter.getInstance();
39 return myWriter != null;
43 public boolean onlyImports() {
48 public void registerFile(JavaFileObject file, Set<JavacRefSymbol> refs, Collection<JavacDefSymbol> defs) {
50 List<JavacRefSymbol> fileIndexData = null;
52 for (JavacDefSymbol def : defs) {
53 Tree.Kind kind = def.getPlaceKind();
54 if (kind == Tree.Kind.CLASS) {
55 Symbol.ClassSymbol sym = (Symbol.ClassSymbol)def.getSymbol();
56 Type superclass = sym.getSuperclass();
57 List<Type> interfaces = sym.getInterfaces();
59 final Symbol[] supers;
60 if (superclass != Type.noType) {
61 supers = new Symbol[interfaces.size() + 1];
62 supers[interfaces.size()] = superclass.asElement();
64 supers = interfaces.isEmpty() ? EMPTY_SYMBOL_ARRAY : new Symbol[interfaces.size()];
68 for (Type anInterface : interfaces) {
69 supers[i++] = anInterface.asElement();
71 myWriter.writeHierarchy(sym, supers);
73 else if (kind == LightUsage.MEMBER_REFERENCE || kind == LightUsage.LAMBDA_EXPRESSION) {
74 if (fileIndexData == null) {
75 fileIndexData = new ArrayList<JavacRefSymbol>();
77 fileIndexData.add(def);
81 final List<LightUsage> usages;
82 if (fileIndexData != null) {
83 fileIndexData.addAll(refs);
84 usages = myWriter.asLightUsages(fileIndexData);
87 usages = myWriter.asLightUsages(refs);
89 myWriter.writeReferences(file, usages);