Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions core/src/main/java/org/jruby/RubyModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -1938,9 +1938,7 @@ public final void addMethodInternal(ThreadContext context, String name, DynamicM
putMethod(context, name, method);
}

synchronized (getRuntime().getHierarchyLock()) {
invalidateCacheDescendants(context);
}
invalidateMethodCache(context);
}

public void removeMethod(ThreadContext context, String id) {
Expand Down Expand Up @@ -2376,6 +2374,17 @@ public void invalidateCacheDescendants(ThreadContext context) {
methodInvalidator.invalidateAll(invalidators);
}

/**
* Invalidate the method cache of this class and all descendants under lock.
*
* @param context
*/
public void invalidateMethodCache(ThreadContext context) {
synchronized (getRuntime().getHierarchyLock()) {
invalidateCacheDescendants(context);
}
}

public static class InvalidatorList<T> extends ArrayList<T> implements RubyClass.BiConsumerIgnoresSecond<RubyClass> {
public InvalidatorList(int size) {
super(size);
Expand Down
9 changes: 9 additions & 0 deletions core/src/main/java/org/jruby/anno/AnnotationBinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,15 @@ public void processType(TypeElement cd) {
out.print(classAndSubs.stream().map(s -> quote(s)).collect(Collectors.joining(", ")));
mappings.forEach((javaName, rubyName) -> out.print(", " + quote(javaName) + ", " + quote(rubyName) + ""));
out.println(");");

// invalidateMethodCache if not booting core
out.println(" if (!core) {");
out.println(" cls.invalidateMethodCache(context);");
if (hasMeta || hasModule) {
out.println(" singletonClass.invalidateMethodCache(context);");
}
out.println(" }");

out.println(" }");

// write out a static initializer for frame names, so it only fires once
Expand Down
46 changes: 46 additions & 0 deletions core/src/test/java/org/jruby/test/TestRubyModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.jruby.test;

import org.jruby.Ruby;
import org.jruby.api.Create;
import org.jruby.exceptions.NoMethodError;
import org.jruby.ext.bigdecimal.BigDecimalLibrary;
import org.jruby.ext.bigdecimal.RubyBigDecimal;

public class TestRubyModule extends junit.framework.TestCase {

Ruby runtime;

public TestRubyModule(String testName) {
super(testName);
}

@Override
protected void setUp() throws Exception {
super.setUp();
runtime = Ruby.newInstance();

}

@Override
protected void tearDown() throws Exception {
super.tearDown();
runtime.tearDown();
}

public void testKernelBigDecimalMethodMissingCache() throws Throwable {
var context = runtime.getCurrentContext();

try {
runtime.getObject().callMethod("BigDecimal", Create.newString(context, "1.0"));
} catch (NoMethodError nme) {
assertTrue(nme.getMessage().contains("BigDecimal"));
}

new BigDecimalLibrary().load(runtime, false);

var result = runtime.getObject().callMethod("BigDecimal", Create.newString(context, "1.0"));

assertTrue(result instanceof RubyBigDecimal);
assertEquals(1.0, result.convertToFloat().getValue());
}
}