diff --git a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacBindingResolver.java b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacBindingResolver.java index 987a9b152e4..1239eca168d 100644 --- a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacBindingResolver.java +++ b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacBindingResolver.java @@ -23,6 +23,7 @@ import org.eclipse.core.runtime.ILog; import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.WorkingCopyOwner; import org.eclipse.jdt.internal.javac.dom.JavacAnnotationBinding; import org.eclipse.jdt.internal.javac.dom.JavacErrorMethodBinding; import org.eclipse.jdt.internal.javac.dom.JavacLambdaBinding; @@ -231,12 +232,14 @@ public IBinding getBinding(String key) { } public final Bindings bindings = new Bindings(); + private WorkingCopyOwner owner; - public JavacBindingResolver(IJavaProject javaProject, JavacTask javacTask, Context context, JavacConverter converter) { + public JavacBindingResolver(IJavaProject javaProject, JavacTask javacTask, Context context, JavacConverter converter, WorkingCopyOwner owner) { this.javac = javacTask; this.context = context; this.javaProject = javaProject; this.converter = converter; + this.owner = owner; } private void resolve() { @@ -1003,4 +1006,9 @@ private static Symbol getRecoveredSymbol(com.sun.tools.javac.code.Type type) { } return null; } + + @Override + public WorkingCopyOwner getWorkingCopyOwner() { + return this.owner; + } } diff --git a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacCompilationUnitResolver.java b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacCompilationUnitResolver.java index 6ddbc7f84d2..66265f062c7 100644 --- a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacCompilationUnitResolver.java +++ b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacCompilationUnitResolver.java @@ -127,7 +127,7 @@ public void resolve(String[] sourceFilePaths, String[] encodings, String[] bindi // parse source units Map res = - parse(sourceUnitList.toArray(org.eclipse.jdt.internal.compiler.env.ICompilationUnit[]::new), apiLevel, compilerOptions, flags, (IJavaProject)null, monitor); + parse(sourceUnitList.toArray(org.eclipse.jdt.internal.compiler.env.ICompilationUnit[]::new), apiLevel, compilerOptions, flags, (IJavaProject)null, null, monitor); for (var entry : res.entrySet()) { CompilationUnit cu = entry.getValue(); @@ -148,7 +148,7 @@ public void resolve(String[] sourceFilePaths, String[] encodings, String[] bindi public void resolve(ICompilationUnit[] compilationUnits, String[] bindingKeys, ASTRequestor requestor, int apiLevel, Map compilerOptions, IJavaProject project, WorkingCopyOwner workingCopyOwner, int flags, IProgressMonitor monitor) { - Map units = parse(compilationUnits, apiLevel, compilerOptions, flags, monitor); + Map units = parse(compilationUnits, apiLevel, compilerOptions, flags, workingCopyOwner, monitor); if (requestor != null) { final JavacBindingResolver[] bindingResolver = new JavacBindingResolver[1]; bindingResolver[0] = null; @@ -221,7 +221,7 @@ private void resolveRequestedBindingKeys(JavacBindingResolver bindingResolver, S var compiler = ToolProvider.getSystemJavaCompiler(); var context = new Context(); JavacTask task = (JavacTask) compiler.getTask(null, null, null, List.of(), List.of(), List.of()); - bindingResolver = new JavacBindingResolver(null, task, context, new JavacConverter(null, null, context, null, true)); + bindingResolver = new JavacBindingResolver(null, task, context, new JavacConverter(null, null, context, null, true), null); } for (CompilationUnit cu : units) { @@ -328,14 +328,21 @@ public void consumeFullyQualifiedName(char[] fullyQualifiedName) { @Override public void parse(ICompilationUnit[] compilationUnits, ASTRequestor requestor, int apiLevel, Map compilerOptions, int flags, IProgressMonitor monitor) { - Map units = parse(compilationUnits, apiLevel, compilerOptions, flags, monitor); + WorkingCopyOwner workingCopyOwner = Arrays.stream(compilationUnits) + .filter(ICompilationUnit.class::isInstance) + .map(ICompilationUnit.class::cast) + .map(ICompilationUnit::getOwner) + .filter(Objects::nonNull) + .findFirst() + .orElse(null); + Map units = parse(compilationUnits, apiLevel, compilerOptions, flags, workingCopyOwner, monitor); if (requestor != null) { units.forEach(requestor::acceptAST); } } private Map parse(ICompilationUnit[] compilationUnits, int apiLevel, - Map compilerOptions, int flags, IProgressMonitor monitor) { + Map compilerOptions, int flags, WorkingCopyOwner workingCopyOwner, IProgressMonitor monitor) { // TODO ECJCompilationUnitResolver has support for dietParse and ignore method body // is this something we need? if (compilationUnits.length > 0 @@ -344,7 +351,7 @@ private Map parse(ICompilationUnit[] compilat // all in same project, build together return parse(Arrays.stream(compilationUnits).map(org.eclipse.jdt.internal.compiler.env.ICompilationUnit.class::cast).toArray(org.eclipse.jdt.internal.compiler.env.ICompilationUnit[]::new), - apiLevel, compilerOptions, flags, compilationUnits[0].getJavaProject(), monitor) + apiLevel, compilerOptions, flags, compilationUnits[0].getJavaProject(), workingCopyOwner, monitor) .entrySet().stream().collect(Collectors.toMap(entry -> (ICompilationUnit)entry.getKey(), entry -> entry.getValue())); } // build individually @@ -352,7 +359,7 @@ private Map parse(ICompilationUnit[] compilat for (ICompilationUnit in : compilationUnits) { if (in instanceof org.eclipse.jdt.internal.compiler.env.ICompilationUnit compilerUnit) { res.put(in, parse(new org.eclipse.jdt.internal.compiler.env.ICompilationUnit[] { compilerUnit }, - apiLevel, compilerOptions, flags, in.getJavaProject(), monitor).get(compilerUnit)); + apiLevel, compilerOptions, flags, in.getJavaProject(), workingCopyOwner, monitor).get(compilerUnit)); } } return res; @@ -365,7 +372,7 @@ public void parse(String[] sourceFilePaths, String[] encodings, FileASTRequestor for( int i = 0; i < sourceFilePaths.length; i++ ) { org.eclipse.jdt.internal.compiler.env.ICompilationUnit ast = createSourceUnit(sourceFilePaths[i], encodings[i]); Map res = - parse(new org.eclipse.jdt.internal.compiler.env.ICompilationUnit[] {ast}, apiLevel, compilerOptions, flags, (IJavaProject)null, monitor); + parse(new org.eclipse.jdt.internal.compiler.env.ICompilationUnit[] {ast}, apiLevel, compilerOptions, flags, (IJavaProject)null, null, monitor); CompilationUnit result = res.get(ast); requestor.acceptAST(sourceFilePaths[i], result); } @@ -425,7 +432,7 @@ public CompilationUnit toCompilationUnit(org.eclipse.jdt.internal.compiler.env.I // TODO currently only parse CompilationUnit res = parse(pathToUnit.values().toArray(org.eclipse.jdt.internal.compiler.env.ICompilationUnit[]::new), - apiLevel, compilerOptions, flags, project, monitor).get(sourceUnit); + apiLevel, compilerOptions, flags, project, workingCopyOwner, monitor).get(sourceUnit); if (initialNeedsToResolveBinding) { ((JavacBindingResolver)res.ast.getBindingResolver()).isRecoveringBindings = (flags & ICompilationUnit.ENABLE_BINDINGS_RECOVERY) != 0; resolveBindings(res, apiLevel); @@ -444,7 +451,7 @@ public CompilationUnit toCompilationUnit(org.eclipse.jdt.internal.compiler.env.I } private Map parse(org.eclipse.jdt.internal.compiler.env.ICompilationUnit[] sourceUnits, int apiLevel, Map compilerOptions, - int flags, IJavaProject javaProject, IProgressMonitor monitor) { + int flags, IJavaProject javaProject, WorkingCopyOwner workingCopyOwner, IProgressMonitor monitor) { if (sourceUnits.length == 0) { return Collections.emptyMap(); } @@ -564,7 +571,7 @@ public boolean visit(Javadoc javadoc) { addCommentsToUnit(javadocComments, res); addCommentsToUnit(converter.notAttachedComments, res); attachMissingComments(res, context, rawText, converter, compilerOptions); - ast.setBindingResolver(new JavacBindingResolver(javaProject, task, context, converter)); + ast.setBindingResolver(new JavacBindingResolver(javaProject, task, context, converter, workingCopyOwner)); // ast.setOriginalModificationCount(ast.modificationCount()); // "un-dirty" AST so Rewrite can process it ast.setDefaultNodeFlag(savedDefaultNodeFlag); diff --git a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacTypeBinding.java b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacTypeBinding.java index 31e74eba745..7171b6b401d 100644 --- a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacTypeBinding.java +++ b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacTypeBinding.java @@ -150,7 +150,7 @@ public IType getJavaElement() { } } try { - return this.resolver.javaProject.findType(cleanedUpName(classSymbol), new NullProgressMonitor()); + return this.resolver.javaProject.findType(cleanedUpName(classSymbol), this.resolver.getWorkingCopyOwner(), new NullProgressMonitor()); } catch (JavaModelException ex) { ILog.get().error(ex.getMessage(), ex); } @@ -159,12 +159,10 @@ public IType getJavaElement() { } private static String cleanedUpName(ClassSymbol classSymbol) { - if (classSymbol.isInner()) { - if (classSymbol.getEnclosingElement() instanceof ClassSymbol enclosing) { - String fullClassName = classSymbol.className(); - String lastSegment = fullClassName.substring(fullClassName.lastIndexOf('.') + 1); - return cleanedUpName(enclosing) + "$" + lastSegment; - } + if (classSymbol.getEnclosingElement() instanceof ClassSymbol enclosing) { + String fullClassName = classSymbol.className(); + String lastSegment = fullClassName.substring(fullClassName.lastIndexOf('.') + 1); + return cleanedUpName(enclosing) + "$" + lastSegment; } return classSymbol.className(); }