-
-
Notifications
You must be signed in to change notification settings - Fork 383
review: feat: support to parse CtType, CtClass and CtField from JDK element CtPath.toString()
#4989
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
base: master
Are you sure you want to change the base?
Changes from all commits
f749dad
d3c901b
2636329
6e239d1
905f897
add1e0c
7638075
cf37d52
f8d17c2
e5d8e1f
e909f37
a2e2090
4a0e8ee
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,16 @@ 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. | ||
| * | ||
| * @param signature the method signature, e.g. "doSomething(int,java.lang.String)" | ||
| * @return null if does not exit | ||
| * | ||
| */ | ||
| @PropertyGetter(role = METHOD) | ||
| <R> CtMethod<R> getMethodBySignature(String signature); | ||
|
Collaborator
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.
Author
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]) | ||
|
Collaborator
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 :)
Author
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 @@ | |
| 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) { | ||
|
Collaborator
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
Author
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; | ||||||||||
|
Contributor
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.