PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/atlassian-plugins-osgi-testrunner-bundle/src/main/java/com/atlassian/plugins/osgi/test/asm/BundleClassVisitor.java

https://bitbucket.org/Adaptavist/atlassian-plugins-osgi-testrunner-parent
Java | 211 lines | 174 code | 33 blank | 4 comment | 20 complexity | b21c5a00864fca62cb6e90a0b4737391 MD5 | raw file
  1. package com.atlassian.plugins.osgi.test.asm;
  2. import java.io.InputStream;
  3. import java.net.URL;
  4. import java.net.URLClassLoader;
  5. import java.security.PrivateKey;
  6. import java.util.Set;
  7. import com.atlassian.plugins.osgi.test.AtlassianPluginsTestRunner;
  8. import org.apache.commons.io.IOUtils;
  9. import org.junit.Test;
  10. import org.junit.runner.RunWith;
  11. import org.objectweb.asm.*;
  12. import org.objectweb.asm.commons.EmptyVisitor;
  13. import org.osgi.framework.Bundle;
  14. import org.springframework.osgi.util.BundleDelegatingClassLoader;
  15. import junit.framework.TestCase;
  16. /**
  17. * @since version
  18. */
  19. public class BundleClassVisitor extends EmptyVisitor
  20. {
  21. private final Bundle bundle;
  22. private boolean isWiredTest;
  23. private boolean isTestClass;
  24. private boolean inITPackage;
  25. private Set<Class<?>> unitTests;
  26. private Set<Class<?>> itTests;
  27. private String normalClassName;
  28. private URL myUrl;
  29. private URL[] allUrls;
  30. public BundleClassVisitor(Bundle bundle, URL url, URL[] urls, Set<Class<?>> unitTests, Set<Class<?>> itTests)
  31. {
  32. this.bundle = bundle;
  33. this.unitTests = unitTests;
  34. this.itTests = itTests;
  35. this.myUrl = url;
  36. this.allUrls = urls;
  37. this.isWiredTest = false;
  38. this.isTestClass = false;
  39. this.inITPackage = false;
  40. }
  41. @Override
  42. public void visit(final int version, final int access, final String name, final String signature, final String superName, final String[] interfaces)
  43. {
  44. this.normalClassName = normalize(name);
  45. if (normalClassName.startsWith("it."))
  46. {
  47. inITPackage = true;
  48. }
  49. isTestClass = normalize(superName).equals(TestCase.class.getName());
  50. if (!isTestClass)
  51. {
  52. isTestClass = isSublassOf(superName, TestCase.class.getName());
  53. }
  54. }
  55. @Override
  56. public void visitEnd()
  57. {
  58. if (!isWiredTest && isTestClass)
  59. {
  60. try
  61. {
  62. Class<?> theClass = bundle.loadClass(normalClassName);
  63. if (inITPackage)
  64. {
  65. itTests.add(theClass);
  66. }
  67. else
  68. {
  69. unitTests.add(theClass);
  70. }
  71. }
  72. catch (Throwable t)
  73. {
  74. System.err.println("Error loading class from bundle: " + bundle.getSymbolicName() + " class: " + normalClassName);
  75. System.err.println("is it in a split package?");
  76. }
  77. }
  78. }
  79. @Override
  80. public AnnotationVisitor visitAnnotation(String annoName, boolean isVisible)
  81. {
  82. String normalName = normalize(annoName);
  83. if (RunWith.class.getName().equals(normalName))
  84. {
  85. return new RunWithAnnotationVisitor();
  86. }
  87. return null;
  88. }
  89. @Override
  90. public FieldVisitor visitField(int i, String s, String s1, String s2, Object o)
  91. {
  92. return null;
  93. }
  94. @Override
  95. public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature, final String[] exceptions)
  96. {
  97. return new TestMethodVisitor();
  98. }
  99. private boolean isSublassOf(String superName, String classToExtend)
  100. {
  101. boolean isSubclass = false;
  102. if (normalize(superName).equals("java.lang.Object"))
  103. {
  104. return isSubclass;
  105. }
  106. String path = superName.replace('.', '/');
  107. InputStream is = null;
  108. try
  109. {
  110. URL superUrl = bundle.getEntry(path + ".class");
  111. if (null != superUrl)
  112. {
  113. is = superUrl.openStream();
  114. }
  115. if (null != is)
  116. {
  117. ClassReader classReader = new ClassReader(is);
  118. isSubclass = normalize(classReader.getSuperName()).equals(classToExtend);
  119. if (!isSubclass)
  120. {
  121. isSubclass = isSublassOf(classReader.getSuperName(), classToExtend);
  122. }
  123. }
  124. }
  125. catch (Exception e)
  126. {
  127. //don't care
  128. }
  129. finally
  130. {
  131. IOUtils.closeQuietly(is);
  132. }
  133. return isSubclass;
  134. }
  135. static String normalize(String name)
  136. {
  137. if (name == null)
  138. {
  139. return null;
  140. }
  141. if (name.startsWith("L") && name.endsWith(";"))
  142. {
  143. name = name.substring(1, name.length() - 1);
  144. }
  145. if (name.endsWith(".class"))
  146. {
  147. name = name.substring(0, name.length() - ".class".length());
  148. }
  149. return name.replace('/', '.');
  150. }
  151. private class RunWithAnnotationVisitor extends EmptyVisitor
  152. {
  153. @Override
  154. public void visit(String name, Object value)
  155. {
  156. if (value instanceof Type)
  157. {
  158. Type type = (Type) value;
  159. if (AtlassianPluginsTestRunner.class.getName().equals(normalize(type.getInternalName())))
  160. {
  161. isWiredTest = true;
  162. }
  163. }
  164. }
  165. }
  166. private class TestMethodVisitor extends EmptyVisitor
  167. {
  168. @Override
  169. public AnnotationVisitor visitAnnotation(String name, boolean visible)
  170. {
  171. if (Test.class.getName().equals(normalize(name)))
  172. {
  173. isTestClass = true;
  174. }
  175. return null;
  176. }
  177. }
  178. }