Skip to content

Commit 4f4fa88

Browse files
author
Rob Stryker
committed
Fix two errors related to code select and searching for local variables
Signed-off-by: Rob Stryker <stryker@redhat.com>
1 parent 0c91f4a commit 4f4fa88

4 files changed

Lines changed: 51 additions & 58 deletions

File tree

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,16 @@ public LocatorResponse resolveLevel(org.eclipse.jdt.core.dom.ASTNode node, IBind
7070
return new LocatorResponse(ACCURATE_MATCH, false, node, false, false);
7171
} else if (this.locator.pattern.findDeclarations) {
7272
// we need to make sure the node has a VariableDeclaration in its ancestry
73-
boolean isDecl = hasVariableDeclarationAncestor(node);
74-
if( isDecl) {
75-
return new LocatorResponse(ACCURATE_MATCH, false, node, false, false);
73+
VariableDeclaration ancestor = getVariableDeclarationAncestor(node);
74+
if( ancestor != null ) {
75+
// This is a declaration, but, is it a declaration for OUR variable?
76+
char[] needleName = this.locator.pattern.name;
77+
if( needleName != null ) {
78+
char[] ancestorName = ancestor.getName().getIdentifier().toCharArray();
79+
if( this.locator.matchesName(needleName, ancestorName)) {
80+
return new LocatorResponse(ACCURATE_MATCH, false, node, false, false);
81+
}
82+
}
7683
}
7784
return toResponse(IMPOSSIBLE_MATCH);
7885
}
@@ -81,14 +88,18 @@ public LocatorResponse resolveLevel(org.eclipse.jdt.core.dom.ASTNode node, IBind
8188
}
8289

8390
private boolean hasVariableDeclarationAncestor(ASTNode node) {
91+
return getVariableDeclarationAncestor(node) != null;
92+
}
93+
94+
private VariableDeclaration getVariableDeclarationAncestor(ASTNode node) {
8495
ASTNode working = node;
8596
while(working != null ) {
86-
if( working instanceof VariableDeclaration) {
87-
return true;
97+
if( working instanceof VariableDeclaration vd) {
98+
return vd;
8899
}
89100
working = working.getParent();
90101
}
91-
return false;
102+
return null;
92103
}
93104

94105
@Override

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

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.eclipse.jdt.core.dom.JavacBindingResolver.BindingKeyException;
3434
import org.eclipse.jdt.core.dom.LambdaExpression;
3535
import org.eclipse.jdt.core.dom.MethodDeclaration;
36-
import org.eclipse.jdt.core.dom.Modifier;
3736
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
3837
import org.eclipse.jdt.core.dom.VariableDeclaration;
3938
import org.eclipse.jdt.core.dom.VariableDeclarationExpression;
@@ -48,7 +47,6 @@
4847
import org.eclipse.jdt.internal.core.ResolvedBinaryField;
4948
import org.eclipse.jdt.internal.core.ResolvedSourceField;
5049
import org.eclipse.jdt.internal.core.SourceField;
51-
import org.eclipse.jdt.internal.core.util.Util;
5250

5351
import com.sun.tools.javac.code.Flags;
5452
import com.sun.tools.javac.code.Kinds;
@@ -165,13 +163,13 @@ private IJavaElement computeJavaElement() {
165163
} else {
166164
ASTNode node = this.resolver.findNode(this.variableSymbol);
167165
if (node instanceof VariableDeclarationFragment fragment) {
168-
return toLocalVariable(fragment, (JavaElement) method);
166+
return DOMToModelPopulator.toLocalVariable(fragment, (JavaElement) method);
169167
} else if (node instanceof SingleVariableDeclaration variableDecl) {
170168
return DOMToModelPopulator.toLocalVariable(variableDecl, (JavaElement) method);
171169
} else if (node instanceof VariableDeclarationStatement statement && statement.fragments().size() == 1) {
172-
return toLocalVariable((VariableDeclarationFragment)statement.fragments().get(0), (JavaElement)method);
170+
return DOMToModelPopulator.toLocalVariable((VariableDeclarationFragment)statement.fragments().get(0), (JavaElement)method);
173171
} else if (node instanceof VariableDeclarationExpression expression && expression.fragments().size() == 1) {
174-
return toLocalVariable((VariableDeclarationFragment)expression.fragments().get(0), (JavaElement)method);
172+
return DOMToModelPopulator.toLocalVariable((VariableDeclarationFragment)expression.fragments().get(0), (JavaElement)method);
175173
}
176174
}
177175
}
@@ -387,53 +385,6 @@ public boolean isEffectivelyFinal() {
387385
return (this.variableSymbol.flags() & Flags.EFFECTIVELY_FINAL) != 0;
388386
}
389387

390-
private static LocalVariable toLocalVariable(VariableDeclarationFragment fragment, JavaElement parent) {
391-
if (fragment.getParent() instanceof VariableDeclarationStatement variableDeclaration) {
392-
return new LocalVariable(parent,
393-
fragment.getName().getIdentifier(),
394-
variableDeclaration.getStartPosition(),
395-
variableDeclaration.getStartPosition() + variableDeclaration.getLength() - 1,
396-
fragment.getName().getStartPosition(),
397-
fragment.getName().getStartPosition() + fragment.getName().getLength() - 1,
398-
Util.getSignature(variableDeclaration.getType()),
399-
null, // I don't think we need this, also it's the ECJ's annotation node
400-
toModelFlags(variableDeclaration.getModifiers(), false),
401-
false);
402-
} else if (fragment.getParent() instanceof VariableDeclarationExpression variableDeclaration) {
403-
return new LocalVariable(parent,
404-
fragment.getName().getIdentifier(),
405-
variableDeclaration.getStartPosition(),
406-
variableDeclaration.getStartPosition() + variableDeclaration.getLength() - 1,
407-
fragment.getName().getStartPosition(),
408-
fragment.getName().getStartPosition() + fragment.getName().getLength() - 1,
409-
Util.getSignature(variableDeclaration.getType()),
410-
null, // I don't think we need this, also it's the ECJ's annotation node
411-
toModelFlags(variableDeclaration.getModifiers(), false),
412-
false);
413-
}
414-
return null;
415-
}
416-
417-
private static int toModelFlags(int domModifiers, boolean isDeprecated) {
418-
int res = 0;
419-
if (Modifier.isAbstract(domModifiers)) res |= org.eclipse.jdt.core.Flags.AccAbstract;
420-
if (Modifier.isDefault(domModifiers)) res |= org.eclipse.jdt.core.Flags.AccDefaultMethod;
421-
if (Modifier.isFinal(domModifiers)) res |= org.eclipse.jdt.core.Flags.AccFinal;
422-
if (Modifier.isNative(domModifiers)) res |= org.eclipse.jdt.core.Flags.AccNative;
423-
if (Modifier.isNonSealed(domModifiers)) res |= org.eclipse.jdt.core.Flags.AccNonSealed;
424-
if (Modifier.isPrivate(domModifiers)) res |= org.eclipse.jdt.core.Flags.AccPrivate;
425-
if (Modifier.isProtected(domModifiers)) res |= org.eclipse.jdt.core.Flags.AccProtected;
426-
if (Modifier.isPublic(domModifiers)) res |= org.eclipse.jdt.core.Flags.AccPublic;
427-
if (Modifier.isSealed(domModifiers)) res |= org.eclipse.jdt.core.Flags.AccSealed;
428-
if (Modifier.isStatic(domModifiers)) res |= org.eclipse.jdt.core.Flags.AccStatic;
429-
if (Modifier.isStrictfp(domModifiers)) res |= org.eclipse.jdt.core.Flags.AccStrictfp;
430-
if (Modifier.isSynchronized(domModifiers)) res |= org.eclipse.jdt.core.Flags.AccSynchronized;
431-
if (Modifier.isTransient(domModifiers)) res |= org.eclipse.jdt.core.Flags.AccTransient;
432-
if (Modifier.isVolatile(domModifiers)) res |= org.eclipse.jdt.core.Flags.AccVolatile;
433-
if (isDeprecated) res |= org.eclipse.jdt.core.Flags.AccDeprecated;
434-
return res;
435-
}
436-
437388
@Override
438389
public String toString() {
439390
return getType().getQualifiedName() + " " + getName();

org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/DOMCodeSelector.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ public IJavaElement[] codeSelect(int offset, int length) throws JavaModelExcepti
287287
if (parent != null && bindingNode instanceof SingleVariableDeclaration variableDecl) {
288288
return new IJavaElement[] { DOMToModelPopulator.toLocalVariable(variableDecl, (JavaElement)parent) };
289289
}
290+
if( parent != null && bindingNode instanceof VariableDeclarationFragment vdf) {
291+
// Parent might be statement or expression
292+
return new IJavaElement[] { DOMToModelPopulator.toLocalVariable(vdf, (JavaElement)parent) };
293+
}
290294
}
291295
}
292296
}

