Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

Weawing doesn't support annotated targets with use statements #6

Open
redhead opened this issue Sep 10, 2013 · 11 comments
Open

Weawing doesn't support annotated targets with use statements #6

redhead opened this issue Sep 10, 2013 · 11 comments
Labels

Comments

@redhead
Copy link

redhead commented Sep 10, 2013

I want to access the annotation in the aspect when using methodAnnotatedWith with the annotation in a namespace. The adviced method file uses a use statement importing the annotation, but the use statement is not generated in the PHP file when compiling.

use MyAnnotation\DoThings;

...

/**
  * @DoThings
  */
public function method() { ... }

Accessing the annotation on the target results in an exception:

[Semantical Error] The annotation "@DoThings" in method Service::method() was never imported. Did you maybe forget to add a "use" statement for this annotation?

The usecase is to get the information stored in the annotation and process it in the aspect using the AnnotationReader (since Nette annotation doesn't support use statements imports of annotation classes), like this:

$reader->getMethodAnnotation($around->getTargetReflection(), 'MyAnnotation\DoThings');

Or is there a way to get the annotation object defined by the pointcut?

@fprochazka
Copy link
Member

The "was never imported" exception often occurs, when you don't have an autoloader for annotations. Try requiring the annotation class manually in app/bootstrap.php so we can rule out the autoloader problem.

@redhead
Copy link
Author

redhead commented Sep 10, 2013

You're missing the point. The AnnotationReader can't get the annotation from the compiled PHP file (in which the adviced method with this annotation is) because there is no use statement for the annotation which is present in the original file.

If you open the compiled PHP file, there should be a use statement for this annotation as it was in the original file. That's causing the problem.

PS: requiring the annotation in bootstrap didn't help. There is no problem in loading the annotation class, but reading the annotation since the use statement is missing in the compiled file.

@fprochazka
Copy link
Member

Why is the annotation in the compiled file? The phpGenerator did copied it from the original method, did it? Oh fuck :)

@redhead
Copy link
Author

redhead commented Sep 10, 2013

To clear things out:

original file:

use MyAnnotation\DoThings;
...
/** @DoThings */
...

the compiled file

use Kdyby\Aop\Pointcut\Matcher\Criteria;
use Symfony\Component\PropertyAccess\PropertyAccess;
...
/** @DoThings */
...

AnnotationReader can't read the annotation because it is in a namespace which is not imported in the compiled file.

@redhead
Copy link
Author

redhead commented Sep 10, 2013

The phpGenerator did copied it from the original method, did it?

Yes.

I don't know if Doctrine annotations get inherited from super classes, if yes, that would solve the problem. If not, the compiled file needs the same use statement as in original file.

By the way, it would be convenient to have a getter in the joinpoint object returning the object of the annotation defined in the pointcut's *AnnotatedWith.

@fprochazka
Copy link
Member

The problem is here... https://github.com/nette/nette/blob/master/Nette/PhpGenerator/Method.php#L91

What do you think would be better solution? expand the annotations to FQN? copying the use statements might be more problematic...

@fprochazka
Copy link
Member

I agree #7 :)

@redhead
Copy link
Author

redhead commented Sep 10, 2013

Awesome! 👍

As for a solution of this problem, it's up to you what suits you best. Expading to FQN is clean and non-problematic way to go I think. I can see some problems in copying the use statements too :)

@milo
Copy link

milo commented Sep 17, 2013

Hi. I solved the alias to FQN expanding and I'm using it without problems. https://github.com/milo/alias-expander Feel free to get what you want, if you want.

@fprochazka
Copy link
Member

@milo Good to know, but I'm probably gonna stick to Doctrine tools, because I'm already using annotations.

@milo
Copy link

milo commented Sep 18, 2013

@hosiplan I see, didn't know that. And as I see, the implementation is the same across different libraries.

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

No branches or pull requests

3 participants