Trail:

Changeset 64

Show
Ignore:
Timestamp:
06/24/09 18:40:18 (3 years ago)
Author:
harald
Message:
  • Add custom enumeration support
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/out/templates/cs/enum.vm

    r45 r64  
    3737        public enum $typeDescriptor.typeName#generics($typeDescriptor)#bounds($typeDescriptor) {#foreach($fieldDescriptor in $typeDescriptor.fieldDescriptors)#if($velocityCount>1), #end 
    3838 
    39                 $fieldDescriptor.fieldName#end 
     39                $fieldDescriptor.fieldName#if($fieldDescriptor.value) = $fieldDescriptor.value#end#end 
    4040 
    4141        } 
  • trunk/src/org/fluffnstuff/asdoclet/AsDoclet.java

    r39 r64  
    11package org.fluffnstuff.asdoclet; 
     2 
     3import java.lang.annotation.Annotation; 
    24 
    35import org.fluffnstuff.asdoclet.generator.AsGenerator; 
     
    2123        private static final String PARAM_NAMESPACE = "-namespace"; 
    2224        private static final String PARAM_GENERATOR = "-generator"; 
     25        private static final String PARAM_ENUM = "-enum"; 
    2326        private static final String PARAM_MAP = "-map"; 
    2427        private static final String PARAM_GENERIC_MAP = "-genericmap"; 
     
    4851                String namespace = null; 
    4952                String generator = null; 
     53                Class<? extends Annotation> enumAnnotation = null; 
    5054 
    5155                ClassTypeMap typeMap = new ClassTypeMap(); 
     
    5660                        if (opt[0].equals(PARAM_NAMESPACE)) namespace = opt[1]; 
    5761                        if (opt[0].equals(PARAM_GENERATOR)) generator = opt[1]; 
     62                        if (opt[0].equals(PARAM_ENUM)) enumAnnotation = (Class<? extends Annotation>) Class.forName(opt[1]); 
    5863                        if (opt[0].equals(PARAM_MAP)) { 
    5964                                String[] strings = opt[1].split(":"); 
     
    7176 
    7277                if ("actionscript".equals(generator)) { 
    73                         return new AsGenerator(destination, namespace, typeMap, annotationMap); 
     78                        return new AsGenerator(destination, namespace, enumAnnotation, typeMap, annotationMap); 
    7479                } else if ("cs".equals(generator)) { 
    75                         return new VelocityGenerator(destination, namespace, "cs", typeMap, annotationMap); 
     80                        return new VelocityGenerator(destination, namespace, "cs", enumAnnotation, typeMap, annotationMap); 
    7681                } 
    7782                return null; 
     
    98103                if (PARAM_NAMESPACE.equals(option)) length = 2; 
    99104                if (PARAM_GENERATOR.equals(option)) length = 2; 
     105                if (PARAM_ENUM.equals(option)) length = 2; 
    100106                if (PARAM_MAP.equals(option)) length = 2; 
    101107                if (PARAM_GENERIC_MAP.equals(option)) length = 2; 
  • trunk/src/org/fluffnstuff/asdoclet/generator/AsGenerator.java

    r60 r64  
    22 
    33import java.io.IOException; 
     4import java.lang.annotation.Annotation; 
    45import java.lang.reflect.Modifier; 
    56import java.text.MessageFormat; 
     
    3839        private ASMethod proxyMethod; 
    3940 
    40         private TypeMap typeMap; 
    41         private TypeMap annotationMap; 
     41        private final TypeMap typeMap; 
     42        private final TypeMap annotationMap; 
     43        private final Class<? extends Annotation> enumAnnotation; 
    4244 
    4345        private Collection<String> imports; 
    4446        private Collection<String> proxyImports; 
    4547 
    46         public AsGenerator(String destination, String namespace, TypeMap typeMap, TypeMap annotationMap) { 
     48        public AsGenerator(String destination, String namespace, Class<? extends Annotation> enumAnnotation, TypeMap typeMap, TypeMap annotationMap) { 
    4749                this.typeMap = typeMap; 
    4850                this.annotationMap = annotationMap; 
     51                this.enumAnnotation = enumAnnotation; 
    4952 
    5053                project = factory.newEmptyASProject(destination); 
     
    8891        } 
    8992 
     93        public Class<? extends Annotation> getEnumAnnotation() { 
     94                return enumAnnotation; 
     95        } 
     96 
    9097        public TypeMap getTypeMap() { 
    9198                return typeMap; 
     
    97104        } 
    98105 
    99         public void addConstant(Type classType, Type constantType, String name, String value, String comment) { 
    100                 createConst((ASClassType) type, name, value, constantType.getName(), comment); 
    101         } 
    102  
    103         public void addEnumField(Type classType, String name, String comment) { 
    104                 Type type = GeneratorUtils.getType(String.class.getCanonicalName(), this); 
    105                 addConstant(classType, type, name, MessageFormat.format("\"{0}\"", name), comment); 
    106         } 
    107  
    108         public void addField(Type classType, int modifier, Type fieldType, String fieldName, String value, String comment) { 
     106        public void addConstant(Type classType, Type constantType, String name, String initializer, String comment) { 
     107                createConst((ASClassType) type, name, initializer, constantType.getName(), comment); 
     108        } 
     109 
     110        public void addEnumField(Type classType, Type fieldType, String name, Object value, String comment) { 
     111                if (value == null) value = name; 
     112                String s = value instanceof Number ? value.toString() : MessageFormat.format("\"{0}\"", name); 
     113                addConstant(classType, fieldType, name, s, comment); 
     114        } 
     115 
     116        public void addField(Type classType, int modifier, Type fieldType, String fieldName, Object value, String comment) { 
    109117                fieldType = resolveTypeArguments(classType, null, fieldType); 
    110118 
     
    286294        } 
    287295 
     296        public boolean hasEnumSupport() { 
     297                return false; 
     298        } 
     299 
    288300        public boolean isDebug() { 
    289301                return false; 
     
    308320        } 
    309321 
    310         private void createConst(ASClassType classType, String name, String value, String type, String comment) { 
     322        private void createConst(ASClassType classType, String name, String initializer, String type, String comment) { 
    311323                ASField field = classType.newField(name, Visibility.PUBLIC, type); 
    312324 
    313                 field.setInitializer(value); 
     325                field.setInitializer(initializer); 
    314326                field.setStatic(true); 
    315327                field.setConst(true); 
  • trunk/src/org/fluffnstuff/asdoclet/generator/Generator.java

    r56 r64  
    11package org.fluffnstuff.asdoclet.generator; 
     2 
     3import java.lang.annotation.Annotation; 
    24 
    35import org.fluffnstuff.asdoclet.map.TypeMap; 
     
    68        void addBody(String body); 
    79 
    8         void addConstant(Type classType, Type constantType, String name, String value, String comment); 
     10        void addConstant(Type classType, Type constantType, String name, String initializer, String comment); 
    911 
    10         void addEnumField(Type classType, String name, String comment); 
     12        void addEnumField(Type classType, Type fieldType, String name, Object value, String comment); 
    1113 
    12         void addField(Type classType, int modifier, Type fieldType, String fieldName, String value, String comment); 
     14        void addField(Type classType, int modifier, Type fieldType, String fieldName, Object value, String comment); 
    1315 
    1416        void addGetter(Type classType, Type methodType, int modifier, Type fieldType, String propertyName, String comment, boolean override); 
     
    4648        TypeMap getAnnotationMap(); 
    4749 
     50        Class<? extends Annotation> getEnumAnnotation(); 
     51 
    4852        String getName(); 
    4953 
    5054        TypeMap getTypeMap(); 
     55 
     56        boolean hasEnumSupport(); 
    5157 
    5258        boolean isDebug(); 
  • trunk/src/org/fluffnstuff/asdoclet/generator/VelocityGenerator.java

    r56 r64  
    44import java.io.FileWriter; 
    55import java.io.Writer; 
     6import java.lang.annotation.Annotation; 
    67import java.lang.reflect.Modifier; 
    78import java.util.ArrayList; 
     
    3031        private Collection<TypeDescriptor> typeDescriptors = new ArrayList<TypeDescriptor>(); 
    3132 
    32         private File destination; 
    33         private String language; 
    34         private String namespace; 
    35  
    3633        private TypeDescriptor typeDescriptor; 
    3734        private ClassDescriptor proxyTypeDescriptor; 
     
    3936        private FieldDescriptor fieldDescriptor; 
    4037        private MethodDescriptor methodDescriptor; 
    41         private TypeMap annotationMap; 
    42         private TypeMap typeMap; 
    43  
    44         public VelocityGenerator(String destination, String namespace, String language, TypeMap typeMap, TypeMap annotationMap) throws Exception { 
     38 
     39        private final File destination; 
     40        private final String language; 
     41        private final String namespace; 
     42 
     43        private final TypeMap annotationMap; 
     44        private final TypeMap typeMap; 
     45        private final Class<? extends Annotation> enumAnnotation; 
     46 
     47        public VelocityGenerator(String destination, String namespace, String language, Class<? extends Annotation> enumAnnotation, TypeMap typeMap, TypeMap annotationMap) throws Exception { 
    4548                this.destination = new File(destination); 
    4649                this.namespace = namespace; 
    4750                this.language = language; 
     51 
    4852                this.typeMap = typeMap; 
    4953                this.annotationMap = annotationMap; 
     54                this.enumAnnotation = enumAnnotation; 
    5055 
    5156                Velocity.setProperty(Velocity.RESOURCE_LOADER, "class"); 
     
    95100        } 
    96101 
     102        public Class<? extends Annotation> getEnumAnnotation() { 
     103                return enumAnnotation; 
     104        } 
     105 
    97106        public TypeMap getTypeMap() { 
    98107                return typeMap; 
     
    105114        } 
    106115 
    107         public void addConstant(Type classType, Type constantType, String name, String value, String comment) { 
    108                 addField(classType, Modifier.STATIC, constantType, name, value, comment); 
    109         } 
    110  
    111         public void addEnumField(Type classType, String name, String comment) { 
    112                 addField(classType, 0, Type.EMPTY, name, null, comment); 
    113         } 
    114  
    115         public void addField(Type classType, int modifier, Type fieldType, String propertyName, String value, String comment) { 
     116        public void addConstant(Type classType, Type constantType, String name, String initializer, String comment) { 
     117                addField(classType, Modifier.STATIC, constantType, name, initializer, comment); 
     118        } 
     119 
     120        public void addEnumField(Type classType, Type fieldType, String name, Object value, String comment) { 
     121                addField(classType, 0, fieldType, name, value, comment); 
     122        } 
     123 
     124        public void addField(Type classType, int modifier, Type fieldType, String propertyName, Object value, String comment) { 
    116125                fieldDescriptor = new FieldDescriptor(modifier, fieldType, propertyName, value); 
    117126                setFieldDescription(comment); 
     
    238247        } 
    239248 
     249        public boolean hasEnumSupport() { 
     250                return true; 
     251        } 
     252 
    240253        public boolean isDebug() { 
    241254                return false; 
  • trunk/src/org/fluffnstuff/asdoclet/generator/utils/GeneratorUtils.java

    r44 r64  
    88import java.util.HashSet; 
    99import java.util.Map; 
     10import java.lang.annotation.Annotation; 
    1011 
    1112import org.fluffnstuff.asdoclet.generator.Generator; 
     
    1819import com.sun.javadoc.TypeVariable; 
    1920import com.sun.javadoc.WildcardType; 
     21import com.sun.javadoc.AnnotationDesc; 
    2022 
    2123public final class GeneratorUtils { 
    22         public static org.fluffnstuff.asdoclet.generator.Type getType(Type type, Generator generator) { 
    23                 return getType(type, generator, new HashSet<String>()); 
    24         } 
     24        private static Map<String, org.fluffnstuff.asdoclet.generator.Type> enumerationTypes = new HashMap<String, org.fluffnstuff.asdoclet.generator.Type>(); 
    2525 
    26         public static org.fluffnstuff.asdoclet.generator.Type getType(String name, Generator generator) { 
    27                 if (name == null) return org.fluffnstuff.asdoclet.generator.Type.NULL; 
    28                 return getType(name, generator, generator.getTypeMap()); 
     26        private static org.fluffnstuff.asdoclet.generator.Type getEnumerationType(Type enumerationType, Generator generator) { 
     27                Class<? extends Annotation> enumAnnotation = generator.getEnumAnnotation(); 
     28                ClassDoc classDoc = enumerationType.asClassDoc(); 
     29                String typeName = enumerationType.qualifiedTypeName(); 
     30 
     31                if (generator.hasEnumSupport()) return org.fluffnstuff.asdoclet.generator.Type.EMPTY; 
     32                if (enumerationTypes.containsKey(typeName)) return enumerationTypes.get(typeName); 
     33 
     34                if (classDoc != null && enumAnnotation != null) { 
     35                        for (MethodDoc methodDoc : classDoc.methods()) { 
     36                                for (AnnotationDesc annotationDesc : methodDoc.annotations()) { 
     37                                        if (annotationDesc.annotationType().qualifiedTypeName().equals(enumAnnotation.getCanonicalName())) { 
     38                                                org.fluffnstuff.asdoclet.generator.Type type = GeneratorUtils.getType(methodDoc.returnType(), generator); 
     39                                                enumerationTypes.put(typeName, type); 
     40                                                return type; 
     41                                        } 
     42                                } 
     43                        } 
     44 
     45                        enumerationTypes.put(typeName, org.fluffnstuff.asdoclet.generator.Type.EMPTY); 
     46                } 
     47 
     48                return org.fluffnstuff.asdoclet.generator.Type.EMPTY; 
    2949        } 
    3050 
     
    6181                if (visited.contains(type.toString())) return getType(type.qualifiedTypeName(), generator); 
    6282                visited.add(type.toString()); 
     83 
     84                org.fluffnstuff.asdoclet.generator.Type enumerationType = getEnumerationType(type, generator); 
     85                if (enumerationType != org.fluffnstuff.asdoclet.generator.Type.EMPTY) return enumerationType; 
    6386 
    6487                Map<String, org.fluffnstuff.asdoclet.generator.Type> bounds = new HashMap<String, org.fluffnstuff.asdoclet.generator.Type>(); 
     
    127150        } 
    128151 
     152        public static org.fluffnstuff.asdoclet.generator.Type getType(Type type, Generator generator) { 
     153                return getType(type, generator, new HashSet<String>()); 
     154        } 
     155 
     156        public static org.fluffnstuff.asdoclet.generator.Type getType(String name, Generator generator) { 
     157                if (name == null) return org.fluffnstuff.asdoclet.generator.Type.NULL; 
     158                return getType(name, generator, generator.getTypeMap()); 
     159        } 
     160 
    129161        public static org.fluffnstuff.asdoclet.generator.Type getAnnotation(String name, Generator generator) { 
    130162                return getType(name, generator, generator.getAnnotationMap()); 
  • trunk/src/org/fluffnstuff/asdoclet/generator/velocity/FieldDescriptor.java

    r39 r64  
    77public class FieldDescriptor extends Descriptor { 
    88        private String fieldName; 
    9         private String value; 
     9        private Object value; 
    1010        private int modifier; 
    1111 
    12         public FieldDescriptor(int modifier, Type type, String fieldName, String value) { 
     12        public FieldDescriptor(int modifier, Type type, String fieldName, Object value) { 
    1313                super(type); 
    1414 
     
    2222        } 
    2323 
    24         public String getValue() { 
     24        public Object getValue() { 
    2525                return value; 
    2626        } 
  • trunk/src/org/fluffnstuff/asdoclet/handler/AbstractHandler.java

    r58 r64  
    33import java.beans.Introspector; 
    44import java.lang.reflect.Modifier; 
     5import java.lang.reflect.Method; 
     6import java.lang.annotation.Annotation; 
    57import java.util.HashMap; 
    68import java.util.HashSet; 
  • trunk/src/org/fluffnstuff/asdoclet/handler/EnumHandler.java

    r39 r64  
    11package org.fluffnstuff.asdoclet.handler; 
    22 
    3 import com.sun.javadoc.ClassDoc; 
    4 import com.sun.javadoc.FieldDoc; 
     3import java.lang.annotation.Annotation; 
     4import java.lang.reflect.InvocationTargetException; 
     5import java.lang.reflect.Method; 
    56 
    67import org.fluffnstuff.asdoclet.generator.Generator; 
    78import org.fluffnstuff.asdoclet.generator.Type; 
    89import org.fluffnstuff.asdoclet.generator.utils.GeneratorUtils; 
     10 
     11import com.sun.javadoc.ClassDoc; 
     12import com.sun.javadoc.FieldDoc; 
    913 
    1014public class EnumHandler extends AbstractHandler { 
     
    1519// --------------------- Interface Handler --------------------- 
    1620 
    17         public void process(ClassDoc classDoc)
     21        public void process(ClassDoc classDoc) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException
    1822                Type type = GeneratorUtils.getType(classDoc.qualifiedName(), getGenerator()); 
    1923 
     
    2529        } 
    2630 
    27         private void processEnumConstants(Type type, ClassDoc classDoc) { 
    28                 for (FieldDoc fieldDoc : classDoc.enumConstants()) getGenerator().addEnumField(type, fieldDoc.name(), fieldDoc.commentText()); 
     31        private void processEnumConstants(Type type, ClassDoc classDoc) throws InvocationTargetException, IllegalAccessException, ClassNotFoundException { 
     32                Method getter = findGetter(classDoc); 
     33                Type valueType = GeneratorUtils.getType(classDoc, getGenerator()); 
     34 
     35                for (FieldDoc fieldDoc : classDoc.enumConstants()) { 
     36                        Object value = null; 
     37 
     38                        if (getter != null) { 
     39                                Enum<?> enuum = Enum.valueOf((Class<Enum>) getter.getDeclaringClass(), fieldDoc.name()); 
     40                                value = getter.invoke(enuum); 
     41                        } 
     42 
     43                        getGenerator().addEnumField(type, valueType, fieldDoc.name(), value, fieldDoc.commentText()); 
     44                } 
     45        } 
     46 
     47        private Method findGetter(ClassDoc classDoc) throws ClassNotFoundException { 
     48                Class<? extends Annotation> annotation = getGenerator().getEnumAnnotation(); 
     49 
     50                Method getter = null; 
     51                if (annotation != null) { 
     52                        Class<?> clazz = Class.forName(classDoc.qualifiedName()); 
     53 
     54                        boolean present = false; 
     55                        for (Method method : clazz.getMethods()) { 
     56                                if (method.isAnnotationPresent(annotation)) { 
     57                                        if (present) { 
     58                                                throw new RuntimeException("enumeration " + classDoc + " has multiple annotations"); 
     59                                        } 
     60 
     61                                        getter = method; 
     62                                        present = true; 
     63                                } 
     64                        } 
     65                } 
     66 
     67                return getter; 
    2968        } 
    3069} 
  • trunk/src/org/fluffnstuff/asdoclet/handler/Handler.java

    r2 r64  
    11package org.fluffnstuff.asdoclet.handler; 
     2 
     3import java.lang.reflect.InvocationTargetException; 
    24 
    35import com.sun.javadoc.ClassDoc; 
    46 
    57public interface Handler { 
    6     void process(ClassDoc classDoc)
     8    void process(ClassDoc classDoc) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException
    79}