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.

DefaultWeavingContext.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. * David Knibb initial implementation
  11. *******************************************************************************/
  12. package org.aspectj.weaver.loadtime;
  13. import java.io.IOException;
  14. import java.net.URL;
  15. import java.util.Enumeration;
  16. import java.util.List;
  17. import org.aspectj.weaver.bcel.BcelWeakClassLoaderReference;
  18. import org.aspectj.weaver.loadtime.definition.Definition;
  19. import org.aspectj.weaver.tools.Trace;
  20. import org.aspectj.weaver.tools.TraceFactory;
  21. import org.aspectj.weaver.tools.WeavingAdaptor;
  22. /**
  23. * Use in non-OSGi environment
  24. *
  25. * @author David Knibb
  26. */
  27. public class DefaultWeavingContext implements IWeavingContext {
  28. protected BcelWeakClassLoaderReference loaderRef;
  29. private String shortName;
  30. private static Trace trace = TraceFactory.getTraceFactory().getTrace(DefaultWeavingContext.class);
  31. /**
  32. * Construct a new WeavingContext to use the specified ClassLoader This is the constructor which should be used.
  33. *
  34. * @param loader
  35. */
  36. public DefaultWeavingContext(ClassLoader loader) {
  37. super();
  38. this.loaderRef = new BcelWeakClassLoaderReference(loader);
  39. }
  40. /**
  41. * Same as ClassLoader.getResources()
  42. */
  43. public Enumeration<URL> getResources(String name) throws IOException {
  44. return getClassLoader().getResources(name);
  45. }
  46. /**
  47. * @return null as we are not in an OSGi environment (therefore no bundles)
  48. */
  49. public String getBundleIdFromURL(URL url) {
  50. return "";
  51. }
  52. /**
  53. * @return classname@hashcode
  54. */
  55. public String getClassLoaderName() {
  56. ClassLoader loader = getClassLoader();
  57. return ((loader != null) ? loader.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(loader))
  58. : "null");
  59. }
  60. public ClassLoader getClassLoader() {
  61. return loaderRef.getClassLoader();
  62. }
  63. /**
  64. * @return filename
  65. */
  66. public String getFile(URL url) {
  67. return url.getFile();
  68. }
  69. /**
  70. * @return unqualifiedclassname@hashcode
  71. */
  72. public String getId() {
  73. if (shortName == null) {
  74. shortName = getClassLoaderName().replace('$', '.');
  75. int index = shortName.lastIndexOf(".");
  76. if (index != -1) {
  77. shortName = shortName.substring(index + 1);
  78. }
  79. }
  80. return shortName;
  81. }
  82. public String getSuffix() {
  83. return getClassLoaderName();
  84. }
  85. public boolean isLocallyDefined(String classname) {
  86. String asResource = classname.replace('.', '/').concat(".class");
  87. ClassLoader loader = getClassLoader();
  88. URL localURL = loader.getResource(asResource);
  89. if (localURL == null) {
  90. return false;
  91. }
  92. boolean isLocallyDefined = true;
  93. ClassLoader parent = loader.getParent();
  94. if (parent != null) {
  95. URL parentURL = parent.getResource(asResource);
  96. if (localURL.equals(parentURL)) {
  97. isLocallyDefined = false;
  98. }
  99. }
  100. return isLocallyDefined;
  101. }
  102. /**
  103. * Simply call weaving adaptor back to parse aop.xml
  104. *
  105. * @param adaptor
  106. * @param loader
  107. */
  108. public List<Definition> getDefinitions(final ClassLoader loader, final WeavingAdaptor adaptor) {
  109. if (trace.isTraceEnabled()) {
  110. trace.enter("getDefinitions", this, new Object[] { "goo", adaptor });
  111. }
  112. List<Definition> definitions = ((ClassLoaderWeavingAdaptor) adaptor).parseDefinitions(loader);
  113. if (trace.isTraceEnabled()) {
  114. trace.exit("getDefinitions", definitions);
  115. }
  116. return definitions;
  117. }
  118. }