Skip to content

Commit 14fa2b5

Browse files
authored
Merge pull request #7332 from ccutrer/reified_parent_not_in_system_classloader
defined reified classes with their parent class's classloader as parent
2 parents 489cd92 + d7a87d4 commit 14fa2b5

File tree

5 files changed

+56
-1
lines changed

5 files changed

+56
-1
lines changed

core/src/main/java/org/jruby/RubyClass.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import org.jruby.compiler.impl.SkinnyMethodAdapter;
6161
import org.jruby.exceptions.RaiseException;
6262
import org.jruby.internal.runtime.methods.DynamicMethod;
63+
import org.jruby.java.codegen.MultiClassLoader;
6364
import org.jruby.java.codegen.RealClassGenerator;
6465
import org.jruby.java.codegen.Reified;
6566
import org.jruby.java.proxies.ConcreteJavaProxy;
@@ -90,6 +91,7 @@
9091
import org.jruby.util.ClassDefiningClassLoader;
9192
import org.jruby.util.CodegenUtils;
9293
import org.jruby.util.JavaNameMangler;
94+
import org.jruby.util.Loader;
9395
import org.jruby.util.OneShotClassLoader;
9496
import org.jruby.util.StringSupport;
9597
import org.jruby.util.collections.ConcurrentWeakHashMap;
@@ -1357,7 +1359,11 @@ public synchronized void reify(String classDumpDir, boolean useChildLoader) {
13571359
parentCL = (OneShotClassLoader) parentReified.getClassLoader();
13581360
} else {
13591361
if (useChildLoader) {
1360-
parentCL = new OneShotClassLoader(runtime.getJRubyClassLoader());
1362+
MultiClassLoader parentLoader = new MultiClassLoader(runtime.getJRubyClassLoader());
1363+
for(Loader cLoader : runtime.getInstanceConfig().getExtraLoaders()) {
1364+
parentLoader.addClassLoader(cLoader.getClassLoader());
1365+
}
1366+
parentCL = new OneShotClassLoader(parentLoader);
13611367
} else {
13621368
parentCL = runtime.getJRubyClassLoader();
13631369
}

rakelib/test.rake

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,21 @@ namespace :test do
2525
desc "Compile test code"
2626
task :compile do
2727
mkdir_p "test/target/test-classes"
28+
mkdir_p "test/target/test-classes-isolated/java_integration/fixtures/isolated"
29+
mkdir_p "test/target/test-interfaces-isolated/java_integration/fixtures/isolated"
30+
2831
classpath = %w[lib/jruby.jar test/target/junit.jar test/target/annotation-api.jar].join(File::PATH_SEPARATOR)
2932
# try detecting javac - so we use the same Java versions as we're running (JAVA_HOME) with :
3033
java_home = [ ENV_JAVA['java.home'], File.join(ENV_JAVA['java.home'], '..') ] # in case of jdk/jre
3134
javac = java_home.map { |home| File.expand_path('bin/javac', home) }.find { |javac| File.exist?(javac) } || 'javac'
3235
sh "#{javac} -cp #{classpath} -d test/target/test-classes #{Dir['spec/java_integration/fixtures/**/*.java'].to_a.join(' ')}"
36+
# move the objects that need to be in separate class loaders
37+
mv "test/target/test-classes/java_integration/fixtures/isolated/classes",
38+
"test/target/test-classes-isolated/java_integration/fixtures/isolated",
39+
force: true
40+
mv "test/target/test-classes/java_integration/fixtures/isolated/interfaces",
41+
"test/target/test-interfaces-isolated/java_integration/fixtures/isolated",
42+
force: true
3343
end
3444

3545
short_tests = ['jruby', 'mri']
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package java_integration.fixtures.isolated.classes;
2+
3+
public class GH7327Base {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package java_integration.fixtures.isolated.interfaces;
2+
3+
public interface GH7327Interface {
4+
}

spec/java_integration/reify/become_java_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,37 @@ class JRUBY5564; end
341341
expect(klass.load_class(a_class.get_name)).to eq(a_class)
342342
end
343343

344+
it "supports reifying concrete classes extending classes from an unrelated class loader" do
345+
isolated_class_path = File.expand_path('../../../../test/target/test-classes-isolated', __FILE__)
346+
classloader = java.net.URLClassLoader.new([java.net.URL.new("file://#{isolated_class_path}/")].to_java(java.net.URL))
347+
JRuby.runtime.instance_config.add_loader(classloader)
348+
349+
# we can reference it from JRuby, because it was added to the JRuby loader above
350+
class GH7327 < Java::Java_integrationFixturesIsolatedClasses::GH7327Base; end
351+
# it should have created a unique Java class
352+
expect(GH7327.become_java!).not_to eq(Java::Java_integrationFixturesIsolatedClasses::GH7327Base.java_class)
353+
ensure
354+
JRuby.runtime.instance_config.extra_loaders.clear
355+
end
356+
357+
it "supports reifying concrete classes extending classes and implementing interfaces from multiple unrelated class loaders" do
358+
isolated_class_path = File.expand_path('../../../../test/target/test-classes-isolated', __FILE__)
359+
classloader1 = java.net.URLClassLoader.new([java.net.URL.new("file://#{isolated_class_path}/")].to_java(java.net.URL))
360+
JRuby.runtime.instance_config.add_loader(classloader1)
361+
isolated_interface_path = File.expand_path('../../../../test/target/test-interfaces-isolated', __FILE__)
362+
classloader2 = java.net.URLClassLoader.new([java.net.URL.new("file://#{isolated_interface_path}/")].to_java(java.net.URL))
363+
JRuby.runtime.instance_config.add_loader(classloader2)
364+
365+
# we can reference them from JRuby, because they were added to the JRuby loader above
366+
class GH7327WithInterface < Java::Java_integrationFixturesIsolatedClasses::GH7327Base
367+
include Java::Java_integrationFixturesIsolatedInterfaces::GH7327Interface
368+
end
369+
# it should have created a unique Java class
370+
expect(GH7327WithInterface.become_java!).not_to eq(Java::Java_integrationFixturesIsolatedClasses::GH7327Base.java_class)
371+
ensure
372+
JRuby.runtime.instance_config.extra_loaders.clear
373+
end
374+
344375
class ReifiedSample
345376
def hello; 'Sayonara from Ruby' end
346377
java_signature "java.lang.String ahoy()"

0 commit comments

Comments
 (0)