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.

Aj.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*******************************************************************************
  2. * Copyright (c) 2005 Contributors.
  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://eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Alexandre Vasseur initial implementation
  11. *******************************************************************************/
  12. package org.aspectj.weaver.loadtime;
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.lang.reflect.Method;
  16. import java.util.Map;
  17. import java.util.WeakHashMap;
  18. import org.aspectj.weaver.tools.WeavingAdaptor;
  19. /**
  20. * Adapter between the generic class pre processor interface and the AspectJ weaver
  21. * Load time weaving consistency relies on Bcel.setRepository
  22. *
  23. * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
  24. */
  25. public class Aj implements ClassPreProcessor {
  26. /**
  27. * Initialization
  28. */
  29. public void initialize() {
  30. ;
  31. }
  32. /**
  33. * Weave
  34. *
  35. * @param className
  36. * @param bytes
  37. * @param loader
  38. * @return
  39. */
  40. public byte[] preProcess(String className, byte[] bytes, ClassLoader loader) {
  41. //TODO AV needs to doc that
  42. if (loader == null || className == null) {
  43. // skip boot loader or null classes (hibernate)
  44. return bytes;
  45. }
  46. try {
  47. byte[] weaved = WeaverContainer.getWeaver(loader).weaveClass(className, bytes);
  48. //FIXME AV make dump optionnal and configurable
  49. __dump(className, weaved);
  50. return weaved;
  51. } catch (Throwable t) {
  52. t.printStackTrace();
  53. return bytes;
  54. }
  55. }
  56. /**
  57. * Cache of weaver
  58. * There is one weaver per classloader
  59. */
  60. static class WeaverContainer {
  61. private static Map weavingAdaptors = new WeakHashMap();
  62. static WeavingAdaptor getWeaver(ClassLoader loader) {
  63. synchronized (weavingAdaptors) {
  64. WeavingAdaptor weavingAdaptor = (WeavingAdaptor) weavingAdaptors.get(loader);
  65. if (weavingAdaptor == null) {
  66. weavingAdaptor = new ClassLoaderWeavingAdaptor(loader);
  67. weavingAdaptors.put(loader, weavingAdaptor);
  68. }
  69. return weavingAdaptor;
  70. }
  71. }
  72. }
  73. static void defineClass(ClassLoader loader, String name, byte[] bytes) {
  74. try {
  75. //TODO av protection domain, and optimize
  76. Method defineClass = ClassLoader.class.getDeclaredMethod(
  77. "defineClass", new Class[]{
  78. String.class, bytes.getClass(), int.class, int.class
  79. }
  80. );
  81. defineClass.setAccessible(true);
  82. defineClass.invoke(
  83. loader, new Object[]{
  84. name,
  85. bytes,
  86. new Integer(0),
  87. new Integer(bytes.length)
  88. }
  89. );
  90. } catch (Throwable t) {
  91. t.printStackTrace();
  92. }
  93. }
  94. /**
  95. * Dump the given bytcode in _dump/...
  96. *
  97. * @param name
  98. * @param b
  99. * @throws Throwable
  100. */
  101. static void __dump(String name, byte[] b) throws Throwable {
  102. if (true) return;//FIXME AV have an option
  103. String className = name.replace('.', '/');
  104. final File dir;
  105. if (className.indexOf('/') > 0) {
  106. dir = new File("_dump" + File.separator + className.substring(0, className.lastIndexOf('/')));
  107. } else {
  108. dir = new File("_dump");
  109. }
  110. dir.mkdirs();
  111. String fileName = "_dump" + File.separator + className + ".class";
  112. FileOutputStream os = new FileOutputStream(fileName);
  113. os.write(b);
  114. os.close();
  115. }
  116. }