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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* *******************************************************************
  2. * Copyright (c) 2004 IBM
  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 junit.framework.TestCase;
  17. import org.aspectj.apache.bcel.classfile.Attribute;
  18. import org.aspectj.apache.bcel.classfile.ConstantPool;
  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.NameValuePair;
  23. import org.aspectj.apache.bcel.classfile.annotation.ElementValue;
  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. /**
  29. * Super class for the Java5 tests, includes various helper methods.
  30. */
  31. public class BcelTestCase extends TestCase {
  32. private boolean verbose = false;
  33. protected File createTestdataFile(String name) {
  34. return new File("testdata" + File.separator + name);
  35. }
  36. protected JavaClass getClassFromJar(String clazzname) throws ClassNotFoundException {
  37. SyntheticRepository repos = createRepos("testcode.jar");
  38. return repos.loadClass(clazzname);
  39. }
  40. protected Method getMethod(JavaClass cl, String methodname) {
  41. Method[] methods = cl.getMethods();
  42. for (int i = 0; i < methods.length; i++) {
  43. Method m = methods[i];
  44. if (m.getName().equals(methodname)) {
  45. return m;
  46. }
  47. }
  48. return null;
  49. }
  50. protected boolean wipe(String name) {
  51. return new File("testdata" + File.separator + name).delete();
  52. }
  53. protected boolean wipe(String dir, String name) {
  54. boolean b = wipe(dir + File.separator + name);
  55. String[] files = new File(dir).list();
  56. if (files == null || files.length == 0) {
  57. new File(dir).delete(); // Why does this not succeed? stupid thing
  58. }
  59. return b;
  60. }
  61. public SyntheticRepository createRepos(String cpentry) {
  62. ClassPath cp = new ClassPath("testdata" + File.separator + cpentry + File.pathSeparator
  63. + System.getProperty("java.class.path"));
  64. return SyntheticRepository.getInstance(cp);
  65. }
  66. protected Attribute[] findAttribute(String name, JavaClass clazz) {
  67. Attribute[] all = clazz.getAttributes();
  68. List<Attribute> chosenAttrsList = new ArrayList<Attribute>();
  69. for (int i = 0; i < all.length; i++) {
  70. if (verbose)
  71. System.err.println("Attribute: " + all[i].getName());
  72. if (all[i].getName().equals(name))
  73. chosenAttrsList.add(all[i]);
  74. }
  75. return chosenAttrsList.toArray(new Attribute[] {});
  76. }
  77. protected Attribute findAttribute(String name, Attribute[] all) {
  78. List<Attribute> chosenAttrsList = new ArrayList<Attribute>();
  79. for (int i = 0; i < all.length; i++) {
  80. if (verbose)
  81. System.err.println("Attribute: " + all[i].getName());
  82. if (all[i].getName().equals(name))
  83. chosenAttrsList.add(all[i]);
  84. }
  85. assertTrue("Should be one match: " + chosenAttrsList.size(), chosenAttrsList.size() == 1);
  86. return chosenAttrsList.get(0);
  87. }
  88. protected String dumpAnnotations(AnnotationGen[] as) {
  89. StringBuffer result = new StringBuffer();
  90. result.append("[");
  91. for (int i = 0; i < as.length; i++) {
  92. AnnotationGen annotation = as[i];
  93. result.append(annotation.toShortString());
  94. if (i + 1 < as.length)
  95. result.append(",");
  96. }
  97. result.append("]");
  98. return result.toString();
  99. }
  100. protected String dumpAnnotations(List<AnnotationGen> as) {
  101. StringBuffer result = new StringBuffer();
  102. result.append("[");
  103. for (int i = 0; i < as.size(); i++) {
  104. AnnotationGen annotation = as.get(i);
  105. result.append(annotation.toShortString());
  106. if (i + 1 < as.size())
  107. result.append(",");
  108. }
  109. result.append("]");
  110. return result.toString();
  111. }
  112. protected String dumpAttributes(Attribute[] as) {
  113. StringBuffer result = new StringBuffer();
  114. result.append("AttributeArray:[");
  115. for (int i = 0; i < as.length; i++) {
  116. Attribute attr = as[i];
  117. result.append(attr.toString());
  118. if (i + 1 < as.length)
  119. result.append(",");
  120. }
  121. result.append("]");
  122. return result.toString();
  123. }
  124. public AnnotationGen createFruitAnnotation(ConstantPool cp, String aFruit, boolean visibility) {
  125. SimpleElementValue evg = new SimpleElementValue(ElementValue.STRING, cp, aFruit);
  126. NameValuePair nvGen = new NameValuePair("fruit", evg, cp);
  127. ObjectType t = new ObjectType("SimpleStringAnnotation");
  128. List<NameValuePair> elements = new ArrayList<NameValuePair>();
  129. elements.add(nvGen);
  130. return new AnnotationGen(t, elements, visibility, cp);
  131. }
  132. }