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.7KB

14 years ago
14 years ago
4 years ago
14 years ago
4 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  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 (Method m : methods) {
  48. if (m.getName().equals(methodname)) {
  49. return m;
  50. }
  51. }
  52. return null;
  53. }
  54. protected Field getField(JavaClass cl, String fieldname) {
  55. Field[] fields = cl.getFields();
  56. for (Field f : fields) {
  57. if (f.getName().equals(fieldname)) {
  58. return f;
  59. }
  60. }
  61. return null;
  62. }
  63. protected boolean wipe(String name) {
  64. return new File("testdata" + File.separator + name).delete();
  65. }
  66. protected boolean wipe(String dir, String name) {
  67. boolean b = wipe(dir + File.separator + name);
  68. String[] files = new File(dir).list();
  69. if (files == null || files.length == 0) {
  70. new File(dir).delete(); // Why does this not succeed? stupid thing
  71. }
  72. return b;
  73. }
  74. public SyntheticRepository createRepos(String cpentry) {
  75. ClassPath cp = new ClassPath("testdata" + File.separator + cpentry + File.pathSeparator
  76. + System.getProperty("java.class.path"));
  77. return SyntheticRepository.getInstance(cp);
  78. }
  79. protected Attribute[] findAttribute(String name, JavaClass clazz) {
  80. Attribute[] all = clazz.getAttributes();
  81. List<Attribute> chosenAttrsList = new ArrayList<>();
  82. for (Attribute attribute : all) {
  83. if (verbose)
  84. System.err.println("Attribute: " + attribute.getName());
  85. if (attribute.getName().equals(name))
  86. chosenAttrsList.add(attribute);
  87. }
  88. return chosenAttrsList.toArray(new Attribute[] {});
  89. }
  90. protected Attribute findAttribute(String name, Attribute[] all) {
  91. List<Attribute> chosenAttrsList = new ArrayList<>();
  92. for (Attribute attribute : all) {
  93. if (verbose)
  94. System.err.println("Attribute: " + attribute.getName());
  95. if (attribute.getName().equals(name))
  96. chosenAttrsList.add(attribute);
  97. }
  98. assertTrue("Should be one match: " + chosenAttrsList.size(), chosenAttrsList.size() == 1);
  99. return chosenAttrsList.get(0);
  100. }
  101. protected String dumpAnnotations(AnnotationGen[] as) {
  102. StringBuilder result = new StringBuilder();
  103. result.append("[");
  104. for (int i = 0; i < as.length; i++) {
  105. AnnotationGen annotation = as[i];
  106. result.append(annotation.toShortString());
  107. if (i + 1 < as.length)
  108. result.append(",");
  109. }
  110. result.append("]");
  111. return result.toString();
  112. }
  113. protected String dumpAnnotations(List<AnnotationGen> as) {
  114. StringBuilder result = new StringBuilder();
  115. result.append("[");
  116. for (int i = 0; i < as.size(); i++) {
  117. AnnotationGen annotation = as.get(i);
  118. result.append(annotation.toShortString());
  119. if (i + 1 < as.size())
  120. result.append(",");
  121. }
  122. result.append("]");
  123. return result.toString();
  124. }
  125. protected String dumpAttributes(Attribute[] as) {
  126. StringBuilder result = new StringBuilder();
  127. result.append("AttributeArray:[");
  128. for (int i = 0; i < as.length; i++) {
  129. Attribute attr = as[i];
  130. result.append(attr.toString());
  131. if (i + 1 < as.length)
  132. result.append(",");
  133. }
  134. result.append("]");
  135. return result.toString();
  136. }
  137. public AnnotationGen createFruitAnnotation(ConstantPool cp, String aFruit, boolean visibility) {
  138. SimpleElementValue evg = new SimpleElementValue(ElementValue.STRING, cp, aFruit);
  139. NameValuePair nvGen = new NameValuePair("fruit", evg, cp);
  140. ObjectType t = new ObjectType("SimpleStringAnnotation");
  141. List<NameValuePair> elements = new ArrayList<>();
  142. elements.add(nvGen);
  143. return new AnnotationGen(t, elements, visibility, cp);
  144. }
  145. public Attribute getAttribute(Attribute[] attrs, byte tag) {
  146. for (Attribute attr: attrs) {
  147. if (attr.getTag() == tag) {
  148. return attr;
  149. }
  150. }
  151. return null;
  152. }
  153. public Attribute getAttribute(Attribute[] attrs, String name) {
  154. for (Attribute attr: attrs) {
  155. if (attr.getName().equals(name)) {
  156. return attr;
  157. }
  158. }
  159. return null;
  160. }
  161. }