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: feature: MethodTypingContext#adaptMethod #1288

Merged
merged 5 commits into from
May 10, 2017

Conversation

pvojtechovsky
Copy link
Collaborator

I am implementing the algorithm, which searches for all methods, which has to be modified when changing method signature (e.g. remove method parameter). This algorithm needs to be able to search in all super types of sub types of declaring type of target methods, for methods with equal sub signature... yes, it is tricky sentence ... so see Example:

class A {
 void method() {}
}
interface IB {
 void method();
}
class B extends A implements IB {
//note that B does not declares `method`. It inherits it from `A`.
}
class C implements IB {
 void method();
}

If A#method() has to be refactored, then that algorithm has to found C#method() too. But note that C#method(), does not overrides A#method(), so it is not enough to use existing overriding filter.
The algorithm first needs to detect that IB#method() is overridden by B#method() (which does not exists, but is inherited from A).
We can do that by new feature implemented in this PR like this:

MethodTypingContext mtc = new MethodTypingContext()
   .setClassTypingContext(new ClassTypingContext(classB));
//create a clone of `A#method` whose generic types are adapted to scope of class `B`
CtMethod classB_method = mtc.adaptMethod(classA_method);
mtc.setMethod(classB_method);
assertTrue(mtc.isOverriding(ifaceB_method));

or shortly like this:

MethodTypingContext mtc = new MethodTypingContext()
   .setClassTypingContext(new ClassTypingContext(classB))
   .setMethod(classA_method); //it detects that method is declared in different scope and adapts it automatically
assertTrue(mtc.isOverriding(ifaceB_method));

The example above is simplified. There are no generic parameters in method, so it might be solved different way. But with generic parameters and longer type hierarchies, the adapting of method parameters is necessary, because A#method and C#method might have different parameter types, but they can be still related, thanks to some generic method(s) in between ...

Note: review only second commit. The first commit comes from another PR. I will rebase after first commit is merged.

@pvojtechovsky pvojtechovsky force-pushed the feat_adaptMethod branch 2 times, most recently from 0fdadba to 2f32060 Compare May 8, 2017 12:09
CtClass<?> ctClassWeddingLunch = factory.Class().get(WeddingLunch.class);
CtMethod<?> trWeddingLunch_eatMe = ctClassWeddingLunch.filterChildren(new NameFilter<>("eatMe")).first();

MethodTypingContext methodSTH = new MethodTypingContext().setClassTypingContext(new ClassTypingContext(ctClassWeddingLunch));
Copy link
Collaborator

Choose a reason for hiding this comment

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

what does this line mean? I thought a MethodTypingContext would be always with respect to a specific method

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes, it is correct. But there are situations when MethodTypingContext is needed for method, which does not exist.
In the example above (first comment of this PR), it is the case when I need MethodTypingContext for method void method()' in adaption scope of class B`

@pvojtechovsky pvojtechovsky changed the title review2: feature: MethodTypingContext#adaptMethod review: feature: MethodTypingContext#adaptMethod May 8, 2017
@monperrus
Copy link
Collaborator

I've updated the tests. The core contract and test of adaptMethod is fine to me.

After analysis, I see one major and one minor problem here.

The major one is that the design and usage of isOverriding(CtMethod<?> thatMethod) is hard to grasp: it seems naturally impossible that a method overrides a context (or the other way around). Do we change this here on in a subsequent PR?

@monperrus monperrus merged commit 7b7e6a8 into INRIA:master May 10, 2017
@monperrus
Copy link
Collaborator

The major one is that the design and usage of isOverriding(CtMethod<?> thatMethod) is hard to grasp: it seems naturally impossible that a method overrides a context (or the other way around). Do we change this here on in a subsequent PR?

See #1292

@pvojtechovsky pvojtechovsky deleted the feat_adaptMethod branch May 10, 2017 17:20
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.

2 participants