1 22 package org.aspectj.tools.ajdoc; 23 24 import org.aspectj.compiler.base.ast.ArrayType; 25 import org.aspectj.compiler.base.ast.NameType; 26 import org.aspectj.compiler.base.ast.PrimitiveType; 27 import org.aspectj.compiler.base.ast.Type; 28 29 import com.sun.javadoc.ClassDoc; 30 31 public class TypeImpl { 33 public static com.sun.javadoc.Type getInstance(Type type) { 34 return factory.getInstance(type); 35 } 36 37 public static com.sun.javadoc.Type getInstance(String spec, 38 ClassDoc where) { 39 return factory.getInstance(spec, where); 40 } 41 42 private final static class Primitive implements org.aspectj.ajdoc.Type { 43 44 45 46 public final static Primitive getInstance(PrimitiveType type) { 47 return getInstance(type.getName()); 48 } 49 50 public final static Primitive getInstance(String name) { 51 if ("void".equals(name)) return voidType; 52 if ("boolean".equals(name)) return booleanType; 53 if ("byte".equals(name)) return byteType; 54 if ("char".equals(name)) return charType; 55 if ("short".equals(name)) return shortType; 56 if ("int".equals(name)) return intType; 57 if ("long".equals(name)) return longType; 58 if ("float".equals(name)) return floatType; 59 if ("double".equals(name)) return doubleType; 60 return null; 61 } 62 63 public final static boolean isPrimitive(String name) { 64 return name.equals("boolean") 65 || name.equals("byte") 66 || name.equals("char") 67 || name.equals("short") 68 || name.equals("int") 69 || name.equals("long") 70 || name.equals("float") 71 || name.equals("double"); 72 } 73 74 private final String name; 75 private Primitive(String name) { this.name = name; } 76 77 public String toString() { return name; } 78 public String typeName() { return name; } 79 public String qualifiedTypeName() { return name; } 80 public String dimension() { return ""; } 81 public ClassDoc asClassDoc() { return null; } 82 83 private final static Primitive voidType = new Primitive("void"); 84 private final static Primitive booleanType = new Primitive("boolean"); 85 private final static Primitive byteType = new Primitive("byte"); 86 private final static Primitive charType = new Primitive("char"); 87 private final static Primitive shortType = new Primitive("short"); 88 private final static Primitive intType = new Primitive("int"); 89 private final static Primitive longType = new Primitive("long"); 90 private final static Primitive floatType = new Primitive("float"); 91 private final static Primitive doubleType = new Primitive("double"); 92 } 93 94 private final static class Array implements org.aspectj.ajdoc.Type { 95 protected final com.sun.javadoc.Type type; 96 protected final int dimension; 97 private Array(com.sun.javadoc.Type type, int dimension) { 98 this.type = type; 99 this.dimension = dimension; 101 } 102 103 public String toString() { return type.toString(); } 104 public String typeName() { return type.typeName(); } 105 public String qualifiedTypeName() { return type.qualifiedTypeName(); } 106 public ClassDoc asClassDoc() { return type.asClassDoc(); } 107 public String dimension() { 108 String str = ""; 109 for (int i = 0; i < dimension; i++) str += "[]"; 110 return str; 111 } 112 113 public boolean equals(Object other) { 114 if (!(other instanceof Array)) { 115 return super.equals(other); 116 } 117 Array array = (Array)other; 118 return array.type.equals(type) 119 && array.dimension == dimension; 120 } 121 } 122 123 124 private static final Factory factory = new Factory(); 125 private final static class Factory { 126 127 private com.sun.javadoc.Type getInstance(Type type) { 128 if (type instanceof PrimitiveType) { 129 return Primitive.getInstance((PrimitiveType)type); 130 } else if (type instanceof ArrayType) { 131 ArrayType arrayType = (ArrayType)type; 132 Type component = arrayType.getComponentType(); 133 while (component instanceof ArrayType) { 134 component = ((ArrayType)component).getComponentType(); 135 } 136 return new Array(getInstance(component), 137 arrayType.getArrayDimCount()); 138 } else { 139 return ClassDocImpl.getInstance(((NameType)type).getTypeDec()); 140 } 141 } 142 143 private com.sun.javadoc.Type getInstance(String spec, 144 ClassDoc where) { 145 int ibracket = spec.indexOf('['); 146 String name; 147 int dimension; 148 if (ibracket != -1) { 149 name = spec.substring(0, ibracket); 150 dimension = spec.substring(ibracket+1).length()/2; 151 } else { 152 name = spec; 153 dimension = 0; 154 } 155 com.sun.javadoc.Type type = Primitive.getInstance(name); 156 if (type == null) { 157 type = where.findClass(name); } 159 if (dimension > 0) { 160 type = new Array(type, dimension); 161 } 162 return type; 163 } 164 } 165 } 166 | Popular Tags |