Skip to content
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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

kaml8
Copy link

@kaml8 kaml8 commented Oct 30, 2022

fix #4984

  • feat(CtType,CtTypeImpl): add method getMethodBySignature(String signature).
  • feat(CtClass,CtClassImpl): add method getConstructorBySignature(String signature).
  • fix(CtPathStringBuilder): delete redundant ')' when get arguments for default constructor.
  • feat(CtPathImpl): add method getJdkClass.
  • feat(CtPathImpl): rewrite method evaluateOn, search for jdk elements when no root parameter is given.
  • docs(path.md): update doc

when calling

`AbstractPathElement.getArguments([constructor with no parameters])`

will produce redundant ')'
Test cases have passed.
Now it supports to get JDK-element from CtPath(As String).
}
}
if (filtered.isEmpty() && ctType != null) filtered.add(ctType);
}
return (List<T>) filtered;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💬 2 similar findings have been found in this PR


unchecked: unchecked cast


🔎 Expand here to view all instances of this finding
File Path Line Number
src/main/java/spoon/reflect/path/impl/CtPathImpl.java 38
src/main/java/spoon/reflect/path/impl/CtPathImpl.java 38

Visit the Lift Web Console to find more details in your report.


ℹ️ Learn about @sonatype-lift commands

You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.

Command Usage
@sonatype-lift ignore Leave out the above finding from this PR
@sonatype-lift ignoreall Leave out all the existing findings from this PR
@sonatype-lift exclude <file|issue|path|tool> Exclude specified file|issue|path|tool from Lift findings by updating your config.toml file

Note: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.


Was this a good recommendation?
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]

Copy link
Collaborator

@I-Al-Istannen I-Al-Istannen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I have a few remarks :)

src/test/java/spoon/test/path/PathTest.java Show resolved Hide resolved
*/
@DerivedProperty
@PropertyGetter(role = CONSTRUCTOR)
CtConstructor<T> getConstructorBySignature(String signature);
Copy link
Collaborator

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.

Copy link
Author

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.

* @return null if does not exit
*/
@PropertyGetter(role = METHOD)
<R> CtMethod<R> getMethodBySignature(String signature);
Copy link
Collaborator

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.

Copy link
Author

Choose a reason for hiding this comment

The 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.

return (List<T>) filtered;
}

private Class<?> getJdkClass(String name) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This is duplicated in a few places in spoon

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I found similar one: TypeFactory.get(final String qualifiedName).
However, this method doesn't support shadow elements while another reload method TypeFactory.get(Class<?> cl) does.

So the question is that we need a function that transform String to Class<?>, as the method above did,
or add extra logic in TypeFactory.get(final String qualifiedName) to process shadow elements intendly.

doc/path.md Outdated Show resolved Hide resolved
doc/path.md Outdated Show resolved Hide resolved
doc/path.md Outdated Show resolved Hide resolved
src/main/java/spoon/reflect/path/impl/CtPathImpl.java Outdated Show resolved Hide resolved
if (filtered.isEmpty()) {
List<String> cls_name_list = new LinkedList<>();
CtType<?> ctType = null;
for (CtPathElement element : elements) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not very well versed with the CtPath API, but this seems like it re-implements existing functionality and we should instead adjust the matching logic of the path elements?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seeing issue #4984 , yeah I want to reuse the case that with no parameter for matching shadow elements.
The matching logic do differ, so it may also be a good idea to extract a new method evaluateOnShadowModel as you suggested.

@@ -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])
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it's a small bug in method AbstractPathElement.getArguments(), when a default constructor in, it produces redundant ).

if (!(typeMember instanceof CtConstructor)) {
continue;
}
CtConstructor<T> c = (CtConstructor<T>) typeMember;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unchecked: unchecked cast


ℹ️ Learn about @sonatype-lift commands

You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.

Command Usage
@sonatype-lift ignore Leave out the above finding from this PR
@sonatype-lift ignoreall Leave out all the existing findings from this PR
@sonatype-lift exclude <file|issue|path|tool> Exclude specified file|issue|path|tool from Lift findings by updating your config.toml file

Note: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.


Was this a good recommendation?
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]

@kaml8
Copy link
Author

kaml8 commented Oct 31, 2022

@MartinWitt
Thanks for testing, Should I modify my code as suggested below?
Because I did't see any kinds of comment in origin branch either.
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Unable to parse CtElement from a JDK-runtime-element by CtPath
2 participants