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.

BcelTestCase.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* *******************************************************************
  2. * Copyright (c) 2004 - 2016 IBM, VMware, Contributors
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Andy Clement - initial implementation {date}
  11. * ******************************************************************/
  12. package org.aspectj.apache.bcel.classfile.tests;
  13. import java.io.File;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import org.aspectj.apache.bcel.classfile.Attribute;
  17. import org.aspectj.apache.bcel.classfile.ConstantPool;
  18. import org.aspectj.apache.bcel.classfile.Field;
  19. import org.aspectj.apache.bcel.classfile.JavaClass;
  20. import org.aspectj.apache.bcel.classfile.Method;
  21. import org.aspectj.apache.bcel.classfile.annotation.AnnotationGen;
  22. import org.aspectj.apache.bcel.classfile.annotation.ElementValue;
  23. import org.aspectj.apache.bcel.classfile.annotation.NameValuePair;
  24. import org.aspectj.apache.bcel.classfile.annotation.SimpleElementValue;
  25. import org.aspectj.apache.bcel.generic.ObjectType;
  26. import org.aspectj.apache.bcel.util.ClassPath;
  27. import org.aspectj.apache.bcel.util.SyntheticRepository;
  28. import junit.framework.TestCase;
  29. /**
  30. * Super class for the Java5 tests, includes various helper methods.
  31. */
  32. public abstract class BcelTestCase extends TestCase {
  33. private boolean verbose = false;
  34. protected File createTestdataFile(String name) {
  35. return new File("testdata" + File.separator + name);
  36. }
  37. protected JavaClass getClassFromJar(String clazzname) throws ClassNotFoundException {
  38. SyntheticRepository repos = createRepos("testcode.jar");
  39. return repos.loadClass(clazzname);
  40. }
  41. protected JavaClass getClassFromJava8Jar(String clazzname) throws ClassNotFoundException {
  42. SyntheticRepository repos = createRepos("java8testcode.jar");
  43. return repos.loadClass(clazzname);
  44. }
  45. protected Method getMethod(JavaClass cl, String methodname) {
  46. Method[] methods = cl.getMethods();
  47. for (int i = 0; i < methods.length; i++) {
  48. Method m = methods[i];
  49. if (m.getName().equals(methodname)) {
  50. return m;
  51. }
  52. }
  53. return null;
  54. }
  55. protected Field getField(JavaClass cl, String fieldname) {
  56. Field[] fields = cl.getFields();
  57. for (int i = 0; i < fields.length; i++) {
  58. Field f = fields[i];
  59. if (f.getName().equals(fieldname)) {
  60. return f;
  61. }
  62. }
  63. return null;
  64. }
  65. protected boolean wipe(String name) {
  66. return new File("testdata" + File.separator + name).delete();
  67. }
  68. protected boolean wipe(String dir, String name) {
  69. boolean b = wipe(dir + File.separator + name);
  70. String[] files = new File(dir).list();
  71. if (files == null || files.length == 0) {
  72. new File(dir).delete(); // Why does this not succeed? stupid thing
  73. }
  74. return b;
  75. }
  76. public SyntheticRepository createRepos(String cpentry) {
  77. ClassPath cp = new ClassPath("testdata" + File.separator + cpentry + File.pathSeparator
  78. + System.getProperty("java.class.path"));
  79. return SyntheticRepository.getInstance(cp);
  80. }
  81. protected Attribute[] findAttribute(String name, JavaClass clazz) {
  82. Attribute[] all = clazz.getAttributes();
  83. List<Attribute> chosenAttrsList = new ArrayList<Attribute>();
  84. for (int i = 0; i < all.length; i++) {
  85. if (verbose)
  86. System.err.println("Attribute: " + all[i].getName());
  87. if (all[i].getName().equals(name))
  88. chosenAttrsList.add(all[i]);
  89. }
  90. return chosenAttrsList.toArray(new Attribute[] {});
  91. }
  92. protected Attribute findAttribute(String name, Attribute[] all) {
  93. List<Attribute> chosenAttrsList = new ArrayList<Attribute>();
  94. for (int i = 0; i < all.length; i++) {
  95. if (verbose)
  96. System.err.println("Attribute: " + all[i].getName());
  97. if (all[i].getName().equals(name))
  98. chosenAttrsList.add(all[i]);
  99. }
  100. assertTrue("Should be one match: " + chosenAttrsList.size(), chosenAttrsList.size() == 1);
  101. return chosenAttrsList.get(0);
  102. }
  103. protected String dumpAnnotations(AnnotationGen[] as) {
  104. StringBuffer result = new StringBuffer();
  105. result.append("[");
  106. for (int i = 0; i < as.length; i++) {
  107. AnnotationGen annotation = as[i];
  108. result.append(annotation.toShortString());
  109. if (i + 1 < as.length)
  110. result.append(",");
  111. }
  112. result.append("]");
  113. return result.toString();
  114. }
  115. protected String dumpAnnotations(List<AnnotationGen> as) {
  116. StringBuffer result = new StringBuffer();
  117. result.append("[");
  118. for (int i = 0; i < as.size(); i++) {
  119. AnnotationGen annotation = as.get(i);
  120. result.append(annotation.toShortString());
  121. if (i + 1 < as.size())
  122. result.append(",");
  123. }
  124. result.append("]");
  125. return result.toString();
  126. }
  127. protected String dumpAttributes(Attribute[] as) {
  128. StringBuffer result = new StringBuffer();
  129. result.append("AttributeArray:[");
  130. for (int i = 0; i < as.length; i++) {
  131. Attribute attr = as[i];
  132. result.append(attr.toString());
  133. if (i + 1 < as.length)
  134. result.append(",");
  135. }
  136. result.append("]");
  137. return result.toString();
  138. }
  139. public AnnotationGen createFruitAnnotation(ConstantPool cp, String aFruit, boolean visibility) {
  140. SimpleElementValue evg = new SimpleElementValue(ElementValue.STRING, cp, aFruit);
  141. NameValuePair nvGen = new NameValuePair("fruit", evg, cp);
  142. ObjectType t = new ObjectType("SimpleStringAnnotation");
  143. List<NameValuePair> elements = new ArrayList<NameValuePair>();
  144. elements.add(nvGen);
  145. return new AnnotationGen(t, elements, visibility, cp);
  146. }
  147. public Attribute getAttribute(Attribute[] attrs, byte tag) {
  148. for (Attribute attr: attrs) {
  149. if (attr.getTag() == tag) {
  150. return attr;
  151. }
  152. }
  153. return null;
  154. }
  155. public Attribute getAttribute(Attribute[] attrs, String name) {
  156. for (Attribute attr: attrs) {
  157. if (attr.getName().equals(name)) {
  158. return attr;
  159. }
  160. }
  161. return null;
  162. }
  163. }