Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1968,7 +1968,7 @@ private void insideMemberSelect(Env env) throws IOException {
addPackageContent(env, (PackageElement) el, kinds, baseType, insideNew, srcOnly, switchItemAdder);
if (results.isEmpty() && ((PackageElement) el).getQualifiedName() == el.getSimpleName()) {
// no package content? Check for unimported class
ClassIndex ci = controller.getClasspathInfo().getClassIndex();
ClassIndex ci = controller.getClassIndex();
if (el.getEnclosedElements().isEmpty() && ci.getPackageNames(el.getSimpleName() + ".", true, EnumSet.allOf(ClassIndex.SearchScope.class)).isEmpty()) {
Trees trees = controller.getTrees();
Scope scope = env.getScope();
Expand Down Expand Up @@ -3982,7 +3982,7 @@ private void addAllStaticMemberNames(final Env env) {
}
}
ClassIndex.NameKind kind = Utilities.isCaseSensitive() ? ClassIndex.NameKind.PREFIX : ClassIndex.NameKind.CASE_INSENSITIVE_PREFIX;
Iterable<Symbols> declaredSymbols = controller.getClasspathInfo().getClassIndex().getDeclaredSymbols(prefix, kind, EnumSet.allOf(ClassIndex.SearchScope.class));
Iterable<Symbols> declaredSymbols = controller.getClassIndex().getDeclaredSymbols(prefix, kind, EnumSet.allOf(ClassIndex.SearchScope.class));
for (Symbols symbols : declaredSymbols) {
if (Utilities.isExcluded(symbols.getEnclosingType().getQualifiedName())
|| excludeHandles != null && excludeHandles.contains(symbols.getEnclosingType())
Expand Down Expand Up @@ -4462,7 +4462,7 @@ private void addPackages(Env env, String fqnPrefix, boolean srcOnly) {
ModuleElement moduleElement = el != null ? controller.getElements().getModuleOf(el) : null;
Set<String> seenPkgs = new HashSet<>();
EnumSet<ClassIndex.SearchScope> scope = srcOnly ? EnumSet.of(ClassIndex.SearchScope.SOURCE) : EnumSet.allOf(ClassIndex.SearchScope.class);
for (String pkgName : env.getController().getClasspathInfo().getClassIndex().getPackageNames(fqnPrefix, false, scope)) {
for (String pkgName : env.getController().getClassIndex().getPackageNames(fqnPrefix, false, scope)) {
if (startsWith(env, pkgName, prefix) && !Utilities.isExcluded(pkgName + ".")
&& (moduleElement != null ? elements.getPackageElement(moduleElement, pkgName) : elements.getPackageElement(pkgName)) != null) { //NOI18N
if (fqnPrefix != null) {
Expand Down Expand Up @@ -4606,7 +4606,7 @@ private void addAllTypes(Env env, EnumSet<ElementKind> kinds) {
}
}
if (!kinds.contains(ElementKind.CLASS) && !kinds.contains(ElementKind.INTERFACE)) {
Set<ElementHandle<TypeElement>> declaredTypes = controller.getClasspathInfo().getClassIndex().getDeclaredTypes(EMPTY, ClassIndex.NameKind.PREFIX, EnumSet.allOf(ClassIndex.SearchScope.class));
Set<ElementHandle<TypeElement>> declaredTypes = controller.getClassIndex().getDeclaredTypes(EMPTY, ClassIndex.NameKind.PREFIX, EnumSet.allOf(ClassIndex.SearchScope.class));
Map<String, ElementHandle<TypeElement>> removed = new HashMap<>(declaredTypes.size());
Set<String> doNotRemove = new HashSet<>();
for (ElementHandle<TypeElement> name : declaredTypes) {
Expand Down Expand Up @@ -4648,7 +4648,7 @@ private void addAllTypes(Env env, EnumSet<ElementKind> kinds) {
? Utilities.isCaseSensitive() ? ClassIndex.NameKind.CAMEL_CASE : ClassIndex.NameKind.CAMEL_CASE_INSENSITIVE
: subwordsPattern != null ? ClassIndex.NameKind.REGEXP
: Utilities.isCaseSensitive() ? ClassIndex.NameKind.PREFIX : ClassIndex.NameKind.CASE_INSENSITIVE_PREFIX;
Set<ElementHandle<TypeElement>> declaredTypes = controller.getClasspathInfo().getClassIndex().getDeclaredTypes(subwordsPattern != null ? subwordsPattern : prefix != null ? prefix : EMPTY, kind, EnumSet.allOf(ClassIndex.SearchScope.class));
Set<ElementHandle<TypeElement>> declaredTypes = controller.getClassIndex().getDeclaredTypes(subwordsPattern != null ? subwordsPattern : prefix != null ? prefix : EMPTY, kind, EnumSet.allOf(ClassIndex.SearchScope.class));
results.ensureCapacity(results.size() + declaredTypes.size());
for (ElementHandle<TypeElement> name : declaredTypes) {
if (!kinds.contains(name.getKind()) || excludeHandles != null && excludeHandles.contains(name) || isAnnonInner(name)) {
Expand Down Expand Up @@ -4701,7 +4701,7 @@ private List<DeclaredType> getSubtypesOf(Env env, DeclaredType baseType) throws
? Utilities.isCaseSensitive() ? ClassIndex.NameKind.CAMEL_CASE : ClassIndex.NameKind.CAMEL_CASE_INSENSITIVE
: subwordsPattern != null ? ClassIndex.NameKind.REGEXP
: Utilities.isCaseSensitive() ? ClassIndex.NameKind.PREFIX : ClassIndex.NameKind.CASE_INSENSITIVE_PREFIX;
for (ElementHandle<TypeElement> handle : controller.getClasspathInfo().getClassIndex().getDeclaredTypes(subwordsPattern != null ? subwordsPattern : prefix, kind, EnumSet.allOf(ClassIndex.SearchScope.class))) {
for (ElementHandle<TypeElement> handle : controller.getClassIndex().getDeclaredTypes(subwordsPattern != null ? subwordsPattern : prefix, kind, EnumSet.allOf(ClassIndex.SearchScope.class))) {
TypeElement te = handle.resolve(controller);
if (te != null && trees.isAccessible(scope, te) && types.isSubtype(types.getDeclaredType(te), baseType)) {
subtypes.add(types.getDeclaredType(te));
Expand All @@ -4711,7 +4711,7 @@ private List<DeclaredType> getSubtypesOf(Env env, DeclaredType baseType) throws
HashSet<TypeElement> elems = new HashSet<>();
LinkedList<DeclaredType> bases = new LinkedList<>();
bases.add(baseType);
ClassIndex index = controller.getClasspathInfo().getClassIndex();
ClassIndex index = controller.getClassIndex();
while (!bases.isEmpty()) {
DeclaredType head = bases.remove();
TypeElement elem = (TypeElement) head.asElement();
Expand Down Expand Up @@ -5769,7 +5769,7 @@ private Set<? extends TypeMirror> getSmartTypes(Env env) throws IOException {
Types types = controller.getTypes();
TypeElement te = (TypeElement) ((DeclaredType) err).asElement();
if (te.getQualifiedName() == te.getSimpleName()) {
ClassIndex ci = controller.getClasspathInfo().getClassIndex();
ClassIndex ci = controller.getClassIndex();
for (ElementHandle<TypeElement> eh : ci.getDeclaredTypes(te.getSimpleName().toString(), ClassIndex.NameKind.SIMPLE_NAME, EnumSet.allOf(ClassIndex.SearchScope.class))) {
te = eh.resolve(controller);
if (te != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4770,7 +4770,7 @@ private static TypeMirror typeToImport(CompilationInfo info, TreePath tp, TypeMi
if (tp.getLeaf().getKind() == Tree.Kind.MEMBER_SELECT) {
MemberSelectTree mst = (MemberSelectTree) tp.getLeaf();
if (mst.getExpression().getKind() == Tree.Kind.IDENTIFIER) {
ClassIndex index = info.getClasspathInfo().getClassIndex();
ClassIndex index = info.getClassIndex();
Types types = info.getTypes();
Trees trees = info.getTrees();
Scope scope = trees.getScope(tp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ public ComputeImports(final CompilationInfo info) {
}

private final CompilationInfo info;
private CompilationInfo allInfo;

private final PreferenceChangeListener pcl = new PreferenceChangeListener() {
@Override
Expand Down Expand Up @@ -165,42 +164,7 @@ private ComputeImports computeCandidatesEx(Set<String> forcedUnresolved) {
if (cache != null) {
return cache;
}
boolean modules = false;

if (info.getSourceVersion().compareTo(SourceVersion.RELEASE_9) <= 0) {
if (info.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.SOURCE).findResource("module-info.java") != null) {
modules = true;
}
}

if (modules) {
ClasspathInfo cpInfo = info.getClasspathInfo();
ClasspathInfo extraInfo = ClasspathInfo.create(
ClassPathSupport.createProxyClassPath(
cpInfo.getClassPath(ClasspathInfo.PathKind.BOOT),
cpInfo.getClassPath(ClasspathInfo.PathKind.MODULE_BOOT)),
ClassPathSupport.createProxyClassPath(
cpInfo.getClassPath(ClasspathInfo.PathKind.COMPILE),
cpInfo.getClassPath(ClasspathInfo.PathKind.MODULE_COMPILE),
cpInfo.getClassPath(ClasspathInfo.PathKind.MODULE_CLASS)),
cpInfo.getClassPath(ClasspathInfo.PathKind.SOURCE));
JavaSource src = JavaSource.create(extraInfo, info.getSnapshot().getSource().getFileObject());
try {
src.runUserActionTask(new Task<CompilationController>() {
@Override
public void run(CompilationController parameter) throws Exception {
allInfo = parameter;
parameter.toPhase(JavaSource.Phase.RESOLVED);
doComputeCandidates(forcedUnresolved);
}
}, true);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
} else {
allInfo = info;
doComputeCandidates(forcedUnresolved);
}
doComputeCandidates(forcedUnresolved);
info.putCachedValue(IMPORT_CANDIDATES_KEY, this, CacheClearPolicy.ON_CHANGE);
return this;
}
Expand All @@ -225,7 +189,6 @@ private synchronized void setVisitor(TreeVisitorImpl visitor) {

private void doComputeCandidates(Set<String> forcedUnresolved) {
final CompilationUnitTree cut = info.getCompilationUnit();
ClasspathInfo cpInfo = allInfo.getClasspathInfo();
final TreeVisitorImpl v = new TreeVisitorImpl(info);
setVisitor(v);
try {
Expand All @@ -249,15 +212,15 @@ private void doComputeCandidates(Set<String> forcedUnresolved) {
return;

List<Element> classes = new ArrayList<Element>();
Set<ElementHandle<TypeElement>> typeNames = cpInfo.getClassIndex().getDeclaredTypes(unresolved, NameKind.SIMPLE_NAME,EnumSet.allOf(ClassIndex.SearchScope.class));
Set<ElementHandle<TypeElement>> typeNames = info.getClassIndex().getDeclaredTypes(unresolved, NameKind.SIMPLE_NAME,EnumSet.allOf(ClassIndex.SearchScope.class));
if (typeNames == null) {
//Canceled
return;
}
for (ElementHandle<TypeElement> typeName : typeNames) {
if (isCancelled())
return;
TypeElement te = typeName.resolve(allInfo);
TypeElement te = typeName.resolve(info);

if (te == null) {
Logger.getLogger(ComputeImports.class.getName()).log(Level.INFO, "Cannot resolve type element \"" + typeName + "\".");
Expand All @@ -272,7 +235,7 @@ private void doComputeCandidates(Set<String> forcedUnresolved) {
}

if (unresolvedNonTypes.contains(unresolved)) {
Iterable<Symbols> simpleNames = cpInfo.getClassIndex().getDeclaredSymbols(unresolved, NameKind.SIMPLE_NAME,EnumSet.allOf(ClassIndex.SearchScope.class));
Iterable<Symbols> simpleNames = info.getClassIndex().getDeclaredSymbols(unresolved, NameKind.SIMPLE_NAME,EnumSet.allOf(ClassIndex.SearchScope.class));

if (simpleNames == null) {
//Canceled:
Expand All @@ -283,7 +246,7 @@ private void doComputeCandidates(Set<String> forcedUnresolved) {
if (isCancelled())
return;

final TypeElement te = p.getEnclosingType().resolve(allInfo);
final TypeElement te = p.getEnclosingType().resolve(info);
final Set<String> idents = p.getSymbols();
if (te != null) {
for (Element ne : te.getEnclosedElements()) {
Expand Down Expand Up @@ -312,7 +275,7 @@ private void doComputeCandidates(Set<String> forcedUnresolved) {
possibleMethodFQNs.clear();

for (Hint hint: v.hints) {
wasChanged |= hint.filter(allInfo, this);
wasChanged |= hint.filter(info, this);
}
}

Expand Down
6 changes: 6 additions & 0 deletions java/java.source.base/src/META-INF/upgrade/getClassIndex.hint
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"Please avoid ClasspathInfo.getClassIndex() wherever possible":
$cpInfo.getClassIndex() :: $cpInfo instanceof org.netbeans.api.java.source.ClasspathInfo
=>
"Use CompilationInfo.getClassIndex()":
$info.getClassIndex() :: matchesWithBind($cpInfo, "$info.getClasspathInfo()") && $info instanceof org.netbeans.api.java.source.CompilationInfo
;;
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.logging.Logger;
import java.util.logging.Level;
import java.util.stream.Collectors;
import javax.lang.model.SourceVersion;
import javax.swing.event.ChangeListener;
import javax.swing.text.Document;
import javax.tools.JavaFileManager;
Expand All @@ -61,6 +62,7 @@
import org.netbeans.modules.parsing.impl.indexing.PathRegistry;
import org.netbeans.spi.java.classpath.ClassPathFactory;
import org.netbeans.spi.java.classpath.ClassPathImplementation;
import org.netbeans.spi.java.classpath.support.ClassPathSupport;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.BaseUtilities;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,15 @@ ModuleElement getModule() {
}
}

/**
* TODO.
* only valid after toPhase(ELEMENTS_RESOLVED)
* @return
*/
public ClassIndex getClassIndex() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add an assert to avoid calling this when the status isn't in the right phase?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an assert in CompilationInfoImpl.

return impl.getClassIndex();
}

/**Constants to specify when a valued cached by {@link #putCachedValue(java.lang.Object, java.lang.Object, org.netbeans.api.java.source.CompilationInfo.CacheClearPolicy)}
* should be evicted from the cache.
*
Expand Down
Loading
Loading