Changeset 490

Show
Ignore:
Timestamp:
04/13/07 00:37:22 (1 year ago)
Author:
mikey
Message:

speed improvements for new annotation parser

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/docroot/annotationParserTest.php

    r489 r490  
    143143        } 
    144144         
    145         $annotationType = self::findAnnotationClass($annotationName, true); 
    146         if (!is_a($annotation, $annotationType)) { 
    147             throw new ReflectionException('The annotation: ' . $annotationName . ' is not an instance of ' . $annotationType . '.'); 
     145        if (self::$annotations[$hash][$annotationName]['type'] != $annotationName) { 
     146            $annotationType = self::findAnnotationClass($annotationName, true); 
     147            if (!is_a($annotation, $annotationType)) { 
     148                throw new ReflectionException('The annotation: ' . $annotationName . ' is not an instance of ' . $annotationType . '.'); 
     149            } 
    148150        } 
    149151         
     
    155157        foreach (self::$annotations[$hash][$annotationName]['params'] as $name => $value) { 
    156158            if ($refClass->hasMethod('set' . ucfirst($name)) == true) { 
    157                 $refMethod = $refClass->getMethod('set' . ucfirst($name)); 
    158                 if ($refMethod->isPublic() == false || $refMethod->isStatic() == true) { 
    159                     throw new ReflectionException('Annotation value for "' . $name . '" can not be set: setter with this name is static or not public.'); 
    160                 } 
    161                  
    162                 $refMethod->invoke($annotation, $value); 
     159                $refClass->getMethod('set' . ucfirst($name))->invoke($annotation, $value); 
    163160            } elseif ($refClass->hasProperty($name) == true) { 
    164                 $refProperty = $refClass->getProperty($name); 
    165                 if ($refProperty->isPublic() == false || $refProperty->isStatic() == true) { 
    166                     throw new ReflectionException('Annotation value for "' . $name . '" can not be set: property with this name is static or not public.'); 
    167                 } 
    168                  
    169                 $refProperty->setValue($annotation, $value); 
     161                $refClass->getProperty($name)->setValue($annotation, $value); 
    170162            } else { 
    171163                throw new ReflectionException('Annotation value for "' . $name . '" can not be set: no public setter and no public property exists with this name.'); 
  • trunk/src/main/php/net/stubbles/reflection/annotations/parser/state/stubAnnotationDocblockState.php

    r454 r490  
    3333         
    3434        // all character except * and space and line breaks 
    35         if (preg_match('/^[^* \n]$/', $token) != false) { 
     35        if (' ' !== $token && '*' !== $token && "\n" !== $token) { 
    3636            $this->parser->changeState(stubAnnotationState::TEXT); 
    3737        } 
  • trunk/src/main/php/net/stubbles/reflection/annotations/parser/state/stubAnnotationNameState.php

    r488 r490  
    5959            } 
    6060             
     61            $this->checkName(); 
    6162            $this->parser->registerAnnotation($this->name); 
    6263            $this->parser->changeState(stubAnnotationState::ANNOTATION); 
     
    6667        if ("\n" === $token) { 
    6768            if (strlen($this->name) > 0) { 
     69                $this->checkName(); 
    6870                $this->parser->registerAnnotation($this->name); 
    6971            } 
     
    7880            } 
    7981             
     82            $this->checkName(); 
    8083            $this->parser->registerAnnotation($this->name); 
    8184            $this->parser->changeState(stubAnnotationState::ANNOTATION_TYPE); 
     
    8891            } 
    8992             
     93            $this->checkName(); 
    9094            $this->parser->registerAnnotation($this->name); 
    9195            $this->parser->changeState(stubAnnotationState::PARAMS); 
     
    9397        } 
    9498         
    95         if (strlen($this->name) == 0 && preg_match('/^[a-zA-Z_]$/', $token) == false) { 
    96             throw new ReflectionException('Annotation name has to start with a letter or underscore, but starts with ' . $token); 
    97         } elseif (preg_match('/^[a-zA-Z_0-9]$/', $token) == false) { 
    98             throw new ReflectionException('Annotation name may contain letters, underscores and numbers, but contains ' . $token); 
     99        $this->name .= $token; 
     100    } 
     101     
     102    /** 
     103     * check if the name is valid 
     104     * 
     105     * @throws  ReflectionException 
     106     */ 
     107    protected function checkName() 
     108    { 
     109        if (preg_match('/^[a-zA-Z_]{1}[a-zA-Z_0-9]*$/', $this->name) == false) { 
     110            throw new ReflectionException('Annotation parameter name may contain letters, underscores and numbers, but contains an invalid character.'); 
    99111        } 
    100          
    101         $this->name .= $token; 
    102112    } 
    103113} 
  • trunk/src/main/php/net/stubbles/reflection/annotations/parser/state/stubAnnotationTypeState.php

    r454 r490  
    5555        if (']' === $token) { 
    5656            if (strlen($this->type) > 0) { 
     57                if (preg_match('/^[a-zA-Z_]{1}[a-zA-Z_0-9]*$/', $this->type) == false) { 
     58                   throw new ReflectionException('Annotation type may contain letters, underscores and numbers, but contains an invalid character.'); 
     59                } 
     60                 
    5761                $this->parser->setAnnotationType($this->type); 
    5862            } 
     
    6266        } 
    6367 
    64         if (strlen($this->type) == 0 && preg_match('/^[a-zA-Z_]$/', $token) == false) { 
    65             throw new ReflectionException('Annotation type has to start with a letter or underscore, but starts with ' . $token); 
    66         } elseif (preg_match('/^[a-zA-Z_0-9\.]$/', $token) == false) { 
    67             throw new ReflectionException('Annotation type may contain letters, underscores, numbers and dots, but contains ' . $token); 
    68         } 
    69  
    7068        $this->type .= $token; 
    7169    } 
  • trunk/src/test/php/net/stubbles/reflection/annotations/parser/state/stubAnnotationNameStateTestCase.php

    r454 r490  
    9797        $this->annotationNameState->process('('); 
    9898    } 
    99      
    100     /** 
    101      * test processing an illegal character on start of the annotation name 
    102      */ 
    103     public function testProcessIllegalCharacterOnStart() 
    104     { 
    105         $this->expectException('ReflectionException'); 
    106         $this->annotationNameState->process('1'); 
    107     } 
    108      
     99 
    109100    /** 
    110101     * test processing other characters 
     
    116107        $this->annotationNameState->process('_'); 
    117108        $this->assertEqual($this->annotationNameState->getName(), 'a1_'); 
    118          
     109    } 
     110 
     111    /** 
     112     * test processing illegal characters 
     113     */ 
     114    public function testProcessIllegalCharactersFollowedBySpace() 
     115    { 
     116        $this->annotationNameState->process('a'); 
     117        $this->annotationNameState->process('1'); 
     118        $this->annotationNameState->process('_'); 
    119119        $this->expectException('ReflectionException'); 
    120120        $this->annotationNameState->process(')'); 
     121        $this->annotationNameState->process(' '); 
     122    } 
     123 
     124    /** 
     125     * test processing illegal characters 
     126     */ 
     127    public function testProcessIllegalCharactersFollowedByLineBreak() 
     128    { 
     129        $this->annotationNameState->process('a'); 
     130        $this->annotationNameState->process('1'); 
     131        $this->annotationNameState->process('_'); 
     132        $this->expectException('ReflectionException'); 
     133        $this->annotationNameState->process(')'); 
     134        $this->annotationNameState->process("\n"); 
     135    } 
     136 
     137    /** 
     138     * test processing illegal characters 
     139     */ 
     140    public function testProcessIllegalCharactersFollowedByTypeParenthesis() 
     141    { 
     142        $this->annotationNameState->process('a'); 
     143        $this->annotationNameState->process('1'); 
     144        $this->annotationNameState->process('_'); 
     145        $this->expectException('ReflectionException'); 
     146        $this->annotationNameState->process(')'); 
     147        $this->annotationNameState->process('['); 
     148    } 
     149 
     150    /** 
     151     * test processing illegal characters 
     152     */ 
     153    public function testProcessIllegalCharactersFollowedByValueParenthesis() 
     154    { 
     155        $this->annotationNameState->process('a'); 
     156        $this->annotationNameState->process('1'); 
     157        $this->annotationNameState->process('_'); 
     158        $this->expectException('ReflectionException'); 
     159        $this->annotationNameState->process(')'); 
     160        $this->annotationNameState->process('('); 
    121161    } 
    122162} 
  • trunk/src/test/php/net/stubbles/reflection/annotations/parser/state/stubAnnotationTypeStateTestCase.php

    r454 r490  
    5454 
    5555    /** 
    56      * test processing an illegal character on start of the annotation name 
    57      */ 
    58     public function testProcessIllegalCharacterOnStart() 
    59     { 
    60         $this->expectException('ReflectionException'); 
    61         $this->annotationTypeState->process('1'); 
    62     } 
    63      
    64     /** 
    6556     * test processing other characters 
    6657     */ 
     
    7465        $this->expectException('ReflectionException'); 
    7566        $this->annotationTypeState->process(')'); 
     67        $this->annotationTypeState->process(']'); 
    7668    } 
    7769}