You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TypeImpl.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. *
  3. * This file is part of the debugger and core tools for the AspectJ(tm)
  4. * programming language; see http://aspectj.org
  5. *
  6. * The contents of this file are subject to the Mozilla Public License
  7. * Version 1.1 (the "License"); you may not use this file except in
  8. * compliance with the License. You may obtain a copy of the License at
  9. * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. *
  16. * The Original Code is AspectJ.
  17. *
  18. * The Initial Developer of the Original Code is Xerox Corporation. Portions
  19. * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
  20. * All Rights Reserved.
  21. */
  22. package org.aspectj.tools.ajdoc;
  23. import org.aspectj.compiler.base.ast.ArrayType;
  24. import org.aspectj.compiler.base.ast.NameType;
  25. import org.aspectj.compiler.base.ast.PrimitiveType;
  26. import org.aspectj.compiler.base.ast.Type;
  27. import com.sun.javadoc.ClassDoc;
  28. public class TypeImpl { //implements org.aspectj.ajdoc.Type {
  29. public static com.sun.javadoc.Type getInstance(Type type) {
  30. return factory.getInstance(type);
  31. }
  32. public static com.sun.javadoc.Type getInstance(String spec,
  33. ClassDoc where) {
  34. return factory.getInstance(spec, where);
  35. }
  36. private final static class Primitive implements org.aspectj.ajdoc.Type {
  37. public final static Primitive getInstance(PrimitiveType type) {
  38. return getInstance(type.getName());
  39. }
  40. public final static Primitive getInstance(String name) {
  41. if ("void".equals(name)) return voidType;
  42. if ("boolean".equals(name)) return booleanType;
  43. if ("byte".equals(name)) return byteType;
  44. if ("char".equals(name)) return charType;
  45. if ("short".equals(name)) return shortType;
  46. if ("int".equals(name)) return intType;
  47. if ("long".equals(name)) return longType;
  48. if ("float".equals(name)) return floatType;
  49. if ("double".equals(name)) return doubleType;
  50. return null;
  51. }
  52. public final static boolean isPrimitive(String name) {
  53. return name.equals("boolean")
  54. || name.equals("byte")
  55. || name.equals("char")
  56. || name.equals("short")
  57. || name.equals("int")
  58. || name.equals("long")
  59. || name.equals("float")
  60. || name.equals("double");
  61. }
  62. private final String name;
  63. private Primitive(String name) { this.name = name; }
  64. public String toString() { return name; }
  65. public String typeName() { return name; }
  66. public String qualifiedTypeName() { return name; }
  67. public String dimension() { return ""; }
  68. public ClassDoc asClassDoc() { return null; }
  69. private final static Primitive voidType = new Primitive("void");
  70. private final static Primitive booleanType = new Primitive("boolean");
  71. private final static Primitive byteType = new Primitive("byte");
  72. private final static Primitive charType = new Primitive("char");
  73. private final static Primitive shortType = new Primitive("short");
  74. private final static Primitive intType = new Primitive("int");
  75. private final static Primitive longType = new Primitive("long");
  76. private final static Primitive floatType = new Primitive("float");
  77. private final static Primitive doubleType = new Primitive("double");
  78. }
  79. private final static class Array implements org.aspectj.ajdoc.Type {
  80. protected final com.sun.javadoc.Type type;
  81. protected final int dimension;
  82. private Array(com.sun.javadoc.Type type, int dimension) {
  83. this.type = type;
  84. //TODO: tune this later
  85. this.dimension = dimension;
  86. }
  87. public String toString() { return type.toString(); }
  88. public String typeName() { return type.typeName(); }
  89. public String qualifiedTypeName() { return type.qualifiedTypeName(); }
  90. public ClassDoc asClassDoc() { return type.asClassDoc(); }
  91. public String dimension() {
  92. String str = "";
  93. for (int i = 0; i < dimension; i++) str += "[]";
  94. return str;
  95. }
  96. public boolean equals(Object other) {
  97. if (!(other instanceof Array)) {
  98. return super.equals(other);
  99. }
  100. Array array = (Array)other;
  101. return array.type.equals(type)
  102. && array.dimension == dimension;
  103. }
  104. }
  105. private static final Factory factory = new Factory();
  106. private final static class Factory {
  107. private com.sun.javadoc.Type getInstance(Type type) {
  108. if (type instanceof PrimitiveType) {
  109. return Primitive.getInstance((PrimitiveType)type);
  110. } else if (type instanceof ArrayType) {
  111. ArrayType arrayType = (ArrayType)type;
  112. Type component = arrayType.getComponentType();
  113. while (component instanceof ArrayType) {
  114. component = ((ArrayType)component).getComponentType();
  115. }
  116. return new Array(getInstance(component),
  117. arrayType.getArrayDimCount());
  118. } else {
  119. return ClassDocImpl.getInstance(((NameType)type).getTypeDec());
  120. }
  121. }
  122. private com.sun.javadoc.Type getInstance(String spec,
  123. ClassDoc where) {
  124. int ibracket = spec.indexOf('[');
  125. String name;
  126. int dimension;
  127. if (ibracket != -1) {
  128. name = spec.substring(0, ibracket);
  129. dimension = spec.substring(ibracket+1).length()/2;
  130. } else {
  131. name = spec;
  132. dimension = 0;
  133. }
  134. com.sun.javadoc.Type type = Primitive.getInstance(name);
  135. if (type == null) {
  136. type = where.findClass(name); //TODO
  137. }
  138. if (dimension > 0) {
  139. type = new Array(type, dimension);
  140. }
  141. return type;
  142. }
  143. }
  144. }