Skip to content

Commit 577d13a

Browse files
committedJan 29, 2018
Force DynamicMethod.name to always be non-null and final.
1 parent 26005d0 commit 577d13a

21 files changed

+45
-190
lines changed
 

‎core/src/main/java/org/jruby/RubyModule.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -1215,11 +1215,6 @@ public final void addMethodAtBootTimeOnly(String name, DynamicMethod method) {
12151215
method.setImplementationClass(methodLocation);
12161216
}
12171217

1218-
// if method does not have a name already, set it
1219-
if (method.getName() == null) {
1220-
method.setName(name);
1221-
}
1222-
12231218
methodLocation.getMethodsForWrite().put(name, method);
12241219

12251220
getRuntime().addProfiledMethod(name, method);
@@ -2048,7 +2043,7 @@ private DynamicMethod createProcMethod(String name, Visibility visibility, RubyP
20482043
// a method definition.
20492044
block.getBody().getStaticScope().makeArgumentScope();
20502045

2051-
return new ProcMethod(this, proc, visibility);
2046+
return new ProcMethod(this, proc, visibility, name);
20522047
}
20532048

20542049
public IRubyObject name() {

‎core/src/main/java/org/jruby/RubyStruct.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,9 @@ public static RubyClass newInstance(IRubyObject recv, IRubyObject[] args, Block
225225
final String memberName = args[i].asJavaString();
226226
// if we are storing a name as well, index is one too high for values
227227
final int index = (name == null && !nilName) ? i : i - 1;
228-
newStruct.addMethod(memberName, new Accessor(newStruct, index));
229-
newStruct.addMethod(memberName + '=', new Mutator(newStruct, index));
228+
newStruct.addMethod(memberName, new Accessor(newStruct, memberName, index));
229+
String nameAsgn = memberName + '=';
230+
newStruct.addMethod(nameAsgn, new Mutator(newStruct, nameAsgn, index));
230231
}
231232

232233
if (block.isGiven()) {
@@ -765,8 +766,8 @@ public IRubyObject initialize_copy(IRubyObject arg) {
765766
private static class Accessor extends DynamicMethod {
766767
private final int index;
767768

768-
public Accessor(RubyClass newStruct, int index) {
769-
super(newStruct, Visibility.PUBLIC);
769+
public Accessor(RubyClass newStruct, String name, int index) {
770+
super(newStruct, Visibility.PUBLIC, name);
770771
this.index = index;
771772
}
772773

@@ -783,15 +784,15 @@ public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule claz
783784

784785
@Override
785786
public DynamicMethod dup() {
786-
return new Accessor((RubyClass) getImplementationClass(), index);
787+
return new Accessor((RubyClass) getImplementationClass(), name, index);
787788
}
788789
}
789790

790791
private static class Mutator extends DynamicMethod {
791792
private final int index;
792793

793-
public Mutator(RubyClass newStruct, int index) {
794-
super(newStruct, Visibility.PUBLIC);
794+
public Mutator(RubyClass newStruct, String name, int index) {
795+
super(newStruct, Visibility.PUBLIC, name);
795796
this.index = index;
796797
}
797798

@@ -808,7 +809,7 @@ public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule claz
808809

809810
@Override
810811
public DynamicMethod dup() {
811-
return new Accessor((RubyClass) getImplementationClass(), index);
812+
return new Mutator((RubyClass) getImplementationClass(), name, index);
812813
}
813814
}
814815

‎core/src/main/java/org/jruby/ext/ffi/AbstractInvoker.java

-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ protected AbstractInvoker(Ruby runtime, RubyClass klass, int arity, MemoryIO io)
8080
public IRubyObject attach(ThreadContext context, IRubyObject obj, IRubyObject methodName) {
8181

8282
DynamicMethod m = createDynamicMethod(obj.getSingletonClass());
83-
m.setName(methodName.asJavaString());
8483
obj.getSingletonClass().addMethod(methodName.asJavaString(), m);
8584
if (obj instanceof RubyModule) {
8685
((RubyModule) obj).addMethod(methodName.asJavaString(), m);

‎core/src/main/java/org/jruby/ext/ffi/Pointer.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public boolean isKindOf(IRubyObject obj, RubyModule type) {
4242
Pointer nullPointer = new Pointer(runtime, pointerClass, new NullMemoryIO(runtime));
4343
pointerClass.setConstant("NULL", nullPointer);
4444

45-
runtime.getNilClass().addMethod("to_ptr", new NilToPointerMethod(runtime.getNilClass(), nullPointer));
45+
runtime.getNilClass().addMethod("to_ptr", new NilToPointerMethod(runtime.getNilClass(), nullPointer, "to_ptr"));
4646

4747
return pointerClass;
4848
}
@@ -197,8 +197,8 @@ private static final class NilToPointerMethod extends DynamicMethod {
197197
private static final Arity ARITY = Arity.NO_ARGUMENTS;
198198
private final Pointer nullPointer;
199199

200-
private NilToPointerMethod(RubyModule implementationClass, Pointer nullPointer) {
201-
super(implementationClass, Visibility.PUBLIC);
200+
private NilToPointerMethod(RubyModule implementationClass, Pointer nullPointer, String name) {
201+
super(implementationClass, Visibility.PUBLIC, name);
202202
this.nullPointer = nullPointer;
203203
}
204204

‎core/src/main/java/org/jruby/ext/ffi/jffi/DefaultMethod.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ public class DefaultMethod extends DynamicMethod implements CacheableMethod {
2323
protected final Arity arity;
2424
protected final Function function;
2525

26-
2726
public DefaultMethod(RubyModule implementationClass, Function function,
2827
Signature signature, NativeInvoker defaultInvoker) {
29-
super(implementationClass, Visibility.PUBLIC);
28+
super(implementationClass, Visibility.PUBLIC, defaultInvoker.getName());
3029
this.arity = Arity.fixed(signature.getParameterCount());
3130
this.function = function;
3231
this.defaultInvoker = defaultInvoker;
@@ -83,7 +82,6 @@ private synchronized NativeInvoker tryCompilation() {
8382

8483
NativeInvoker invoker = getJITHandle().compile(getImplementationClass(), function, signature, getName());
8584
if (invoker != null) {
86-
invoker.setName(getName());
8785
compiledInvoker = invoker;
8886
getImplementationClass().invalidateCacheDescendants();
8987
return compiledInvoker;

‎core/src/main/java/org/jruby/ext/ffi/jffi/NativeInvoker.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ abstract public class NativeInvoker extends DynamicMethod {
2323

2424

2525
public NativeInvoker(RubyModule implementationClass, com.kenai.jffi.Function function, Signature signature) {
26-
super(implementationClass, Visibility.PUBLIC);
26+
super(implementationClass, Visibility.PUBLIC, "ffi"+function.getFunctionAddress());
2727
this.arity = Arity.fixed(signature.getParameterCount());
2828
this.function = function;
2929
this.signature = signature;

‎core/src/main/java/org/jruby/internal/runtime/methods/AliasMethod.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class AliasMethod extends DynamicMethod {
4545
private String oldName;
4646

4747
public AliasMethod(RubyModule implementationClass, DynamicMethod oldMethod, String oldName) {
48-
super(implementationClass, oldMethod.getVisibility());
48+
super(implementationClass, oldMethod.getVisibility(), oldName);
4949

5050
this.oldName = oldName;
5151
this.oldMethod = oldMethod;

‎core/src/main/java/org/jruby/internal/runtime/methods/DelegatingDynamicMethod.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public abstract class DelegatingDynamicMethod extends DynamicMethod {
4141
protected final DynamicMethod delegate;
4242

4343
public DelegatingDynamicMethod(DynamicMethod delegate) {
44-
super(delegate.getImplementationClass(), delegate.getVisibility());
44+
super(delegate.getImplementationClass(), delegate.getVisibility(), delegate.getName());
4545
this.delegate = delegate;
4646
}
4747

@@ -168,11 +168,6 @@ public String getName() {
168168
return delegate.getName(); //To change body of overridden methods use File | Settings | File Templates.
169169
}
170170

171-
@Override
172-
public void setName(String name) {
173-
delegate.setName(name); //To change body of overridden methods use File | Settings | File Templates.
174-
}
175-
176171
@Override
177172
public boolean isNotImplemented() {
178173
return delegate.isNotImplemented(); //To change body of overridden methods use File | Settings | File Templates.

‎core/src/main/java/org/jruby/internal/runtime/methods/DynamicMethod.java

+15-25
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,13 @@ public abstract class DynamicMethod {
6565
/** Flags for builtin, notimpl, etc */
6666
protected byte flags;
6767
/** The simple, base name this method was defined under. May be null.*/
68-
protected String name;
68+
protected final String name;
6969
/** An arbitrarily-typed "method handle" for use by compilers and call sites */
7070
protected Object handle;
7171

7272
private static final int BUILTIN_FLAG = 0x1;
7373
private static final int NOTIMPL_FLAG = 0x2;
7474

75-
/**
76-
* Base constructor for dynamic method handles.
77-
*
78-
* @param implementationClass The class to which this method will be
79-
* immediately bound
80-
* @param visibility The visibility assigned to this method
81-
* pre/post invocation logic.
82-
*/
83-
protected DynamicMethod(RubyModule implementationClass, Visibility visibility) {
84-
assert implementationClass != null;
85-
init(implementationClass, visibility);
86-
}
87-
8875
/**
8976
* Base constructor for dynamic method handles with names.
9077
*
@@ -94,15 +81,19 @@ protected DynamicMethod(RubyModule implementationClass, Visibility visibility) {
9481
* @param name The simple name of this method
9582
*/
9683
protected DynamicMethod(RubyModule implementationClass, Visibility visibility, String name) {
97-
this(implementationClass, visibility);
84+
assert implementationClass != null;
85+
assert name != null;
9886
this.name = name;
87+
init(implementationClass, visibility);
9988
}
10089

10190
/**
10291
* A no-arg constructor used only by the UndefinedMethod subclass and
10392
* CompiledMethod handles. instanceof assertions make sure this is so.
10493
*/
105-
protected DynamicMethod() {
94+
protected DynamicMethod(String name) {
95+
this.visibility = (byte) Visibility.PUBLIC.ordinal();
96+
this.name = name;
10697
// assert (this instanceof UndefinedMethod ||
10798
// this instanceof CompiledMethod ||
10899
// this instanceof );
@@ -491,15 +482,6 @@ public String getName() {
491482
return name;
492483
}
493484

494-
/**
495-
* Set the base name for this method.
496-
*
497-
* @param name the name to set
498-
*/
499-
public void setName(String name) {
500-
this.name = name;
501-
}
502-
503485
/**
504486
* Get the "handle" associated with this DynamicMethod.
505487
*
@@ -568,4 +550,12 @@ public CallConfiguration getCallConfig() {
568550
@Deprecated
569551
public void setCallConfig(CallConfiguration callConfig) {
570552
}
553+
554+
/**
555+
* @deprecated Use {@link DynamicMethod#DynamicMethod(RubyModule, Visibility, String)}
556+
*/
557+
@Deprecated
558+
protected DynamicMethod(RubyModule implementationClass, Visibility visibility) {
559+
this(implementationClass, visibility, "(anonymous)");
560+
}
571561
}

‎core/src/main/java/org/jruby/internal/runtime/methods/HandleMethod.java

+3-27
Original file line numberDiff line numberDiff line change
@@ -76,31 +76,7 @@ public class HandleMethod extends DynamicMethod implements MethodArgs2, Cloneabl
7676
public HandleMethod(
7777
RubyModule implementationClass,
7878
Visibility visibility,
79-
long encodedSignature,
80-
boolean builtin,
81-
boolean notImplemented,
82-
String parameterDesc,
83-
final int min,
84-
final int max,
85-
final Callable<MethodHandle>... makers) {
86-
87-
super(implementationClass, visibility);
88-
this.signature = Signature.decode(encodedSignature);
89-
this.builtin = builtin;
90-
this.notImplemented = notImplemented;
91-
this.parameterDesc = parameterDesc;
92-
this.min = min;
93-
this.max = max;
94-
this.maker0 = makers[0];
95-
this.maker1 = makers[1];
96-
this.maker2 = makers[2];
97-
this.maker3 = makers[3];
98-
this.maker4 = makers[4];
99-
}
100-
101-
public HandleMethod(
102-
RubyModule implementationClass,
103-
Visibility visibility,
79+
String name,
10480
long encodedSignature,
10581
boolean builtin,
10682
boolean notImplemented,
@@ -113,7 +89,7 @@ public HandleMethod(
11389
final Callable<MethodHandle> maker3,
11490
final Callable<MethodHandle> maker4) {
11591

116-
super(implementationClass, visibility);
92+
super(implementationClass, visibility, name);
11793
this.signature = Signature.decode(encodedSignature);
11894
this.builtin = builtin;
11995
this.notImplemented = notImplemented;
@@ -284,7 +260,7 @@ public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule claz
284260

285261
@Override
286262
public DynamicMethod dup() {
287-
return new HandleMethod(implementationClass, getVisibility(), signature.encode(), builtin, notImplemented, parameterDesc, min, max, maker0, maker1, maker2, maker3, maker4);
263+
return new HandleMethod(implementationClass, getVisibility(), name, signature.encode(), builtin, notImplemented, parameterDesc, min, max, maker0, maker1, maker2, maker3, maker4);
288264
}
289265

290266
@Override

‎core/src/main/java/org/jruby/internal/runtime/methods/InvokeDynamicMethodFactory.java

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public DynamicMethod getAnnotatedMethod(final RubyModule implementationClass, fi
120120
return new HandleMethod(
121121
implementationClass,
122122
desc1.anno.visibility(),
123+
desc1.name,
123124
(min == max) ?
124125
org.jruby.runtime.Signature.from(min, 0, 0, 0, 0, org.jruby.runtime.Signature.Rest.NONE, -1).encode() :
125126
org.jruby.runtime.Signature.OPTIONAL.encode(),

‎core/src/main/java/org/jruby/internal/runtime/methods/JavaMethod.java

-2
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ public JavaMethod(RubyModule implementationClass, Visibility visibility, String
9292
super(implementationClass, visibility, name);
9393
}
9494

95-
protected JavaMethod() {}
96-
9795
public void init(RubyModule implementationClass, Arity arity, Visibility visibility, StaticScope staticScope) {
9896
this.staticScope = staticScope;
9997
setArity(arity);

‎core/src/main/java/org/jruby/internal/runtime/methods/MethodMethod.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class MethodMethod extends DynamicMethod {
4949
*/
5050
public MethodMethod(RubyModule implementationClass, RubyUnboundMethod method, Visibility visibility) {
5151
// FIXME: set up a CallConfiguration for this
52-
super(implementationClass, visibility);
52+
super(implementationClass, visibility, method.getMethodName());
5353
this.method = method;
5454
}
5555

‎core/src/main/java/org/jruby/internal/runtime/methods/MethodMissingMethod.java

-49
This file was deleted.

‎core/src/main/java/org/jruby/internal/runtime/methods/NullMethod.java

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class NullMethod extends DynamicMethod {
4848
* Constructor for the one NullMethod instance.
4949
*/
5050
private NullMethod() {
51+
super("null");
5152
}
5253

5354
/**

‎core/src/main/java/org/jruby/internal/runtime/methods/ProcMethod.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ public class ProcMethod extends DynamicMethod implements PositionAware, IRMethod
5353
* Constructor for ProcMethod.
5454
* @param visibility
5555
*/
56-
public ProcMethod(RubyModule implementationClass, RubyProc proc, Visibility visibility) {
56+
public ProcMethod(RubyModule implementationClass, RubyProc proc, Visibility visibility, String name) {
5757
// FIXME: set up a call configuration for this
58-
super(implementationClass, visibility);
58+
super(implementationClass, visibility, name);
5959
this.proc = proc;
6060
}
6161

@@ -64,7 +64,7 @@ public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule klaz
6464
}
6565

6666
public DynamicMethod dup() {
67-
return new ProcMethod(getImplementationClass(), proc, getVisibility());
67+
return new ProcMethod(getImplementationClass(), proc, getVisibility(), name);
6868
}
6969

7070
// TODO: Push isSame up to DynamicMethod to simplify general equality

‎core/src/main/java/org/jruby/internal/runtime/methods/UndefinedMethod.java

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class UndefinedMethod extends DynamicMethod {
4747
* Constructor for the one UndefinedMethod instance.
4848
*/
4949
private UndefinedMethod() {
50+
super("(undefined)");
5051
}
5152

5253
/**

‎core/src/main/java/org/jruby/internal/runtime/methods/WrapperMethod.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class WrapperMethod extends DynamicMethod {
4848
* @param visibility
4949
*/
5050
public WrapperMethod(RubyModule implementationClass, DynamicMethod method, Visibility visibility) {
51-
super(implementationClass, visibility);
51+
super(implementationClass, visibility, method.getName() );
5252
this.method = method;
5353
}
5454

‎core/src/main/java/org/jruby/java/util/BlankSlateWrapper.java

-52
This file was deleted.

‎core/src/main/java/org/jruby/javasupport/Java.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ public final IRubyObject call(ThreadContext context, IRubyObject self, RubyModul
10921092
static final class ProcToInterface extends org.jruby.internal.runtime.methods.DynamicMethod {
10931093

10941094
ProcToInterface(final RubyClass singletonClass) {
1095-
super(singletonClass, PUBLIC);
1095+
super(singletonClass, PUBLIC, "call");
10961096
}
10971097

10981098
@Override // method_missing impl :

‎core/src/main/java/org/jruby/runtime/Helpers.java

+1
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ private static class MethodMissingMethod extends DynamicMethod {
383383
private final Visibility lastVisibility;
384384

385385
public MethodMissingMethod(DynamicMethod delegate, Visibility lastVisibility, CallType lastCallStatus) {
386+
super(delegate.getImplementationClass(), lastVisibility, delegate.getName());
386387
this.delegate = delegate;
387388
this.lastCallStatus = lastCallStatus;
388389
this.lastVisibility = lastVisibility;

0 commit comments

Comments
 (0)
Please sign in to comment.