Skip to content

Commit 0b57920

Browse files
Rob Strykermickaelistria
authored andcommitted
Fix testLocalVariableDeclaration1 and 10+ others
Signed-off-by: Rob Stryker <[email protected]>
1 parent fb479e7 commit 0b57920

File tree

2 files changed

+74
-16
lines changed

2 files changed

+74
-16
lines changed

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/core/search/matching/DOMLocalVariableLocator.java

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
import java.util.Objects;
1414

15+
import org.eclipse.core.runtime.CoreException;
16+
import org.eclipse.jdt.core.IJavaElement;
17+
import org.eclipse.jdt.core.dom.ASTNode;
1518
import org.eclipse.jdt.core.dom.ChildPropertyDescriptor;
1619
import org.eclipse.jdt.core.dom.Expression;
1720
import org.eclipse.jdt.core.dom.IBinding;
@@ -20,6 +23,7 @@
2023
import org.eclipse.jdt.core.dom.QualifiedName;
2124
import org.eclipse.jdt.core.dom.SimpleName;
2225
import org.eclipse.jdt.core.dom.VariableDeclaration;
26+
import org.eclipse.jdt.core.search.SearchMatch;
2327
import org.eclipse.jdt.internal.core.LocalVariable;
2428
import org.eclipse.jdt.internal.core.search.LocatorResponse;
2529

@@ -59,28 +63,71 @@ public LocatorResponse resolveLevel(org.eclipse.jdt.core.dom.ASTNode node, IBind
5963
return toResponse(IMPOSSIBLE_MATCH);
6064
}
6165
if (Objects.equals(binding.getJavaElement(), getLocalVariable())) {
62-
return toResponse(ACCURATE_MATCH);
66+
// We need to know if this is a reference request or a declaration request
67+
if (this.locator.pattern.findReferences) {
68+
return new LocatorResponse(ACCURATE_MATCH, false, node, false, false);
69+
} else if (this.locator.pattern.findDeclarations) {
70+
// we need to make sure the node has a VariableDeclaration in its ancestry
71+
boolean isDecl = hasVariableDeclarationAncestor(node);
72+
if( isDecl) {
73+
return new LocatorResponse(ACCURATE_MATCH, false, node, false, false);
74+
}
75+
return toResponse(IMPOSSIBLE_MATCH);
76+
}
6377
}
6478
return toResponse(INACCURATE_MATCH);
6579
}
6680

81+
private boolean hasVariableDeclarationAncestor(ASTNode node) {
82+
ASTNode working = node;
83+
while(working != null ) {
84+
if( working instanceof VariableDeclaration) {
85+
return true;
86+
}
87+
working = working.getParent();
88+
}
89+
return false;
90+
}
91+
6792
@Override
6893
public LocatorResponse match(VariableDeclaration node, NodeSetWrapper nodeSet, MatchLocator locator) {
69-
int referencesLevel = IMPOSSIBLE_MATCH;
70-
if (this.locator.pattern.findReferences)
94+
int defaultLevelOnMatch = this.locator.pattern.mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH;
95+
if (this.locator.pattern.findReferences) {
7196
// must be a write only access with an initializer
72-
if (this.locator.pattern.writeAccess && !this.locator.pattern.readAccess && node.getInitializer() != null)
73-
if (this.locator.matchesName(this.locator.pattern.name, node.getName().getIdentifier().toCharArray()))
74-
referencesLevel = this.locator.pattern.mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH;
75-
76-
int declarationsLevel = IMPOSSIBLE_MATCH;
77-
if (this.locator.pattern.findDeclarations)
78-
if (this.locator.matchesName(this.locator.pattern.name, node.getName().getIdentifier().toCharArray()))
79-
if (getLocalVariable() != null && node.getStartPosition() == getLocalVariable().declarationSourceStart)
80-
declarationsLevel = this.locator.pattern.mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH;
81-
82-
// Use the stronger match
83-
int level = nodeSet.addMatch(node, referencesLevel >= declarationsLevel ? referencesLevel : declarationsLevel);
84-
return toResponse(level, true);
97+
if (this.locator.pattern.writeAccess && !this.locator.pattern.readAccess && node.getInitializer() != null) {
98+
if (this.locator.matchesName(this.locator.pattern.name, node.getName().getIdentifier().toCharArray())) {
99+
return toResponse(defaultLevelOnMatch, false);
100+
}
101+
}
102+
}
103+
104+
if (this.locator.pattern.findDeclarations) {
105+
if (this.locator.matchesName(this.locator.pattern.name, node.getName().getIdentifier().toCharArray())) {
106+
LocalVariable lvFromPattern = getLocalVariable();
107+
if (lvFromPattern != null ) {
108+
if(node.getStartPosition() == lvFromPattern.declarationSourceStart) {
109+
return toResponse(defaultLevelOnMatch, false);
110+
} else if( node.getName().getStartPosition() == lvFromPattern.nameStart) {
111+
return new LocatorResponse(defaultLevelOnMatch, true, node.getName(), false, false);
112+
}
113+
}
114+
}
115+
}
116+
return toResponse(0, false);
117+
}
118+
119+
@Override
120+
public void reportSearchMatch(MatchLocator locator, ASTNode node, SearchMatch match) throws CoreException {
121+
if(this.locator.pattern.findDeclarations && hasVariableDeclarationAncestor(node) ) {
122+
LocalVariable localVariable = getLocalVariable();
123+
int offset = localVariable.nameStart;
124+
int length = localVariable.nameEnd-offset+1;
125+
IJavaElement element = localVariable;
126+
SearchMatch newMatch = locator.newDeclarationMatch(element, null, match.getAccuracy(), offset, length);
127+
SearchMatchingUtility.reportSearchMatch(locator, newMatch);
128+
} else {
129+
SearchMatchingUtility.reportSearchMatch(locator, match);
130+
}
85131
}
132+
86133
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,20 @@ public String getGenericTypeSignature(Type t, TypeSymbol s, boolean useSlashes)
372372
String simpleName = s.getSimpleName().toString();
373373
String typeArgs = ";";
374374
if(t.getTypeArguments().nonEmpty() ) {
375+
String typeArgBase = removeTrailingSemicolon(getKey(ct, ct.tsym.flatName(), false, useSlashes));
376+
final int[] counter = new int[] {0};
375377
typeArgs = '<'
376378
+ Arrays.stream(getTypeArguments())
377379
.map(x -> x instanceof JavacTypeBinding jtb ? jtb.getGenericTypeSignature(useSlashes) : x.getKey())
380+
.map(x -> {
381+
String b33 = typeArgBase;
382+
if( b33.length() > 0 && "LIZVCDBFJS[!".indexOf(x.charAt(0)) == -1) {
383+
String ret = b33 + ";{" + counter[0] + "}" + x;
384+
counter[0] = counter[0] + 1;
385+
return ret;
386+
}
387+
return x;
388+
})
378389
.collect(Collectors.joining())
379390
+ ">;";
380391
}

0 commit comments

Comments
 (0)