Changeset 64
- Timestamp:
- 06/24/09 18:40:18 (3 years ago)
- Files:
-
- trunk/out/templates/cs/enum.vm (modified) (1 diff)
- trunk/src/org/fluffnstuff/asdoclet/AsDoclet.java (modified) (6 diffs)
- trunk/src/org/fluffnstuff/asdoclet/generator/AsGenerator.java (modified) (6 diffs)
- trunk/src/org/fluffnstuff/asdoclet/generator/Generator.java (modified) (3 diffs)
- trunk/src/org/fluffnstuff/asdoclet/generator/VelocityGenerator.java (modified) (6 diffs)
- trunk/src/org/fluffnstuff/asdoclet/generator/utils/GeneratorUtils.java (modified) (4 diffs)
- trunk/src/org/fluffnstuff/asdoclet/generator/velocity/FieldDescriptor.java (modified) (2 diffs)
- trunk/src/org/fluffnstuff/asdoclet/handler/AbstractHandler.java (modified) (1 diff)
- trunk/src/org/fluffnstuff/asdoclet/handler/EnumHandler.java (modified) (3 diffs)
- trunk/src/org/fluffnstuff/asdoclet/handler/Handler.java (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/out/templates/cs/enum.vm
r45 r64 37 37 public enum $typeDescriptor.typeName#generics($typeDescriptor)#bounds($typeDescriptor) {#foreach($fieldDescriptor in $typeDescriptor.fieldDescriptors)#if($velocityCount>1), #end 38 38 39 $fieldDescriptor.fieldName# end39 $fieldDescriptor.fieldName#if($fieldDescriptor.value) = $fieldDescriptor.value#end#end 40 40 41 41 } trunk/src/org/fluffnstuff/asdoclet/AsDoclet.java
r39 r64 1 1 package org.fluffnstuff.asdoclet; 2 3 import java.lang.annotation.Annotation; 2 4 3 5 import org.fluffnstuff.asdoclet.generator.AsGenerator; … … 21 23 private static final String PARAM_NAMESPACE = "-namespace"; 22 24 private static final String PARAM_GENERATOR = "-generator"; 25 private static final String PARAM_ENUM = "-enum"; 23 26 private static final String PARAM_MAP = "-map"; 24 27 private static final String PARAM_GENERIC_MAP = "-genericmap"; … … 48 51 String namespace = null; 49 52 String generator = null; 53 Class<? extends Annotation> enumAnnotation = null; 50 54 51 55 ClassTypeMap typeMap = new ClassTypeMap(); … … 56 60 if (opt[0].equals(PARAM_NAMESPACE)) namespace = opt[1]; 57 61 if (opt[0].equals(PARAM_GENERATOR)) generator = opt[1]; 62 if (opt[0].equals(PARAM_ENUM)) enumAnnotation = (Class<? extends Annotation>) Class.forName(opt[1]); 58 63 if (opt[0].equals(PARAM_MAP)) { 59 64 String[] strings = opt[1].split(":"); … … 71 76 72 77 if ("actionscript".equals(generator)) { 73 return new AsGenerator(destination, namespace, typeMap, annotationMap);78 return new AsGenerator(destination, namespace, enumAnnotation, typeMap, annotationMap); 74 79 } else if ("cs".equals(generator)) { 75 return new VelocityGenerator(destination, namespace, "cs", typeMap, annotationMap);80 return new VelocityGenerator(destination, namespace, "cs", enumAnnotation, typeMap, annotationMap); 76 81 } 77 82 return null; … … 98 103 if (PARAM_NAMESPACE.equals(option)) length = 2; 99 104 if (PARAM_GENERATOR.equals(option)) length = 2; 105 if (PARAM_ENUM.equals(option)) length = 2; 100 106 if (PARAM_MAP.equals(option)) length = 2; 101 107 if (PARAM_GENERIC_MAP.equals(option)) length = 2; trunk/src/org/fluffnstuff/asdoclet/generator/AsGenerator.java
r60 r64 2 2 3 3 import java.io.IOException; 4 import java.lang.annotation.Annotation; 4 5 import java.lang.reflect.Modifier; 5 6 import java.text.MessageFormat; … … 38 39 private ASMethod proxyMethod; 39 40 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; 42 44 43 45 private Collection<String> imports; 44 46 private Collection<String> proxyImports; 45 47 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) { 47 49 this.typeMap = typeMap; 48 50 this.annotationMap = annotationMap; 51 this.enumAnnotation = enumAnnotation; 49 52 50 53 project = factory.newEmptyASProject(destination); … … 88 91 } 89 92 93 public Class<? extends Annotation> getEnumAnnotation() { 94 return enumAnnotation; 95 } 96 90 97 public TypeMap getTypeMap() { 91 98 return typeMap; … … 97 104 } 98 105 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) { 109 117 fieldType = resolveTypeArguments(classType, null, fieldType); 110 118 … … 286 294 } 287 295 296 public boolean hasEnumSupport() { 297 return false; 298 } 299 288 300 public boolean isDebug() { 289 301 return false; … … 308 320 } 309 321 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) { 311 323 ASField field = classType.newField(name, Visibility.PUBLIC, type); 312 324 313 field.setInitializer( value);325 field.setInitializer(initializer); 314 326 field.setStatic(true); 315 327 field.setConst(true); trunk/src/org/fluffnstuff/asdoclet/generator/Generator.java
r56 r64 1 1 package org.fluffnstuff.asdoclet.generator; 2 3 import java.lang.annotation.Annotation; 2 4 3 5 import org.fluffnstuff.asdoclet.map.TypeMap; … … 6 8 void addBody(String body); 7 9 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); 9 11 10 void addEnumField(Type classType, String name, String comment);12 void addEnumField(Type classType, Type fieldType, String name, Object value, String comment); 11 13 12 void addField(Type classType, int modifier, Type fieldType, String fieldName, Stringvalue, String comment);14 void addField(Type classType, int modifier, Type fieldType, String fieldName, Object value, String comment); 13 15 14 16 void addGetter(Type classType, Type methodType, int modifier, Type fieldType, String propertyName, String comment, boolean override); … … 46 48 TypeMap getAnnotationMap(); 47 49 50 Class<? extends Annotation> getEnumAnnotation(); 51 48 52 String getName(); 49 53 50 54 TypeMap getTypeMap(); 55 56 boolean hasEnumSupport(); 51 57 52 58 boolean isDebug(); trunk/src/org/fluffnstuff/asdoclet/generator/VelocityGenerator.java
r56 r64 4 4 import java.io.FileWriter; 5 5 import java.io.Writer; 6 import java.lang.annotation.Annotation; 6 7 import java.lang.reflect.Modifier; 7 8 import java.util.ArrayList; … … 30 31 private Collection<TypeDescriptor> typeDescriptors = new ArrayList<TypeDescriptor>(); 31 32 32 private File destination;33 private String language;34 private String namespace;35 36 33 private TypeDescriptor typeDescriptor; 37 34 private ClassDescriptor proxyTypeDescriptor; … … 39 36 private FieldDescriptor fieldDescriptor; 40 37 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 { 45 48 this.destination = new File(destination); 46 49 this.namespace = namespace; 47 50 this.language = language; 51 48 52 this.typeMap = typeMap; 49 53 this.annotationMap = annotationMap; 54 this.enumAnnotation = enumAnnotation; 50 55 51 56 Velocity.setProperty(Velocity.RESOURCE_LOADER, "class"); … … 95 100 } 96 101 102 public Class<? extends Annotation> getEnumAnnotation() { 103 return enumAnnotation; 104 } 105 97 106 public TypeMap getTypeMap() { 98 107 return typeMap; … … 105 114 } 106 115 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, Stringvalue, 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) { 116 125 fieldDescriptor = new FieldDescriptor(modifier, fieldType, propertyName, value); 117 126 setFieldDescription(comment); … … 238 247 } 239 248 249 public boolean hasEnumSupport() { 250 return true; 251 } 252 240 253 public boolean isDebug() { 241 254 return false; trunk/src/org/fluffnstuff/asdoclet/generator/utils/GeneratorUtils.java
r44 r64 8 8 import java.util.HashSet; 9 9 import java.util.Map; 10 import java.lang.annotation.Annotation; 10 11 11 12 import org.fluffnstuff.asdoclet.generator.Generator; … … 18 19 import com.sun.javadoc.TypeVariable; 19 20 import com.sun.javadoc.WildcardType; 21 import com.sun.javadoc.AnnotationDesc; 20 22 21 23 public 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>(); 25 25 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; 29 49 } 30 50 … … 61 81 if (visited.contains(type.toString())) return getType(type.qualifiedTypeName(), generator); 62 82 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; 63 86 64 87 Map<String, org.fluffnstuff.asdoclet.generator.Type> bounds = new HashMap<String, org.fluffnstuff.asdoclet.generator.Type>(); … … 127 150 } 128 151 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 129 161 public static org.fluffnstuff.asdoclet.generator.Type getAnnotation(String name, Generator generator) { 130 162 return getType(name, generator, generator.getAnnotationMap()); trunk/src/org/fluffnstuff/asdoclet/generator/velocity/FieldDescriptor.java
r39 r64 7 7 public class FieldDescriptor extends Descriptor { 8 8 private String fieldName; 9 private Stringvalue;9 private Object value; 10 10 private int modifier; 11 11 12 public FieldDescriptor(int modifier, Type type, String fieldName, Stringvalue) {12 public FieldDescriptor(int modifier, Type type, String fieldName, Object value) { 13 13 super(type); 14 14 … … 22 22 } 23 23 24 public StringgetValue() {24 public Object getValue() { 25 25 return value; 26 26 } trunk/src/org/fluffnstuff/asdoclet/handler/AbstractHandler.java
r58 r64 3 3 import java.beans.Introspector; 4 4 import java.lang.reflect.Modifier; 5 import java.lang.reflect.Method; 6 import java.lang.annotation.Annotation; 5 7 import java.util.HashMap; 6 8 import java.util.HashSet; trunk/src/org/fluffnstuff/asdoclet/handler/EnumHandler.java
r39 r64 1 1 package org.fluffnstuff.asdoclet.handler; 2 2 3 import com.sun.javadoc.ClassDoc; 4 import com.sun.javadoc.FieldDoc; 3 import java.lang.annotation.Annotation; 4 import java.lang.reflect.InvocationTargetException; 5 import java.lang.reflect.Method; 5 6 6 7 import org.fluffnstuff.asdoclet.generator.Generator; 7 8 import org.fluffnstuff.asdoclet.generator.Type; 8 9 import org.fluffnstuff.asdoclet.generator.utils.GeneratorUtils; 10 11 import com.sun.javadoc.ClassDoc; 12 import com.sun.javadoc.FieldDoc; 9 13 10 14 public class EnumHandler extends AbstractHandler { … … 15 19 // --------------------- Interface Handler --------------------- 16 20 17 public void process(ClassDoc classDoc) {21 public void process(ClassDoc classDoc) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException { 18 22 Type type = GeneratorUtils.getType(classDoc.qualifiedName(), getGenerator()); 19 23 … … 25 29 } 26 30 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; 29 68 } 30 69 } trunk/src/org/fluffnstuff/asdoclet/handler/Handler.java
r2 r64 1 1 package org.fluffnstuff.asdoclet.handler; 2 3 import java.lang.reflect.InvocationTargetException; 2 4 3 5 import com.sun.javadoc.ClassDoc; 4 6 5 7 public interface Handler { 6 void process(ClassDoc classDoc) ;8 void process(ClassDoc classDoc) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException; 7 9 }