org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/DOMToModelPopulator.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,33 @@ public static LocalVariable toLocalVariable(SingleVariableDeclaration parameter,
894894
return toLocalVariable(parameter, parent, parameter.getParent() instanceof MethodDeclaration);
895895
}
896896

897+
public static LocalVariable toLocalVariable(VariableDeclarationFragment fragment, JavaElement parent) {
898+
if (fragment.getParent() instanceof VariableDeclarationStatement variableDeclaration) {
899+
return new LocalVariable(parent,
900+
fragment.getName().getIdentifier(),
901+
variableDeclaration.getStartPosition(),
902+
variableDeclaration.getStartPosition() + variableDeclaration.getLength() - 1,
903+
fragment.getName().getStartPosition(),
904+
fragment.getName().getStartPosition() + fragment.getName().getLength() - 1,
905+
Util.getSignature(variableDeclaration.getType()),
906+
null, // I don't think we need this, also it's the ECJ's annotation node
907+
toModelFlags(variableDeclaration.getModifiers(), false),
908+
false);
909+
} else if (fragment.getParent() instanceof VariableDeclarationExpression variableDeclaration) {
910+
return new LocalVariable(parent,
911+
fragment.getName().getIdentifier(),
912+
variableDeclaration.getStartPosition(),
913+
variableDeclaration.getStartPosition() + variableDeclaration.getLength() - 1,
914+
fragment.getName().getStartPosition(),
915+
fragment.getName().getStartPosition() + fragment.getName().getLength() - 1,
916+
Util.getSignature(variableDeclaration.getType()),
917+
null, // I don't think we need this, also it's the ECJ's annotation node
918+
toModelFlags(variableDeclaration.getModifiers(), false),
919+
false);
920+
}
921+
return null;
922+
}
923+
897924
private static LocalVariable toLocalVariable(SingleVariableDeclaration parameter, JavaElement parent, boolean isParameter) {
898925
return new LocalVariable(parent,
899926
parameter.getName().getIdentifier(),

0 commit comments

Comments
 (0)