PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/org.enclojure.ide/src/org/enclojure/ide/asm/tree/ClassNode.java

http://enclojure-clojure-lib.googlecode.com/
Java | 280 lines | 134 code | 27 blank | 119 comment | 17 complexity | 66933a538e5f2f6678246326ecd7a782 MD5 | raw file
  1. /***
  2. * ASM: a very small and fast Java bytecode manipulation framework
  3. * Copyright (c) 2000-2007 INRIA, France Telecom
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the copyright holders nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  28. * THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. package org.enclojure.ide.asm.tree;
  31. import org.enclojure.ide.asm.Attribute;
  32. import org.enclojure.ide.asm.ClassVisitor;
  33. import org.enclojure.ide.asm.MethodVisitor;
  34. import org.enclojure.ide.asm.FieldVisitor;
  35. import java.util.List;
  36. import java.util.ArrayList;
  37. import java.util.Arrays;
  38. /**
  39. * A node that represents a class.
  40. *
  41. * @author Eric Bruneton
  42. */
  43. public class ClassNode extends MemberNode implements ClassVisitor {
  44. /**
  45. * The class version.
  46. */
  47. public int version;
  48. /**
  49. * The class's access flags (see {@link org.enclojure.ide.asm.Opcodes}). This
  50. * field also indicates if the class is deprecated.
  51. */
  52. public int access;
  53. /**
  54. * The internal name of the class (see
  55. * {@link org.enclojure.ide.asm.Type#getInternalName() getInternalName}).
  56. */
  57. public String name;
  58. /**
  59. * The signature of the class. Mayt be <tt>null</tt>.
  60. */
  61. public String signature;
  62. /**
  63. * The internal of name of the super class (see
  64. * {@link org.enclojure.ide.asm.Type#getInternalName() getInternalName}). For
  65. * interfaces, the super class is {@link Object}. May be <tt>null</tt>,
  66. * but only for the {@link Object} class.
  67. */
  68. public String superName;
  69. /**
  70. * The internal names of the class's interfaces (see
  71. * {@link org.enclojure.ide.asm.Type#getInternalName() getInternalName}). This
  72. * list is a list of {@link String} objects.
  73. */
  74. public List interfaces;
  75. /**
  76. * The name of the source file from which this class was compiled. May be
  77. * <tt>null</tt>.
  78. */
  79. public String sourceFile;
  80. /**
  81. * Debug information to compute the correspondance between source and
  82. * compiled elements of the class. May be <tt>null</tt>.
  83. */
  84. public String sourceDebug;
  85. /**
  86. * The internal name of the enclosing class of the class. May be
  87. * <tt>null</tt>.
  88. */
  89. public String outerClass;
  90. /**
  91. * The name of the method that contains the class, or <tt>null</tt> if the
  92. * class is not enclosed in a method.
  93. */
  94. public String outerMethod;
  95. /**
  96. * The descriptor of the method that contains the class, or <tt>null</tt>
  97. * if the class is not enclosed in a method.
  98. */
  99. public String outerMethodDesc;
  100. /**
  101. * Informations about the inner classes of this class. This list is a list
  102. * of {@link InnerClassNode} objects.
  103. *
  104. * @associates org.objectweb.asm.tree.InnerClassNode
  105. */
  106. public List innerClasses;
  107. /**
  108. * The fields of this class. This list is a list of {@link FieldNode}
  109. * objects.
  110. *
  111. * @associates org.objectweb.asm.tree.FieldNode
  112. */
  113. public List fields;
  114. /**
  115. * The methods of this class. This list is a list of {@link MethodNode}
  116. * objects.
  117. *
  118. * @associates org.objectweb.asm.tree.MethodNode
  119. */
  120. public List methods;
  121. /**
  122. * Constructs a new {@link ClassNode}.
  123. */
  124. public ClassNode() {
  125. this.interfaces = new ArrayList();
  126. this.innerClasses = new ArrayList();
  127. this.fields = new ArrayList();
  128. this.methods = new ArrayList();
  129. }
  130. // ------------------------------------------------------------------------
  131. // Implementation of the ClassVisitor interface
  132. // ------------------------------------------------------------------------
  133. public void visit(
  134. final int version,
  135. final int access,
  136. final String name,
  137. final String signature,
  138. final String superName,
  139. final String[] interfaces)
  140. {
  141. this.version = version;
  142. this.access = access;
  143. this.name = name;
  144. this.signature = signature;
  145. this.superName = superName;
  146. if (interfaces != null) {
  147. this.interfaces.addAll(Arrays.asList(interfaces));
  148. }
  149. }
  150. public void visitSource(final String file, final String debug) {
  151. sourceFile = file;
  152. sourceDebug = debug;
  153. }
  154. public void visitOuterClass(
  155. final String owner,
  156. final String name,
  157. final String desc)
  158. {
  159. outerClass = owner;
  160. outerMethod = name;
  161. outerMethodDesc = desc;
  162. }
  163. public void visitInnerClass(
  164. final String name,
  165. final String outerName,
  166. final String innerName,
  167. final int access)
  168. {
  169. InnerClassNode icn = new InnerClassNode(name,
  170. outerName,
  171. innerName,
  172. access);
  173. innerClasses.add(icn);
  174. }
  175. public FieldVisitor visitField(
  176. final int access,
  177. final String name,
  178. final String desc,
  179. final String signature,
  180. final Object value)
  181. {
  182. FieldNode fn = new FieldNode(access, name, desc, signature, value);
  183. fields.add(fn);
  184. return fn;
  185. }
  186. public MethodVisitor visitMethod(
  187. final int access,
  188. final String name,
  189. final String desc,
  190. final String signature,
  191. final String[] exceptions)
  192. {
  193. MethodNode mn = new MethodNode(access,
  194. name,
  195. desc,
  196. signature,
  197. exceptions);
  198. methods.add(mn);
  199. return mn;
  200. }
  201. // ------------------------------------------------------------------------
  202. // Accept method
  203. // ------------------------------------------------------------------------
  204. /**
  205. * Makes the given class visitor visit this class.
  206. *
  207. * @param cv a class visitor.
  208. */
  209. public void accept(final ClassVisitor cv) {
  210. // visits header
  211. String[] interfaces = new String[this.interfaces.size()];
  212. this.interfaces.toArray(interfaces);
  213. cv.visit(version, access, name, signature, superName, interfaces);
  214. // visits source
  215. if (sourceFile != null || sourceDebug != null) {
  216. cv.visitSource(sourceFile, sourceDebug);
  217. }
  218. // visits outer class
  219. if (outerClass != null) {
  220. cv.visitOuterClass(outerClass, outerMethod, outerMethodDesc);
  221. }
  222. // visits attributes
  223. int i, n;
  224. n = visibleAnnotations == null ? 0 : visibleAnnotations.size();
  225. for (i = 0; i < n; ++i) {
  226. AnnotationNode an = (AnnotationNode) visibleAnnotations.get(i);
  227. an.accept(cv.visitAnnotation(an.desc, true));
  228. }
  229. n = invisibleAnnotations == null ? 0 : invisibleAnnotations.size();
  230. for (i = 0; i < n; ++i) {
  231. AnnotationNode an = (AnnotationNode) invisibleAnnotations.get(i);
  232. an.accept(cv.visitAnnotation(an.desc, false));
  233. }
  234. n = attrs == null ? 0 : attrs.size();
  235. for (i = 0; i < n; ++i) {
  236. cv.visitAttribute((Attribute) attrs.get(i));
  237. }
  238. // visits inner classes
  239. for (i = 0; i < innerClasses.size(); ++i) {
  240. ((InnerClassNode) innerClasses.get(i)).accept(cv);
  241. }
  242. // visits fields
  243. for (i = 0; i < fields.size(); ++i) {
  244. ((FieldNode) fields.get(i)).accept(cv);
  245. }
  246. // visits methods
  247. for (i = 0; i < methods.size(); ++i) {
  248. ((MethodNode) methods.get(i)).accept(cv);
  249. }
  250. // visits end
  251. cv.visitEnd();
  252. }
  253. }