Skip to content

Commit f97ae80

Browse files
datho7561mickaelistria
authored andcommitted
Key for anonymous type declarations
- should fix one completion test Signed-off-by: David Thompson <[email protected]>
1 parent 08e90d9 commit f97ae80

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/codeassist/DOMCompletionContext.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ private IBinding resolveBindingForContext(ASTNode nodep) {
151151
if (nodep instanceof TypeDeclaration typeDecl) {
152152
return typeDecl.resolveBinding();
153153
}
154+
if (nodep instanceof AnonymousClassDeclaration anonymousClassDeclaration) {
155+
return anonymousClassDeclaration.resolveBinding();
156+
}
154157
if (nodep instanceof MethodDeclaration methodDecl) {
155158
return methodDecl.resolveBinding();
156159
}

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacTypeBinding.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
4545
import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
4646
import org.eclipse.jdt.core.dom.BodyDeclaration;
47+
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
4748
import org.eclipse.jdt.core.dom.CompilationUnit;
4849
import org.eclipse.jdt.core.dom.IAnnotationBinding;
4950
import org.eclipse.jdt.core.dom.IBinding;
@@ -231,7 +232,7 @@ private IJavaElement computeJavaElement() {
231232
if (getDeclaringMethod() != null && getDeclaringMethod().getJavaElement() instanceof IMethod method) {
232233
// TODO find proper occurenceCount (eg checking the source range)
233234
return resolved(method.getType(this.typeSymbol.name.toString(), 1));
234-
}
235+
}
235236
}
236237

237238
JavaFileObject jfo = classSymbol == null ? null : classSymbol.sourcefile;
@@ -361,7 +362,7 @@ public String getGenericTypeSignature(boolean useSlashes) {
361362
return getGenericTypeSignature(this.type, this.typeSymbol, useSlashes);
362363
}
363364
public String getGenericTypeSignature(Type t, TypeSymbol s, boolean useSlashes) {
364-
if( t instanceof ClassType ct && ct.getEnclosingType() != null ) {
365+
if( t instanceof ClassType ct && !s.isAnonymous() && ct.getEnclosingType() != null ) {
365366
// return Lg1/t/s/def/Generic<Ljava/lang/Exception;>.Member;
366367
// Don't return Lg1/t/s/def/Generic$Member<>;
367368
Type enclosing = ct.getEnclosingType();
@@ -487,16 +488,17 @@ static void getKey(StringBuilder builder, Type typeToBuild, String n, boolean is
487488
}
488489
}
489490

490-
/*
491-
* TODO - this name 'n' might be something like test0502.A$1
492-
* but the test suite expects test0502.A$182,
493-
* where 182 is the location in the source of the symbol.
494-
*/
495491
String nameAsString = n.toString();
496492
if (useSlashes) {
497493
nameAsString = nameAsString.replace('.', '/');
498494
}
499495
nameAsString = nameAsString.replaceFirst("\\$([0-9]+)([A-Za-z$_][A-Za-z$_0-9]*)", "\\$$1\\$$2");
496+
if (typeToBuild.tsym.isAnonymous()) {
497+
ASTNode node = resolver.symbolToDeclaration.get(typeToBuild.tsym);
498+
if (node != null && node.getParent() instanceof ClassInstanceCreation cic) {
499+
nameAsString = nameAsString.replaceFirst("\\$([0-9]+)", "\\$" + cic.getType().getStartPosition());
500+
}
501+
}
500502
builder.append(nameAsString);
501503
// This is a hack and will likely need to be enhanced
502504
if (typeToBuild.tsym instanceof ClassSymbol classSymbol && !(classSymbol.type instanceof ErrorType) && classSymbol.owner instanceof PackageSymbol) {

0 commit comments

Comments
 (0)