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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 org.aspectj.apache.bcel.classfile.Attribute;
  17. import org.aspectj.apache.bcel.classfile.JavaClass;
  18. import org.aspectj.apache.bcel.classfile.Method;
  19. import org.aspectj.apache.bcel.classfile.annotation.Annotation;
  20. import org.aspectj.apache.bcel.generic.ConstantPoolGen;
  21. import org.aspectj.apache.bcel.generic.ObjectType;
  22. import org.aspectj.apache.bcel.generic.annotation.AnnotationGen;
  23. import org.aspectj.apache.bcel.generic.annotation.ElementNameValuePairGen;
  24. import org.aspectj.apache.bcel.generic.annotation.ElementValueGen;
  25. import org.aspectj.apache.bcel.generic.annotation.SimpleElementValueGen;
  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 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 Method getMethod(JavaClass cl,String methodname) {
  42. Method[] methods = cl.getMethods();
  43. for (int i = 0; i < methods.length; i++) {
  44. Method m = methods[i];
  45. if (m.getName().equals(methodname)) {
  46. return m;
  47. }
  48. }
  49. return null;
  50. }
  51. protected boolean wipe(String name) {
  52. return new File("testdata"+File.separator+name).delete();
  53. }
  54. protected boolean wipe(String dir, String name) {
  55. boolean b = wipe(dir+File.separator+name);
  56. String[] files = new File(dir).list();
  57. if (files==null || files.length==0) {
  58. new File(dir).delete(); // Why does this not succeed? stupid thing
  59. }
  60. return b;
  61. }
  62. public SyntheticRepository createRepos(String cpentry) {
  63. ClassPath cp = new ClassPath(
  64. "testdata"+File.separator+cpentry+File.pathSeparator+
  65. System.getProperty("java.class.path"));
  66. return SyntheticRepository.getInstance(cp);
  67. }
  68. protected Attribute[] findAttribute(String name, JavaClass clazz) {
  69. Attribute[] all = clazz.getAttributes();
  70. List chosenAttrsList = new ArrayList();
  71. for (int i = 0; i < all.length; i++) {
  72. if (verbose) System.err.println("Attribute: "+all[i].getName());
  73. if (all[i].getName().equals(name)) chosenAttrsList.add(all[i]);
  74. }
  75. return (Attribute[])chosenAttrsList.toArray(new Attribute[]{});
  76. }
  77. protected Attribute findAttribute(String name, Attribute[] all) {
  78. List chosenAttrsList = new ArrayList();
  79. for (int i = 0; i < all.length; i++) {
  80. if (verbose) System.err.println("Attribute: "+all[i].getName());
  81. if (all[i].getName().equals(name)) chosenAttrsList.add(all[i]);
  82. }
  83. assertTrue("Should be one match: "+chosenAttrsList.size(),chosenAttrsList.size()==1);
  84. return (Attribute)chosenAttrsList.get(0);
  85. }
  86. protected String dumpAnnotations(Annotation[] as) {
  87. StringBuffer result = new StringBuffer();
  88. result.append("[");
  89. for (int i = 0; i < as.length; i++) {
  90. Annotation annotation = as[i];
  91. result.append(annotation.toShortString());
  92. if (i+1<as.length) result.append(",");
  93. }
  94. result.append("]");
  95. return result.toString();
  96. }
  97. protected String dumpAnnotations(AnnotationGen[] as) {
  98. StringBuffer result = new StringBuffer();
  99. result.append("[");
  100. for (int i = 0; i < as.length; i++) {
  101. AnnotationGen annotation = as[i];
  102. result.append(annotation.toShortString());
  103. if (i+1<as.length) result.append(",");
  104. }
  105. result.append("]");
  106. return result.toString();
  107. }
  108. protected String dumpAttributes(Attribute[] as) {
  109. StringBuffer result = new StringBuffer();
  110. result.append("AttributeArray:[");
  111. for (int i = 0; i < as.length; i++) {
  112. Attribute attr = as[i];
  113. result.append(attr.toString());
  114. if (i+1<as.length) result.append(",");
  115. }
  116. result.append("]");
  117. return result.toString();
  118. }
  119. public AnnotationGen createFruitAnnotation(ConstantPoolGen cp, String aFruit, boolean visibility) {
  120. SimpleElementValueGen evg = new SimpleElementValueGen(ElementValueGen.STRING,cp,aFruit);
  121. ElementNameValuePairGen nvGen = new ElementNameValuePairGen("fruit",evg,cp);
  122. ObjectType t = new ObjectType("SimpleStringAnnotation");
  123. List elements = new ArrayList();
  124. elements.add(nvGen);
  125. return new AnnotationGen(t,elements,visibility,cp);
  126. }
  127. }