summaryrefslogtreecommitdiff
path: root/java/java-impl/src/com/intellij/psi/impl/light/LightReferenceListBuilder.java
blob: 16cbdfe27905bf2e28324573bbe2b29065771c12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * Copyright 2000-2011 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.psi.impl.light;

import com.intellij.lang.Language;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.psi.*;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Max Medvedev
 */
public class LightReferenceListBuilder extends LightElement implements PsiReferenceList {
  private final List<PsiJavaCodeReferenceElement> myRefs = new ArrayList<PsiJavaCodeReferenceElement>();
  private PsiJavaCodeReferenceElement[] myCachedRefs = null;
  private PsiClassType[] myCachedTypes = null;
  private final Role myRole;
  private final PsiElementFactory myFactory;

  public LightReferenceListBuilder(PsiManager manager, Role role) {
    this(manager, JavaLanguage.INSTANCE, role);
  }

  public LightReferenceListBuilder(PsiManager manager, Language language, Role role) {
    super(manager, language);
    myRole = role;
    myFactory = JavaPsiFacade.getElementFactory(getProject());
  }

  @Override
  public String toString() {
    return "light reference list";
  }

  public void addReference(PsiClass aClass) {
    addReference(aClass.getQualifiedName());
  }

  public void addReference(String qualifiedName) {
    final PsiJavaCodeReferenceElement ref = myFactory.createReferenceElementByFQClassName(qualifiedName, getResolveScope());
    myRefs.add(ref);
  }

  public void addReference(PsiClassType type) {
    final PsiClass resolved = type.resolve();
    if (resolved == null) return;

    final PsiJavaCodeReferenceElement ref = myFactory.createReferenceElementByType(type);
    myRefs.add(ref);
  }

  @NotNull
  @Override
  public PsiJavaCodeReferenceElement[] getReferenceElements() {
    if (myCachedRefs == null) {
      if (myRefs.isEmpty()) {
        myCachedRefs = PsiJavaCodeReferenceElement.EMPTY_ARRAY;
      }
      else {
        myCachedRefs = ContainerUtil.toArray(myRefs, new PsiJavaCodeReferenceElement[myRefs.size()]);
      }
    }
    return myCachedRefs;
  }

  @NotNull
  @Override
  public PsiClassType[] getReferencedTypes() {
    if (myCachedTypes == null) {
      if (myRefs.isEmpty()) {
        myCachedTypes = PsiClassType.EMPTY_ARRAY;
      }
      else {
        final int size = myRefs.size();
        myCachedTypes = new PsiClassType[size];
        for (int i = 0; i < size; i++) {
          myCachedTypes[i] = myFactory.createType(myRefs.get(i));
        }
      }
    }

    return myCachedTypes;
  }

  @Override
  public Role getRole() {
    return myRole;
  }
}