-
-
Notifications
You must be signed in to change notification settings - Fork 355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: consolidate a consistent behavior for CtElement#getParent #3793
Changes from 6 commits
ff82f1a
a5ab091
1b625e1
94f7fca
2f16fa3
8b6e119
fc1fdad
503ddc7
7233465
5fb4fb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -383,34 +383,32 @@ public boolean isParentInitialized() { | |||||||||||||||||||||||||||||
@Override | ||||||||||||||||||||||||||||||
@SuppressWarnings("unchecked") | ||||||||||||||||||||||||||||||
public <P extends CtElement> P getParent(Class<P> parentType) throws ParentNotInitializedException { | ||||||||||||||||||||||||||||||
if (parent == null) { | ||||||||||||||||||||||||||||||
return null; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
if (parentType.isAssignableFrom(getParent().getClass())) { | ||||||||||||||||||||||||||||||
return (P) getParent(); | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
return getParent().getParent(parentType); | ||||||||||||||||||||||||||||||
CtElement current = this; | ||||||||||||||||||||||||||||||
do { | ||||||||||||||||||||||||||||||
current = current.getParent(); | ||||||||||||||||||||||||||||||
if (parentType.isAssignableFrom(current.getClass())) { | ||||||||||||||||||||||||||||||
return (P) current; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} while (current.isParentInitialized()); | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same behavior can be achieved with a standard while loop. You can also get rid of the "unchecked" warning by casting with the parent type instead.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My suggestion here is incorrect:
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
return null; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
@Override | ||||||||||||||||||||||||||||||
@SuppressWarnings("unchecked") | ||||||||||||||||||||||||||||||
public <E extends CtElement> E getParent(Filter<E> filter) throws ParentNotInitializedException { | ||||||||||||||||||||||||||||||
E current = (E) getParent(); | ||||||||||||||||||||||||||||||
while (true) { | ||||||||||||||||||||||||||||||
CtElement current = this; | ||||||||||||||||||||||||||||||
do { | ||||||||||||||||||||||||||||||
current = current.getParent(); | ||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||
while (current != null && !filter.matches(current)) { | ||||||||||||||||||||||||||||||
current = (E) current.getParent(); | ||||||||||||||||||||||||||||||
if (filter.matches((E) current)) { | ||||||||||||||||||||||||||||||
return (E) current; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||
} catch (ClassCastException e) { | ||||||||||||||||||||||||||||||
} catch (ClassCastException ignored) { | ||||||||||||||||||||||||||||||
// expected, some elements are not of type | ||||||||||||||||||||||||||||||
current = (E) current.getParent(); | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} while (current.isParentInitialized()); | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A standard while loop can do the same thing (couldn't do a suggestion here due to the fragmented diff): CtElement current = getParent();
while (current.isParentInitialized()) {
try {
if (filter.matches((E) current)) {
return (E) current;
}
} catch (ClassCastException ignored) {
// expected, some elements are not of type
}
current = current.getParent();
}; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My suggestion here is incorrect:
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if (current != null && filter.matches(current)) { | ||||||||||||||||||||||||||||||
return current; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
return null; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,7 +102,7 @@ public CtTypeParameter getDeclaration() { | |
return null; | ||
} | ||
|
||
CtElement e = this; | ||
CtElement typeDeclarer = this; | ||
CtElement parent = getParent(); | ||
|
||
if (parent instanceof CtTypeParameter && Objects.equals(getSimpleName(), ((CtTypeParameter) parent).getSimpleName())) { | ||
|
@@ -119,8 +119,8 @@ public CtTypeParameter getDeclaration() { | |
// we might enter in that case because of a call | ||
// of getSuperInterfaces() for example | ||
CtTypeReference typeReference = (CtTypeReference) parent; | ||
e = typeReference.getTypeDeclaration(); | ||
if (e == null) { | ||
typeDeclarer = typeReference.getTypeDeclaration(); | ||
if (typeDeclarer == null) { | ||
return null; | ||
} | ||
} else { | ||
|
@@ -130,31 +130,31 @@ public CtTypeParameter getDeclaration() { | |
|
||
if (parent instanceof CtExecutableReference) { | ||
CtExecutableReference parentExec = (CtExecutableReference) parent; | ||
if (Objects.nonNull(parentExec.getDeclaringType()) && !parentExec.getDeclaringType().equals(e)) { | ||
if (Objects.nonNull(parentExec.getDeclaringType()) && !parentExec.getDeclaringType().equals(typeDeclarer)) { | ||
CtElement parent2 = parentExec.getExecutableDeclaration(); | ||
if (parent2 instanceof CtMethod) { | ||
e = parent2; | ||
} else { | ||
e = e.getParent(CtFormalTypeDeclarer.class); | ||
typeDeclarer = parent2; | ||
} | ||
} else { | ||
e = e.getParent(CtFormalTypeDeclarer.class); | ||
} | ||
} else { | ||
if (!(e instanceof CtFormalTypeDeclarer)) { | ||
e = e.getParent(CtFormalTypeDeclarer.class); | ||
} | ||
|
||
if (!(typeDeclarer instanceof CtFormalTypeDeclarer)) { | ||
if (typeDeclarer.isParentInitialized()) { | ||
typeDeclarer = typeDeclarer.getParent(CtFormalTypeDeclarer.class); | ||
} else { | ||
typeDeclarer = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use the ternary operator here like you've done in the other places? |
||
} | ||
} | ||
|
||
// case #1: we're a type of a method parameter, a local variable, ... | ||
// the strategy is to look in the parents | ||
// collecting all formal type declarers of the hierarchy | ||
while (e != null) { | ||
CtTypeParameter result = findTypeParamDeclaration((CtFormalTypeDeclarer) e, this.getSimpleName()); | ||
while (typeDeclarer != null) { | ||
CtTypeParameter result = findTypeParamDeclaration((CtFormalTypeDeclarer) typeDeclarer, this.getSimpleName()); | ||
if (result != null) { | ||
return result; | ||
} | ||
e = e.getParent(CtFormalTypeDeclarer.class); | ||
typeDeclarer = typeDeclarer.isParentInitialized() ? typeDeclarer.getParent(CtFormalTypeDeclarer.class) : null; | ||
} | ||
return null; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above.