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

fix: cleaning of method signature computation #1561

Merged
merged 11 commits into from
Oct 2, 2017

Conversation

monperrus
Copy link
Collaborator

@monperrus monperrus commented Sep 26, 2017

We have problems regarding method signatures:

    1. the current signatures of method references are completely different which is particularly confusing. One contains the declaring type which is about method links in Javadoc, not about signatures.
    1. There is no clear definition of method signatures in the Java specification (while usually the return type is not considered as part of the signature, there are cases with generics where it actually matters).

This PR fixes 1) and makes a sensible choice for 2)

Fix #1483

up
@monperrus monperrus changed the title fix: cleaning of method signature computation WIP: fix: cleaning of method signature computation Sep 27, 2017
@monperrus
Copy link
Collaborator Author

need #1562 first in order to strengthen the test case

@INRIA INRIA deleted a comment from spoon-bot Sep 27, 2017
@pvojtechovsky
Copy link
Collaborator

makes a sensible choice for 2)

how can client choose that? I see that return value is actually not printed only if it is undefined.
May be there will be some clients, who will like signatures without return type even if return type is known - it is more fitting to: "usually the return type is not considered as part of the signature"

What about new method getSignature(boolean includingReturnType)? Or an global switch or hook which let's client register own signature printer (it is probably over-engineered?)

@monperrus
Copy link
Collaborator Author

monperrus commented Sep 28, 2017 via email

@pvojtechovsky
Copy link
Collaborator

I looked at gumtree-spoon-ast-diff project today and the labels for nodes of methods are made using method.getSignature(). This PR will may be break this algorithm, because in this case it was correct to ignore type of returned value. Will there be an easy way to generate method signature without return type, which may be different and is really not relevant in many cases?

@monperrus
Copy link
Collaborator Author

monperrus commented Sep 28, 2017 via email

@monperrus
Copy link
Collaborator Author

It seems to be doable to remove the return type from the signature.
Just basic assertEquals to change. Only the failure of ImportTest.testWithInnerEnumDoesNotImportStaticInnerMethods is more difficult.

@monperrus
Copy link
Collaborator Author

ImportTest.testWithInnerEnumDoesNotImportStaticInnerMethods is also in fact easy to fix.

So it seems we can go for a getSignature without return type.

WDYT?

@pvojtechovsky
Copy link
Collaborator

So it seems we can go for a getSignature without return type.

I can imagine that some clients will want to see return type and some wont ...
And I personally do not use getSignature - only for debugging, so I have no real use case.

Anyway I agree that this PR makes sense and Spoon should print signatures more consistent then till now. So I agree that your PR is a good way!

@monperrus
Copy link
Collaborator Author

when #1562 is merged, I'll improve the test, and then if everybody's OK, we'll proceed then.

@INRIA INRIA deleted a comment from spoon-bot Sep 29, 2017
@INRIA INRIA deleted a comment from spoon-bot Sep 29, 2017
@INRIA INRIA deleted a comment from spoon-bot Sep 29, 2017
@INRIA INRIA deleted a comment from spoon-bot Sep 29, 2017
@monperrus monperrus changed the title WIP: fix: cleaning of method signature computation fix: cleaning of method signature computation Sep 29, 2017
@monperrus
Copy link
Collaborator Author

ready for review

@@ -448,7 +448,9 @@ protected boolean addMethodImport(CtExecutableReference ref) {
protected boolean isImportedInMethodImports(CtExecutableReference<?> ref) {
if (!(ref.isImplicit()) && methodImports.containsKey(ref.getSimpleName())) {
CtExecutableReference<?> exist = methodImports.get(ref.getSimpleName());
if (exist.getSignature().equals(ref.getSignature())) {
if ((exist.getDeclaringType() != null ? exist.getDeclaringType().getQualifiedName() : "" + "." + exist.getSignature()).equals(
Copy link
Collaborator

Choose a reason for hiding this comment

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

could you add brackets around (x?y:z), so the precedence of operators is clear even for me ;-)
Or better to create private (or public?) method `getQualifiedSignature()'

private static String getShortSignatureForJavadoc(CtExecutableReference<?> ref) {
SignaturePrinter sp = new SignaturePrinter();
sp.writeNameAndParameters(ref);
return ref.getDeclaringType().getSimpleName() + CtExecutable.EXECUTABLE_SEPARATOR + sp.getSignature();
Copy link
Collaborator

Choose a reason for hiding this comment

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

What about new public class MethodSignatureHelper, which would implement

  • getSignature(...)
  • getJavaDocSignature(...)
  • getQualifiedSignature(...)
  • .,. ?

@@ -38,6 +36,8 @@

private final StringBuffer signature = new StringBuffer();
Copy link
Collaborator

Choose a reason for hiding this comment

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

what about faster StringBuilder ?

@@ -686,7 +686,7 @@ public void testSnippedWithComments(){
assertTrue(clazz1==builder.getSnippetCompilationUnit().getDeclaredTypes().get(0));

CtMethod<?> methodString = (CtMethod<?>) clazz1.getMethods().toArray()[0];
assertEquals("java.io.File foo(java.lang.String)", methodString.getSignature());
assertEquals("foo", methodString.getSimpleName());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why this change? methodString.getSignature() fails here?

@pvojtechovsky
Copy link
Collaborator

This implementation is OK for me. The comments above are optional.

@monperrus
Copy link
Collaborator Author

monperrus commented Oct 2, 2017 via email

@monperrus
Copy link
Collaborator Author

thanks, changed.

@pvojtechovsky pvojtechovsky merged commit a4e9c9c into INRIA:master Oct 2, 2017
@pvojtechovsky
Copy link
Collaborator

Thank You Martin for this fix!

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.

Inconsistent executable signature on generic types
2 participants