annotations icon indicating copy to clipboard operation
annotations copied to clipboard

AnnotationReader may incorrectly throw exceptions

Open guilhermeblanco opened this issue 9 years ago • 4 comments

From @arnaud-lb on July 6, 2012 17:11

The AnnotationReader pre-parses classes' doc block with a PreParser that doesn't have import (use) informations.

Due to this, it can happen that an annotation resolves to a wrong class.

The parser will then attempt to validate the class and fail (no @Annotation, cannot be used on classes, etc).

Here is a test case: https://github.com/doctrine/common/pull/161

The ArrayObject annotation is resolved as \ArrayObject, which is not a valid annotation. This results in the following exception:

Doctrine\Common\Annotations\AnnotationException: [Semantical Error] The class "ArrayObject" is not annotated with @Annotation. Are you sure this class can be used as annotation? If so, then you need to add @Annotation to the _class_ doc comment of "ArrayObject". If it is indeed no annotation, then you need to add @IgnoreAnnotation("ArrayObject") to the _class_ doc comment of class Doctrine\Tests\Common\Annotations\Issue159\ParseMe.

Copied from original issue: doctrine/common#159

guilhermeblanco avatar Mar 27 '16 03:03 guilhermeblanco

From @stof on July 6, 2012 17:25

could you create a pull request instead of pasting a diff ? It will be easier for us

guilhermeblanco avatar Mar 27 '16 03:03 guilhermeblanco

From @arnaud-lb on July 6, 2012 17:43

@stof done

guilhermeblanco avatar Mar 27 '16 03:03 guilhermeblanco

From @popofr13 on October 12, 2012 9:26

I have a similar error on a Symfony 2.1 project :

[Semantical Error] The class "Annotation" is not annotated with @Annotation. Are you sure this class can be used as annotation? If so, then you need to add @Annotation to the _class_ doc comment of "Annotation". If it is indeed no annotation, then you need to add @IgnoreAnnotation("Annotation") to the _class_ doc comment of class @Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter.

How can i fix it, get around ?

guilhermeblanco avatar Mar 27 '16 03:03 guilhermeblanco

I have encountered a similar error, that is probably caused by the same reason:

ERROR: [Semantical Error] The annotation "@Annotation" in method <namespace>\ExtendedClass::method() was never imported. Did you maybe forget to add a "use" statement for this annotation?

The PHP Parser only parse the class of the current object, but it does not parse all the parent classes.

schlessera avatar Mar 30 '16 15:03 schlessera