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

Usage of CtTypeParameter and it's reference #1197

Closed
pvojtechovsky opened this issue Feb 26, 2017 · 1 comment
Closed

Usage of CtTypeParameter and it's reference #1197

pvojtechovsky opened this issue Feb 26, 2017 · 1 comment
Labels

Comments

@pvojtechovsky
Copy link
Collaborator

pvojtechovsky commented Feb 26, 2017

I started to implement a refactoring method, which removes unused method parameters, which needs a list of all implementations and declarations of refactored method, which needs correct implementation of CtExecutableReference#isOverriding method, which actually fails on #1160, whose correct implementation needs some new methods around CtTypeParameter and it's reference.
I would like to implement fix for #1160, but I am not sure, how to design appropriate new Spoon API, which is needed to implement it in elegant way.

I actually touched these problems:

  1. CtTypeParameterReference is by default created with parent==null. And therefore next call of CtTypeParameterReference#getDeclaration() returns null, because it is not able to detect scope where to search for type parameter declaration. So I think the parent field should be always initialized too - by default it might point directly to CtTypeParameter instance. Do you agree with that change?
  2. In the example below, there are two classes O and P. The typed parameter A of class O (O$A) and typed parameter D of class P (P$D) are synonyms in scope of class P.
class X {
}
class O<A extends X> {
	<B extends A> B foo() {
		return null;
	}
}
class P<D extends X, F> extends O<D> {
	@Override
	<E extends D> E foo() {
		return null;
	}
}

In different words:

CtTypeReference ctType_P_D = ...//reference to P$D
CtTypeReference ctType_O_A = ...//reference to O$A
//both assertions should pass
assertTrue(ctType_O_A.isSubtypeOf(ctType_P_D));
assertTrue(ctType_P_D.isSubtypeOf(ctType_O_A));
//may be we need a new API method `isSame`, which would pass in that case too.
assertTrue(ctType_P_D.isSame(ctType_O_A));

Do you agree to implement a method like CtTypeParameter#isSame? How it should be exactly named?

  1. What is the actual type of typed parameter in child class.
    If the class hierarchy X,O,P from example above is extended like this:
class R extends O<InputStream> {
}

and then I write spoon code like:

CtType ctType_R = ...
CtMethod methodFoo = ctType_R.getMethod("foo");
CtType ctType_O_foo_B = methodFoo.getType();

I get ctType_O_foo_B, which is CtTypeParameterReference with simple name B, which is declared in scope of method foo in class O. But I need to be able to detect that in scope of class R the reference points to something what extends InputStream.
So I would need a new spoon API method for that. But how that API should look like?
A) CtTypeParameterReference#getReferenceInScope(CtType)

CtTypeReference tr = ((CtTypeParameterReference)ctType_O_foo_B).getReferenceInScope(ctType_R);

B) CtTypeParameterReference#declaringType defines scope of type parameter and #parent defines view point.
Actually CtTypeParameterReference#parent is used to found a context/scope where CtTypeParameterReference#simpleName is defined. May be the there should be used CtTypeParameterReference#declaringType for that. Then CtTypeParameterReference#parent might be used (same like in case of CtTypeReference) to define place which is pointing at CtTypeParameter. Then the code would be like this:

((CtTypeParameterReference)ctType_O_foo_B).setParent(ctType_R);
CtTypeReference tr = ctType_O_foo_B.getDeclaration();

Are there already some spoon methods, which supports that problem?
Do you have an concept how that API should look like?

@surli surli added the question label Mar 10, 2017
@pvojtechovsky
Copy link
Collaborator Author

Java lang specification speaks about

I am closing this, because the question is too complex. And I probably do not need to answer exactly this one

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

No branches or pull requests

2 participants