-
-
Notifications
You must be signed in to change notification settings - Fork 352
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
review: feat: support to parse CtType
, CtClass
and CtField
from JDK element CtPath.toString()
#4989
base: master
Are you sure you want to change the base?
Changes from all commits
3766b36
c7b28ac
056b96c
f98231e
8a78406
ed960fb
0bfa2d4
120ac0d
bbbbeba
3b779ee
6ddbcb2
02adcd8
b77a790
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 |
---|---|---|
|
@@ -234,6 +234,14 @@ public interface CtType<T> extends CtNamedElement, CtTypeInformation, CtTypeMemb | |
@PropertyGetter(role = METHOD) | ||
<R> CtMethod<R> getMethod(String name, CtTypeReference<?>... parameterTypes); | ||
|
||
/** | ||
* Gets a method from its signature. | ||
* | ||
* @return null if does not exit | ||
*/ | ||
@PropertyGetter(role = METHOD) | ||
<R> CtMethod<R> getMethodBySignature(String signature); | ||
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. I don't quite see how this relates to the title and scope of this PR. This should be done in a separate PR IMHO. 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. Same to constructor, a CtMethod has a CtPath ends with #method[signature=(...)], I need this function to get the CtElement. |
||
|
||
/** | ||
* Returns the methods that are directly declared by this class or | ||
* interface. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,8 @@ | |
import spoon.reflect.path.impl.CtNamedPathElement; | ||
import spoon.reflect.path.impl.CtPathElement; | ||
import spoon.reflect.path.impl.CtPathImpl; | ||
import spoon.reflect.path.impl.CtTypedNameElement; | ||
import spoon.reflect.path.impl.CtRolePathElement; | ||
import spoon.reflect.path.impl.CtTypedNameElement; | ||
|
||
import java.util.ArrayDeque; | ||
import java.util.Deque; | ||
|
@@ -145,7 +145,8 @@ private String parseArgumentValue(Tokenizer tokenizer, String argName, CtPathEle | |
} | ||
} else if ("]".equals(token) || ";".equals(token)) { | ||
//finished reading of argument value | ||
pathElement.addArgument(argName, argValue.toString()); | ||
//[fix bug]:AbstractPathElement.getArguments([constructor with no parameters]) | ||
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. I am not sure what this is doing here, I will need to have a look at it later. This also feels like it should be a different PR and have a test case :) 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. Yes it's a small bug in method |
||
pathElement.addArgument(argName, argValue.toString().replace("())", "()")); | ||
return token; | ||
} | ||
argValue.append(token); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,8 +7,12 @@ | |
*/ | ||
package spoon.reflect.path.impl; | ||
|
||
import spoon.reflect.declaration.CtClass; | ||
import spoon.reflect.declaration.CtElement; | ||
import spoon.reflect.declaration.CtType; | ||
import spoon.reflect.factory.TypeFactory; | ||
import spoon.reflect.path.CtPath; | ||
import spoon.reflect.path.CtRole; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
|
@@ -36,6 +40,70 @@ public <T extends CtElement> List<T> evaluateOn(CtElement... startNode) { | |
return (List<T>) filtered; | ||
} | ||
|
||
@Override | ||
public CtElement evaluateOnShadowModel() { | ||
List<String> classRoleNameList = new LinkedList<>(); | ||
CtType<?> ctType = null; | ||
for (CtPathElement element : elements) { | ||
if (element instanceof CtRolePathElement) { // search by CtRolePathElement | ||
Collection<String> values = ((CtRolePathElement) element).getArguments().values(); | ||
String val = null; | ||
if (values.iterator().hasNext()) { | ||
val = values.iterator().next(); | ||
} | ||
if (val != null) { | ||
if (CtRole.SUB_PACKAGE.equals(((CtRolePathElement) element).getRole()) | ||
|| CtRole.CONTAINED_TYPE.equals(((CtRolePathElement) element).getRole())) { | ||
classRoleNameList.add(val); | ||
} | ||
Class<?> cls = getJdkClass(String.join(".", classRoleNameList)); | ||
if (cls != null) { | ||
if (ctType == null) { | ||
ctType = new TypeFactory().get(cls); | ||
} else { | ||
if (CtRole.METHOD.equals(((CtRolePathElement) element).getRole())) { | ||
return ctType.getMethodBySignature(val); | ||
} | ||
if (CtRole.CONSTRUCTOR.equals(((CtRolePathElement) element).getRole())) { | ||
return ((CtClass) ctType).getConstructorBySignature(val); | ||
} | ||
if (CtRole.FIELD.equals(((CtRolePathElement) element).getRole())) { | ||
return ctType.getField(val); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return ctType; | ||
} | ||
|
||
private Class<?> getJdkClass(String name) { | ||
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. Note: This is duplicated in a few places in spoon 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. Yes, I found similar one: So the question is that we need a function that transform |
||
name = name.replaceAll("[\\[\\]]", ""); | ||
switch (name) { | ||
case "byte": | ||
return byte.class; | ||
case "int": | ||
return int.class; | ||
case "long": | ||
return long.class; | ||
case "float": | ||
return float.class; | ||
case "double": | ||
return double.class; | ||
case "char": | ||
return char.class; | ||
case "boolean": | ||
return boolean.class; | ||
default: | ||
try { | ||
return Class.forName(name); | ||
} catch (ClassNotFoundException e) { | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public CtPath relativePath(CtElement parent) { | ||
List<CtElement> roots = new ArrayList<>(); | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -88,6 +88,20 @@ public CtConstructor<T> getConstructor(CtTypeReference<?>... parameterTypes) { | |||||||||
return null; | ||||||||||
} | ||||||||||
|
||||||||||
@Override | ||||||||||
public CtConstructor<T> getConstructorBySignature(String signature) { | ||||||||||
for (CtTypeMember typeMember : getTypeMembers()) { | ||||||||||
if (!(typeMember instanceof CtConstructor)) { | ||||||||||
continue; | ||||||||||
} | ||||||||||
CtConstructor<T> c = (CtConstructor<T>) typeMember; | ||||||||||
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. unchecked: unchecked cast ℹ️ Learn about @sonatype-lift commandsYou can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.
Note: When talking to LiftBot, you need to refresh the page to see its response. Was this a good recommendation? |
||||||||||
if (c.getSignature().replaceAll(c.getDeclaringType().getQualifiedName(), "").equals(signature)) { | ||||||||||
return c; | ||||||||||
} | ||||||||||
} | ||||||||||
return null; | ||||||||||
} | ||||||||||
|
||||||||||
@Override | ||||||||||
public Set<CtConstructor<T>> getConstructors() { | ||||||||||
Set<CtConstructor<T>> constructors = new SignatureBasedSortedSet<>(); | ||||||||||
|
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.
I don't quite see how this relates to the title and scope of this PR. This should be done in a separate PR IMHO.
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.
As a CtConstructor has a CtPath ends with
#constructor[signature=(int)]
, I need this function to get the CtElement.