Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

BcelTestCase.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.ConstantPool;
  20. import org.aspectj.apache.bcel.classfile.annotation.AnnotationGen;
  21. import org.aspectj.apache.bcel.classfile.annotation.ElementNameValuePairGen;
  22. import org.aspectj.apache.bcel.classfile.annotation.ElementValueGen;
  23. import org.aspectj.apache.bcel.classfile.annotation.SimpleElementValueGen;
  24. import org.aspectj.apache.bcel.generic.ObjectType;
  25. import org.aspectj.apache.bcel.util.ClassPath;
  26. import org.aspectj.apache.bcel.util.SyntheticRepository;
  27. import junit.framework.TestCase;
  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(
  63. "testdata"+File.separator+cpentry+File.pathSeparator+
  64. System.getProperty("java.class.path"));
  65. return SyntheticRepository.getInstance(cp);
  66. }
  67. protected Attribute[] findAttribute(String name, JavaClass clazz) {
  68. Attribute[] all = clazz.getAttributes();
  69. List chosenAttrsList = new ArrayList();
  70. for (int i = 0; i < all.length; i++) {
  71. if (verbose) System.err.println("Attribute: "+all[i].getName());
  72. if (all[i].getName().equals(name)) chosenAttrsList.add(all[i]);
  73. }
  74. return (Attribute[])chosenAttrsList.toArray(new Attribute[]{});
  75. }
  76. protected Attribute findAttribute(String name, Attribute[] all) {
  77. List chosenAttrsList = new ArrayList();
  78. for (int i = 0; i < all.length; i++) {
  79. if (verbose) System.err.println("Attribute: "+all[i].getName());
  80. if (all[i].getName().equals(name)) chosenAttrsList.add(all[i]);
  81. }
  82. assertTrue("Should be one match: "+chosenAttrsList.size(),chosenAttrsList.size()==1);
  83. return (Attribute)chosenAttrsList.get(0);
  84. }
  85. protected String dumpAnnotations(AnnotationGen[] as) {
  86. StringBuffer result = new StringBuffer();
  87. result.append("[");
  88. for (int i = 0; i < as.length; i++) {
  89. AnnotationGen annotation = as[i];
  90. result.append(annotation.toShortString());
  91. if (i+1<as.length) result.append(",");
  92. }
  93. result.append("]");
  94. return result.toString();
  95. }
  96. protected String dumpAttributes(Attribute[] as) {
  97. StringBuffer result = new StringBuffer();
  98. result.append("AttributeArray:[");
  99. for (int i = 0; i < as.length; i++) {
  100. Attribute attr = as[i];
  101. result.append(attr.toString());
  102. if (i+1<as.length) result.append(",");
  103. }
  104. result.append("]");
  105. return result.toString();
  106. }
  107. public AnnotationGen createFruitAnnotation(ConstantPool cp, String aFruit, boolean visibility) {
  108. SimpleElementValueGen evg = new SimpleElementValueGen(ElementValueGen.STRING,cp,aFruit);
  109. ElementNameValuePairGen nvGen = new ElementNameValuePairGen("fruit",evg,cp);
  110. ObjectType t = new ObjectType("SimpleStringAnnotation");
  111. List elements = new ArrayList();
  112. elements.add(nvGen);
  113. return new AnnotationGen(t,elements,visibility,cp);
  114. }
  115. }