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.

Signature.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. package org.aspectj.apache.bcel.classfile;
  2. /* ====================================================================
  3. * The Apache Software License, Version 1.1
  4. *
  5. * Copyright (c) 2001 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Apache" and "Apache Software Foundation" and
  28. * "Apache BCEL" must not be used to endorse or promote products
  29. * derived from this software without prior written permission. For
  30. * written permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * "Apache BCEL", nor may "Apache" appear in their name, without
  34. * prior written permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation. For more
  52. * information on the Apache Software Foundation, please see
  53. * <http://www.apache.org/>.
  54. *
  55. * Extended by Adrian Colyer, June 2005 to support unpacking of Signature
  56. * attribute
  57. */
  58. import java.io.ByteArrayInputStream;
  59. import java.io.DataInputStream;
  60. import java.io.DataOutputStream;
  61. import java.io.IOException;
  62. import org.aspectj.apache.bcel.Constants;
  63. /**
  64. * This class is derived from <em>Attribute</em> and represents a reference
  65. * to a <href="http://wwwipd.ira.uka.de/~pizza/gj/">GJ</a> attribute.
  66. *
  67. * @version $Id: Signature.java,v 1.6.6.1 2007/02/09 10:45:09 aclement Exp $
  68. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  69. * @see Attribute
  70. */
  71. public final class Signature extends Attribute {
  72. private int signature_index;
  73. /**
  74. * Initialize from another object. Note that both objects use the same
  75. * references (shallow copy). Use clone() for a physical copy.
  76. */
  77. public Signature(Signature c) {
  78. this(c.getNameIndex(), c.getLength(), c.getSignatureIndex(), c.getConstantPool());
  79. }
  80. /**
  81. * Construct object from file stream.
  82. * @param name_index Index in constant pool to CONSTANT_Utf8
  83. * @param length Content length in bytes
  84. * @param file Input stream
  85. * @param constant_pool Array of constants
  86. * @throws IOException
  87. */
  88. Signature(int name_index, int length, DataInputStream file,
  89. ConstantPool constant_pool) throws IOException
  90. {
  91. this(name_index, length, file.readUnsignedShort(), constant_pool);
  92. }
  93. /**
  94. * @param name_index Index in constant pool to CONSTANT_Utf8
  95. * @param length Content length in bytes
  96. * @param constant_pool Array of constants
  97. * @param Signature_index Index in constant pool to CONSTANT_Utf8
  98. */
  99. public Signature(int name_index, int length, int signature_index,
  100. ConstantPool constant_pool)
  101. {
  102. super(Constants.ATTR_SIGNATURE, name_index, length, constant_pool);
  103. this.signature_index = signature_index;
  104. }
  105. /**
  106. * Called by objects that are traversing the nodes of the tree implicitely
  107. * defined by the contents of a Java class. I.e., the hierarchy of methods,
  108. * fields, attributes, etc. spawns a tree of objects.
  109. *
  110. * @param v Visitor object
  111. */
  112. public void accept(Visitor v) {
  113. v.visitSignature(this);
  114. }
  115. /**
  116. * Dump source file attribute to file stream in binary format.
  117. *
  118. * @param file Output file stream
  119. * @throws IOException
  120. */
  121. public final void dump(DataOutputStream file) throws IOException
  122. {
  123. super.dump(file);
  124. file.writeShort(signature_index);
  125. }
  126. /**
  127. * @return Index in constant pool of source file name.
  128. */
  129. public final int getSignatureIndex() { return signature_index; }
  130. /**
  131. * @param Signature_index.
  132. */
  133. public final void setSignatureIndex(int signature_index) {
  134. this.signature_index = signature_index;
  135. }
  136. /**
  137. * @return GJ signature.
  138. */
  139. public final String getSignature() {
  140. ConstantUtf8 c = (ConstantUtf8)constant_pool.getConstant(signature_index,
  141. Constants.CONSTANT_Utf8);
  142. return c.getBytes();
  143. }
  144. /**
  145. * Extends ByteArrayInputStream to make 'unreading' chars possible.
  146. */
  147. private static final class MyByteArrayInputStream extends ByteArrayInputStream {
  148. MyByteArrayInputStream(String data) { super(data.getBytes()); }
  149. final int mark() { return pos; }
  150. final String getData() { return new String(buf); }
  151. final void reset(int p) { pos = p; }
  152. final void unread() { if(pos > 0) pos--; }
  153. }
  154. private static boolean identStart(int ch) {
  155. return ch == 'T' || ch == 'L';
  156. }
  157. private static boolean identPart(int ch) {
  158. return ch == '/' || ch == ';';
  159. }
  160. private static final void matchIdent(MyByteArrayInputStream in, StringBuffer buf) {
  161. int ch;
  162. if((ch = in.read()) == -1)
  163. throw new RuntimeException("Illegal signature: " + in.getData() +
  164. " no ident, reaching EOF");
  165. //System.out.println("return from ident:" + (char)ch);
  166. if(!identStart(ch)) {
  167. StringBuffer buf2 = new StringBuffer();
  168. int count = 1;
  169. while(Character.isJavaIdentifierPart((char)ch)) {
  170. buf2.append((char)ch);
  171. count++;
  172. ch = in.read();
  173. }
  174. if(ch == ':') { // Ok, formal parameter
  175. in.skip("Ljava/lang/Object".length());
  176. buf.append(buf2);
  177. ch = in.read();
  178. in.unread();
  179. //System.out.println("so far:" + buf2 + ":next:" +(char)ch);
  180. } else {
  181. for(int i=0; i < count; i++)
  182. in.unread();
  183. }
  184. return;
  185. }
  186. StringBuffer buf2 = new StringBuffer();
  187. ch = in.read();
  188. do {
  189. buf2.append((char)ch);
  190. ch = in.read();
  191. //System.out.println("within ident:"+ (char)ch);
  192. } while((ch != -1) && (Character.isJavaIdentifierPart((char)ch) || (ch == '/')));
  193. buf.append(buf2.toString().replace('/', '.'));
  194. //System.out.println("regular return ident:"+ (char)ch + ":" + buf2);
  195. if(ch != -1)
  196. in.unread();
  197. }
  198. private static final void matchGJIdent(MyByteArrayInputStream in,
  199. StringBuffer buf)
  200. {
  201. int ch;
  202. matchIdent(in, buf);
  203. ch = in.read();
  204. if((ch == '<') || ch == '(') { // Parameterized or method
  205. //System.out.println("Enter <");
  206. buf.append((char)ch);
  207. matchGJIdent(in, buf);
  208. while(((ch = in.read()) != '>') && (ch != ')')) { // List of parameters
  209. if(ch == -1)
  210. throw new RuntimeException("Illegal signature: " + in.getData() +
  211. " reaching EOF");
  212. //System.out.println("Still no >");
  213. buf.append(", ");
  214. in.unread();
  215. matchGJIdent(in, buf); // Recursive call
  216. }
  217. //System.out.println("Exit >");
  218. buf.append((char)ch);
  219. } else
  220. in.unread();
  221. ch = in.read();
  222. if(identStart(ch)) {
  223. in.unread();
  224. matchGJIdent(in, buf);
  225. } else if(ch == ')') {
  226. in.unread();
  227. return;
  228. } else if(ch != ';')
  229. throw new RuntimeException("Illegal signature: " + in.getData() + " read " +
  230. (char)ch);
  231. }
  232. public static String translate(String s) {
  233. //System.out.println("Sig:" + s);
  234. StringBuffer buf = new StringBuffer();
  235. matchGJIdent(new MyByteArrayInputStream(s), buf);
  236. return buf.toString();
  237. }
  238. public static final boolean isFormalParameterList(String s) {
  239. return s.startsWith("<") && (s.indexOf(':') > 0);
  240. }
  241. public static final boolean isActualParameterList(String s) {
  242. return s.startsWith("L") && s.endsWith(">;");
  243. }
  244. /**
  245. * @return String representation
  246. */
  247. public final String toString() {
  248. String s = getSignature();
  249. return "Signature(" + s + ")";
  250. }
  251. /**
  252. * @return deep copy of this attribute
  253. */
  254. public Attribute copy(ConstantPool constant_pool) {
  255. return (Signature)clone();
  256. }
  257. // =============================================
  258. // AMC extensions
  259. private ClassSignature classSig;
  260. private MethodTypeSignature methodSig;
  261. private FieldTypeSignature fieldSig;
  262. public ClassSignature asClassSignature() {
  263. if (classSig == null) {
  264. GenericSignatureParser parser = new GenericSignatureParser();
  265. classSig = parser.parseAsClassSignature(getSignature());
  266. }
  267. return classSig;
  268. }
  269. public MethodTypeSignature asMethodTypeSignature() {
  270. if (methodSig == null) {
  271. GenericSignatureParser parser = new GenericSignatureParser();
  272. methodSig = parser.parseAsMethodSignature(getSignature());
  273. }
  274. return methodSig;
  275. }
  276. public FieldTypeSignature asFieldTypeSignature() {
  277. if (fieldSig == null) {
  278. GenericSignatureParser parser = new GenericSignatureParser();
  279. fieldSig = parser.parseAsFieldSignature(getSignature());
  280. }
  281. return fieldSig;
  282. }
  283. /**
  284. * structure holding a parsed class signature
  285. */
  286. public static class ClassSignature {
  287. public FormalTypeParameter[] formalTypeParameters = new FormalTypeParameter[0];
  288. public ClassTypeSignature superclassSignature;
  289. public ClassTypeSignature[] superInterfaceSignatures = new ClassTypeSignature[0];
  290. public String toString() {
  291. StringBuffer ret = new StringBuffer();
  292. ret.append(formalTypeParameters.toString());
  293. ret.append(superclassSignature.toString());
  294. for (int i = 0; i < superInterfaceSignatures.length; i++) {
  295. ret.append(superInterfaceSignatures[i].toString());
  296. }
  297. return ret.toString();
  298. }
  299. }
  300. public static class MethodTypeSignature {
  301. public FormalTypeParameter[] formalTypeParameters = new FormalTypeParameter[0];
  302. public TypeSignature[] parameters = new TypeSignature[0];
  303. public TypeSignature returnType;
  304. public FieldTypeSignature[] throwsSignatures = new FieldTypeSignature[0];
  305. public MethodTypeSignature(
  306. FormalTypeParameter[] aFormalParameterList,
  307. TypeSignature[] aParameterList,
  308. TypeSignature aReturnType,
  309. FieldTypeSignature[] aThrowsSignatureList
  310. ) {
  311. this.formalTypeParameters = aFormalParameterList;
  312. this.parameters = aParameterList;
  313. this.returnType = aReturnType;
  314. this.throwsSignatures = aThrowsSignatureList;
  315. }
  316. public String toString() {
  317. StringBuffer sb = new StringBuffer();
  318. if (formalTypeParameters.length > 0) {
  319. sb.append("<");
  320. for (int i = 0; i < formalTypeParameters.length; i++) {
  321. sb.append(formalTypeParameters[i].toString());
  322. }
  323. sb.append(">");
  324. }
  325. sb.append("(");
  326. for (int i = 0; i < parameters.length; i++) {
  327. sb.append(parameters[i].toString());
  328. }
  329. sb.append(")");
  330. sb.append(returnType.toString());
  331. for (int i = 0; i < throwsSignatures.length; i++) {
  332. sb.append("^");
  333. sb.append(throwsSignatures[i].toString());
  334. }
  335. return sb.toString();
  336. }
  337. }
  338. /**
  339. * structure capturing a FormalTypeParameter from the Signature grammar
  340. */
  341. public static class FormalTypeParameter {
  342. public String identifier;
  343. public FieldTypeSignature classBound;
  344. public FieldTypeSignature[] interfaceBounds;
  345. public String toString() {
  346. StringBuffer ret = new StringBuffer();
  347. ret.append("T");
  348. ret.append(identifier);
  349. ret.append(":");
  350. ret.append(classBound.toString());
  351. for (int i = 0; i < interfaceBounds.length; i++) {
  352. ret.append(":");
  353. ret.append(interfaceBounds[i].toString());
  354. }
  355. return ret.toString();
  356. }
  357. }
  358. public static abstract class TypeSignature {
  359. public boolean isBaseType() { return false; }
  360. }
  361. public static class BaseTypeSignature extends TypeSignature {
  362. private String sig;
  363. public BaseTypeSignature(String aPrimitiveType) {
  364. sig = aPrimitiveType;
  365. }
  366. public boolean isBaseType() { return true; }
  367. public String toString() {
  368. return sig;
  369. }
  370. }
  371. public static abstract class FieldTypeSignature extends TypeSignature {
  372. public boolean isClassTypeSignature() { return false; }
  373. public boolean isTypeVariableSignature() { return false; }
  374. public boolean isArrayTypeSignature() { return false; }
  375. }
  376. public static class ClassTypeSignature extends FieldTypeSignature {
  377. public String classSignature;
  378. public SimpleClassTypeSignature outerType;
  379. public SimpleClassTypeSignature[] nestedTypes;
  380. public ClassTypeSignature(String sig,String identifier) {
  381. this.classSignature = sig;
  382. this.outerType = new SimpleClassTypeSignature(identifier);
  383. this.nestedTypes = new SimpleClassTypeSignature[0];
  384. }
  385. public ClassTypeSignature(String sig, SimpleClassTypeSignature outer, SimpleClassTypeSignature[] inners) {
  386. this.classSignature = sig;
  387. this.outerType = outer;
  388. this.nestedTypes = inners;
  389. }
  390. public boolean isClassTypeSignature() { return true; }
  391. public String toString() {
  392. return classSignature;
  393. }
  394. }
  395. public static class TypeVariableSignature extends FieldTypeSignature {
  396. public String typeVariableName;
  397. public TypeVariableSignature(String typeVarToken) {
  398. this.typeVariableName = typeVarToken.substring(1);
  399. }
  400. public boolean isTypeVariableSignature() { return true; }
  401. public String toString() {
  402. return "T" + typeVariableName + ";";
  403. }
  404. }
  405. public static class ArrayTypeSignature extends FieldTypeSignature {
  406. public TypeSignature typeSig;
  407. public ArrayTypeSignature(TypeSignature aTypeSig) {
  408. this.typeSig = aTypeSig;
  409. }
  410. public boolean isArrayTypeSignature() { return true; }
  411. public String toString() {
  412. return "[" + typeSig.toString();
  413. }
  414. }
  415. public static class SimpleClassTypeSignature {
  416. public String identifier;
  417. public TypeArgument[] typeArguments;
  418. public SimpleClassTypeSignature(String identifier) {
  419. this.identifier = identifier;
  420. this.typeArguments = new TypeArgument[0];
  421. }
  422. public SimpleClassTypeSignature(String identifier, TypeArgument[] args) {
  423. this.identifier = identifier;
  424. this.typeArguments = args;
  425. }
  426. public String toString() {
  427. StringBuffer sb = new StringBuffer();
  428. sb.append(identifier);
  429. if (typeArguments.length > 0) {
  430. sb.append("<");
  431. for (int i = 0; i < typeArguments.length; i++) {
  432. sb.append(typeArguments[i].toString());
  433. }
  434. sb.append(">");
  435. }
  436. return sb.toString();
  437. }
  438. }
  439. public static class TypeArgument {
  440. public boolean isWildcard = false;
  441. public boolean isPlus = false;
  442. public boolean isMinus = false;
  443. public FieldTypeSignature signature; // null if isWildcard
  444. public TypeArgument() {
  445. isWildcard = true;
  446. }
  447. public TypeArgument(boolean plus, boolean minus, FieldTypeSignature aSig) {
  448. this.isPlus = plus;
  449. this.isMinus = minus;
  450. this.signature = aSig;
  451. }
  452. public String toString() {
  453. if (isWildcard) return "*";
  454. StringBuffer sb = new StringBuffer();
  455. if (isPlus) sb.append("+");
  456. if (isMinus) sb.append("-");
  457. sb.append(signature.toString());
  458. return sb.toString();
  459. }
  460. }
  461. }