Skip to content

Instantly share code, notes, and snippets.

@gissuebot
Created July 7, 2014 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gissuebot/2985eaec86c63de8a69e to your computer and use it in GitHub Desktop.
Save gissuebot/2985eaec86c63de8a69e to your computer and use it in GitHub Desktop.
Migrated attachment for Guice issue 94, comment 2
Index: src/com/google/inject/ConstructionContext.java
===================================================================
--- src/com/google/inject/ConstructionContext.java (revision 321)
+++ src/com/google/inject/ConstructionContext.java (working copy)
@@ -23,6 +23,8 @@
import java.util.ArrayList;
import java.util.List;
+import com.google.inject.util.GuiceClassLoader;
+
/**
* Context of a dependency construction. Used to manage circular references.
*
@@ -80,7 +82,7 @@
= new DelegatingInvocationHandler<T>();
invocationHandlers.add(invocationHandler);
- Object object = Proxy.newProxyInstance(expectedType.getClassLoader(),
+ Object object = Proxy.newProxyInstance(new GuiceClassLoader(expectedType),
new Class[] { expectedType }, invocationHandler);
return expectedType.cast(object);
}
Index: src/com/google/inject/ProxyFactory.java
===================================================================
--- src/com/google/inject/ProxyFactory.java (revision 321)
+++ src/com/google/inject/ProxyFactory.java (working copy)
@@ -16,6 +16,7 @@
package com.google.inject;
+import com.google.inject.util.GuiceEnhancer;
import com.google.inject.util.GuiceFastClass;
import com.google.inject.util.GuiceNamingPolicy;
import com.google.inject.util.Objects;
@@ -140,7 +141,7 @@
}
// Create the proxied class.
- Enhancer enhancer = new Enhancer();
+ Enhancer enhancer = new GuiceEnhancer();
enhancer.setSuperclass(declaringClass);
enhancer.setUseCache(false); // We do enough caching.
enhancer.setCallbackFilter(new CallbackFilter() {
Index: src/com/google/inject/util/GuiceEnhancer.java
===================================================================
--- src/com/google/inject/util/GuiceEnhancer.java (revision 0)
+++ src/com/google/inject/util/GuiceEnhancer.java (revision 0)
@@ -0,0 +1,41 @@
+/**
+ * Copyright (C) 2007 Stuart McCulloch
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.inject.util;
+
+import net.sf.cglib.proxy.Enhancer;
+
+/**
+ * Provide custom classloading for Guice
+ *
+ * @author stuart.mcculloch@jayway.net (Stuart McCulloch)
+ */
+public class GuiceEnhancer extends Enhancer {
+ private ClassLoader loader;
+
+ public void setSuperclass(Class superclass) {
+ loader = new GuiceClassLoader(superclass);
+ super.setSuperclass(superclass);
+ }
+
+ protected ClassLoader getDefaultClassLoader() {
+ if (loader != null) {
+ return loader;
+ } else {
+ return super.getDefaultClassLoader();
+ }
+ }
+}
Property changes on: src/com/google/inject/util/GuiceEnhancer.java
___________________________________________________________________
Name: svn:eol-style
+ native
Index: src/com/google/inject/util/GuiceFastClass.java
===================================================================
--- src/com/google/inject/util/GuiceFastClass.java (revision 321)
+++ src/com/google/inject/util/GuiceFastClass.java (working copy)
@@ -26,7 +26,7 @@
public class GuiceFastClass {
public static FastClass create(Class type) {
- return create(type.getClassLoader(), type);
+ return create(new GuiceClassLoader(type), type);
}
public static FastClass create(ClassLoader loader, Class type) {
Index: src/com/google/inject/util/GuiceClassLoader.java
===================================================================
--- src/com/google/inject/util/GuiceClassLoader.java (revision 0)
+++ src/com/google/inject/util/GuiceClassLoader.java (revision 0)
@@ -0,0 +1,39 @@
+/**
+ * Copyright (C) 2007 Stuart McCulloch
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.inject.util;
+
+/**
+ * Provide bridge between Guice and Application classloaders
+ *
+ * @author stuart.mcculloch@jayway.net (Stuart McCulloch)
+ */
+public class GuiceClassLoader extends ClassLoader {
+ private ClassLoader loader;
+
+ public GuiceClassLoader(Class type) {
+ super(GuiceClassLoader.class.getClassLoader());
+ loader = type.getClassLoader();
+ }
+
+ protected Class findClass(String name) throws ClassNotFoundException {
+ if (null == loader || name.startsWith("com.google.inject.")) {
+ return super.findClass(name);
+ } else {
+ return loader.loadClass(name);
+ }
+ }
+}
Property changes on: src/com/google/inject/util/GuiceClassLoader.java
___________________________________________________________________
Name: svn:eol-style
+ native
Index: guice.bnd
===================================================================
--- guice.bnd (revision 0)
+++ guice.bnd (revision 0)
@@ -0,0 +1,3 @@
+# ignore this package as it is not available
+Import-Package: !com.google.inject.asm.util, *
+Export-Package: *
Index: build.xml
===================================================================
--- build.xml (revision 321)
+++ build.xml (working copy)
@@ -24,7 +24,24 @@
</jarjar>
</target>
- <target name="dist" depends="jar, javadoc"
+ <target name="bnd" depends="jar">
+ <mkdir dir="${build.dir}/osgi"/>
+ <java classname="aQute.bnd.main.bnd">
+ <arg value="wrap"/>
+ <arg value="-output"/>
+ <arg value="${build.dir}/osgi/guice-${version}.jar"/>
+ <arg value="-properties"/>
+ <arg value="guice.bnd"/>
+ <arg value="${build.dir}/dist/guice-${version}.jar"/>
+ <classpath>
+ <pathelement location="lib/build/bnd-0.0.130.jar"/>
+ <pathelement path="${java.class.path}"/>
+ </classpath>
+ </java>
+ <move toDir="${build.dir}/dist" file="${build.dir}/osgi/guice-${version}.jar"/>
+ </target>
+
+ <target name="dist" depends="bnd, javadoc"
description="Build entire distribution.">
<ant antfile="servlet/build.xml" target="jar" inheritAll="false"/>
<ant antfile="spring/build.xml" target="jar" inheritAll="false"/>
@@ -57,7 +74,7 @@
</target>
<target name="test.dist"
- depends="jar, test.compile"
+ depends="bnd, test.compile"
description="Execute JUnit tests against distribution jar.">
<java fork="true"
classname="junit.textui.TestRunner"
Index: lib/build/bnd-0.0.130.jar
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Property changes on: lib/build/bnd-0.0.130.jar
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment