Title: [1286] trunk/qdox/src/test/java/com/thoughtworks/qdox/builder: Small improvements for EvaluatingVisitor related to casting
Revision
1286
Author
rfscholte
Date
2011-08-04 17:15:58 -0500 (Thu, 04 Aug 2011)

Log Message

Small improvements for EvaluatingVisitor related to casting

Modified Paths


Diff

Modified: trunk/qdox/src/main/java/com/thoughtworks/qdox/builder/EvaluatingVisitor.java (1285 => 1286)

--- trunk/qdox/src/main/java/com/thoughtworks/qdox/builder/EvaluatingVisitor.java	2011-08-04 21:46:19 UTC (rev 1285)
+++ trunk/qdox/src/main/java/com/thoughtworks/qdox/builder/EvaluatingVisitor.java	2011-08-04 22:15:58 UTC (rev 1286)
@@ -26,6 +26,7 @@
 import com.thoughtworks.qdox.model.Annotation;
 import com.thoughtworks.qdox.model.JavaAnnotation;
 import com.thoughtworks.qdox.model.JavaField;
+import com.thoughtworks.qdox.model.Type;
 import com.thoughtworks.qdox.model._expression_.Add;
 import com.thoughtworks.qdox.model._expression_.And;
 import com.thoughtworks.qdox.model._expression_.AnnotationValue;
@@ -890,38 +891,39 @@
     public Object visit( Cast annotationCast )
     {
         Object value = annotationCast.getValue().accept( this );
-        String type = annotationCast.getType().getFullyQualifiedName();
+        Type type = annotationCast.getType();
         Object result;
 
-        if ( value instanceof Number )
+        if ( type.isPrimitive() && value instanceof Number )
         {
             Number n = (Number) value;
+            String typeName = type.getName();
 
-            if ( type.equals( "byte" ) )
+            if ( typeName.equals( "byte" ) )
             {
                 result = Byte.valueOf( n.byteValue() );
             }
-            else if ( type.equals( "char" ) )
+            else if ( typeName.equals( "char" ) )
             {
                 result = Character.valueOf( (char) n.intValue() );
             }
-            else if ( type.equals( "short" ) )
+            else if ( typeName.equals( "short" ) )
             {
                 result = Short.valueOf( n.shortValue() );
             }
-            else if ( type.equals( "int" ) )
+            else if ( typeName.equals( "int" ) )
             {
                 result = Integer.valueOf( n.intValue() );
             }
-            else if ( type.equals( "long" ) )
+            else if ( typeName.equals( "long" ) )
             {
                 result = Long.valueOf( n.longValue() );
             }
-            else if ( type.equals( "float" ) )
+            else if ( typeName.equals( "float" ) )
             {
                 result = Float.valueOf( n.floatValue() );
             }
-            else if ( type.equals( "double" ) )
+            else if ( typeName.equals( "double" ) )
             {
                 result = Double.valueOf( n.doubleValue() );
             }
@@ -934,7 +936,7 @@
         {
             try
             {
-                result = Class.forName( type ).cast( value );
+                result = Class.forName( type.getFullyQualifiedName() ).cast( value );
             }
             catch ( ClassNotFoundException e )
             {

Modified: trunk/qdox/src/test/java/com/thoughtworks/qdox/builder/EvaluatingVisitorTest.java (1285 => 1286)

--- trunk/qdox/src/test/java/com/thoughtworks/qdox/builder/EvaluatingVisitorTest.java	2011-08-04 21:46:19 UTC (rev 1285)
+++ trunk/qdox/src/test/java/com/thoughtworks/qdox/builder/EvaluatingVisitorTest.java	2011-08-04 22:15:58 UTC (rev 1286)
@@ -7,6 +7,7 @@
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+import java.math.BigDecimal;
 import java.util.Collections;
 import java.util.List;
 
@@ -318,27 +319,24 @@
     public void testVisitCast()
     {
         AnnotationValue value = mock( AnnotationValue.class );
-
         when( value.accept( visitor ) ).thenReturn( 7 );
-        assertEquals( (byte) 7, visitor.visit( new Cast( new Type( "byte" ), value ) ) );
 
-        when( value.accept( visitor ) ).thenReturn( 7 );
+        assertEquals( (byte) 7, visitor.visit( new Cast( new Type( "byte" ), value ) ) );
         assertEquals( (char) 7, visitor.visit( new Cast( new Type( "char" ), value ) ) );
-
-        when( value.accept( visitor ) ).thenReturn( 7 );
         assertEquals( (short) 7, visitor.visit( new Cast( new Type( "short" ), value ) ) );
-        
-        when( value.accept( visitor ) ).thenReturn( 7 );
         assertEquals( (int) 7, visitor.visit( new Cast( new Type( "int" ), value ) ) );
-
-        when( value.accept( visitor ) ).thenReturn( 7 );
         assertEquals( (long) 7, visitor.visit( new Cast( new Type( "long" ), value ) ) );
-
-        when( value.accept( visitor ) ).thenReturn( 7 );
         assertEquals( (float) 7, visitor.visit( new Cast( new Type( "float" ), value ) ) );
-        
-        when( value.accept( visitor ) ).thenReturn( 7 );
         assertEquals( (double) 7, visitor.visit( new Cast( new Type( "double" ), value ) ) );
+
+        try
+        {
+            visitor.visit( new Cast( new Type( "void" ), value ) );
+            fail("Although 'void' is a primitive, you can't cast to it");
+        }
+        catch( IllegalArgumentException iae) 
+        {            
+        }
         
         when( value.accept( visitor ) ).thenReturn( "hello world" );
         assertEquals( (String) "hello world", visitor.visit( new Cast( new Type( "java.lang.String" ), value ) ) );


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to