Skip to content

Commit

Permalink
GROOVY-11293: ensure isVargsMethod initialized before return
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jan 22, 2024
1 parent 6b8915f commit 6153dfd
Show file tree
Hide file tree
Showing 26 changed files with 96 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public abstract class GeneratedMetaMethod extends MetaMethod {
private final Class returnType;

public GeneratedMetaMethod(String name, CachedClass declaringClass, Class returnType, Class[] parameters) {
super(parameters);
this.name = name;
this.declaringClass = declaringClass;
this.returnType = returnType;
nativeParamTypes = parameters;
}

@Override
Expand Down
47 changes: 24 additions & 23 deletions src/main/java/org/codehaus/groovy/reflection/ParameterTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
import java.lang.reflect.Array;

public class ParameterTypes {
protected volatile Class[] nativeParamTypes;
protected volatile CachedClass[] parameterTypes;

protected boolean isVargsMethod;
private volatile CachedClass[] parameterTypes;
private volatile Class[] nativeParamTypes;
private boolean isVargsMethod;

public ParameterTypes() {
}
Expand All @@ -52,8 +52,8 @@ public ParameterTypes(String[] pt) {
}
}

public ParameterTypes(CachedClass[] parameterTypes) {
setParametersTypes(parameterTypes);
public ParameterTypes(CachedClass[] pt) {
setParametersTypes(pt);
}

protected final void setParametersTypes(CachedClass[] pt) {
Expand All @@ -65,25 +65,23 @@ public CachedClass[] getParameterTypes() {
if (parameterTypes == null) {
getParametersTypes0();
}

return parameterTypes;
}

private synchronized void getParametersTypes0() {
if (parameterTypes != null)
return;

var npt = nativeParamTypes == null ? getPT() : nativeParamTypes;
if (npt.length == 0) {
nativeParamTypes = MetaClassHelper.EMPTY_TYPE_ARRAY;
setParametersTypes(CachedClass.EMPTY_ARRAY);
} else {
CachedClass[] pt = new CachedClass[npt.length];
for (int i = 0; i != npt.length; ++i)
pt[i] = ReflectionCache.getCachedClass(npt[i]);

nativeParamTypes = npt;
setParametersTypes(pt);
if (parameterTypes == null) {
var npt = nativeParamTypes == null ? getPT() : nativeParamTypes;
if (npt.length == 0) {
nativeParamTypes = MetaClassHelper.EMPTY_TYPE_ARRAY;
setParametersTypes(CachedClass.EMPTY_ARRAY);
} else {
CachedClass[] pt = new CachedClass[npt.length];
for (int i = 0; i != npt.length; ++i) {
pt[i] = ReflectionCache.getCachedClass(npt[i]);
}
nativeParamTypes = npt;
setParametersTypes(pt);
}
}
}

Expand Down Expand Up @@ -114,11 +112,14 @@ protected Class[] getPT() {
}

public boolean isVargsMethod() {
if (parameterTypes == null) {
getParametersTypes0(); // GROOVY-11293
}
return isVargsMethod;
}

public boolean isVargsMethod(Object[] arguments) {
if (isVargsMethod) {
if (isVargsMethod()) {
int aCount = arguments.length;
int pCount = parameterTypes.length;
if (aCount > pCount || aCount == pCount-1) { // too many or too few?
Expand Down Expand Up @@ -156,7 +157,7 @@ public Object[] correctArguments(Object[] arguments) {

var pt = getParameterTypes();
if (pt.length == 1 && arguments.length == 0) {
if (!isVargsMethod) return MetaClassHelper.ARRAY_WITH_NULL;
if (!isVargsMethod()) return MetaClassHelper.ARRAY_WITH_NULL;
return new Object[]{Array.newInstance(pt[0].getTheClass().getComponentType(), 0)};
}

Expand Down Expand Up @@ -230,7 +231,7 @@ public boolean isValidMethod(Class[] argumentTypes) {
CachedClass[] pt = getParameterTypes();
final int nArguments = argumentTypes.length, nParameters = pt.length, nthParameter = nParameters - 1;

if (isVargsMethod && nArguments >= nthParameter)
if (isVargsMethod() && nArguments >= nthParameter)
return isValidVargsMethod(argumentTypes, pt, nthParameter);
else if (nArguments == nParameters)
return isValidExactMethod(argumentTypes, pt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
import java.lang.reflect.Modifier;

public abstract class NumberNumberMetaMethod extends CallSiteAwareMetaMethod {
private static final CachedClass NUMBER_CLASS = ReflectionCache.getCachedClass(Number.class);
private static final CachedClass [] NUMBER_CLASS_ARR = new CachedClass[] { NUMBER_CLASS };
private static final CachedClass NUMBER_CLASS = ReflectionCache.getCachedClass(Number.class);
private static final CachedClass[] NUMBER_CLASS_ARR = {NUMBER_CLASS};

protected NumberNumberMetaMethod() {
parameterTypes = NUMBER_CLASS_ARR;
setParametersTypes(NUMBER_CLASS_ARR);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
package org.codehaus.groovy.runtime.dgmimpl.arrays;

public abstract class ArrayGetAtMetaMethod extends ArrayMetaMethod {

protected ArrayGetAtMetaMethod() {
parameterTypes = INTEGER_CLASS_ARR;
setParametersTypes(INTEGER_CLASS_ARR);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import java.lang.reflect.Modifier;

public abstract class ArrayMetaMethod extends CallSiteAwareMetaMethod {
protected static final CachedClass INTEGER_CLASS = ReflectionCache.getCachedClass(Integer.class);
protected static final CachedClass[] INTEGER_CLASS_ARR = new CachedClass[]{INTEGER_CLASS};
protected static final CachedClass INTEGER_CLASS = ReflectionCache.getCachedClass(Integer.class);
protected static final CachedClass[] INTEGER_CLASS_ARR = {INTEGER_CLASS};

protected static int normaliseIndex(int i, int size) {
int temp = i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@
*/
package org.codehaus.groovy.runtime.dgmimpl.arrays;

import org.codehaus.groovy.reflection.CachedClass;

import static org.codehaus.groovy.reflection.ReflectionCache.OBJECT_CLASS;

public abstract class ArrayPutAtMetaMethod extends ArrayMetaMethod {
private static final CachedClass[] PARAM_TYPES = {INTEGER_CLASS, OBJECT_CLASS};

protected ArrayPutAtMetaMethod() {
setParametersTypes(PARAM_TYPES);
}

@Override
public String getName() {
return "putAt";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite;

public class BooleanArrayGetAtMetaMethod extends ArrayGetAtMetaMethod {
private static final CachedClass ARR_CLASS = ReflectionCache.getCachedClass(boolean[].class);
private static final CachedClass ARRAY_CLASS = ReflectionCache.getCachedClass(boolean[].class);

@Override
public Class getReturnType() {
Expand All @@ -35,7 +35,7 @@ public Class getReturnType() {

@Override
public final CachedClass getDeclaringClass() {
return ARR_CLASS;
return ARRAY_CLASS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,11 @@
import org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite;

public class BooleanArrayPutAtMetaMethod extends ArrayPutAtMetaMethod {
private static final CachedClass OBJECT_CLASS = ReflectionCache.OBJECT_CLASS;
private static final CachedClass ARR_CLASS = ReflectionCache.getCachedClass(boolean[].class);
private static final CachedClass[] PARAM_CLASS_ARR = new CachedClass[]{INTEGER_CLASS, OBJECT_CLASS};

public BooleanArrayPutAtMetaMethod() {
parameterTypes = PARAM_CLASS_ARR;
}
private static final CachedClass ARRAY_CLASS = ReflectionCache.getCachedClass(boolean[].class);

@Override
public final CachedClass getDeclaringClass() {
return ARR_CLASS;
return ARRAY_CLASS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite;

public class ByteArrayGetAtMetaMethod extends ArrayGetAtMetaMethod {
private static final CachedClass ARR_CLASS = ReflectionCache.getCachedClass(byte[].class);
private static final CachedClass ARRAY_CLASS = ReflectionCache.getCachedClass(byte[].class);

@Override
public Class getReturnType() {
Expand All @@ -35,7 +35,7 @@ public Class getReturnType() {

@Override
public final CachedClass getDeclaringClass() {
return ARR_CLASS;
return ARRAY_CLASS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,11 @@
import org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite;

public class ByteArrayPutAtMetaMethod extends ArrayPutAtMetaMethod {
private static final CachedClass OBJECT_CLASS = ReflectionCache.OBJECT_CLASS;
private static final CachedClass ARR_CLASS = ReflectionCache.getCachedClass(byte[].class);
private static final CachedClass[] PARAM_CLASS_ARR = new CachedClass[]{INTEGER_CLASS, OBJECT_CLASS};

public ByteArrayPutAtMetaMethod() {
parameterTypes = PARAM_CLASS_ARR;
}
private static final CachedClass ARRAY_CLASS = ReflectionCache.getCachedClass(byte[].class);

@Override
public final CachedClass getDeclaringClass() {
return ARR_CLASS;
return ARRAY_CLASS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite;

public class CharacterArrayGetAtMetaMethod extends ArrayGetAtMetaMethod {
private static final CachedClass ARR_CLASS = ReflectionCache.getCachedClass(char[].class);
private static final CachedClass ARRAY_CLASS = ReflectionCache.getCachedClass(char[].class);

@Override
public Class getReturnType() {
Expand All @@ -35,7 +35,7 @@ public Class getReturnType() {

@Override
public final CachedClass getDeclaringClass() {
return ARR_CLASS;
return ARRAY_CLASS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,11 @@
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;

public class CharacterArrayPutAtMetaMethod extends ArrayPutAtMetaMethod {
private static final CachedClass OBJECT_CLASS = ReflectionCache.OBJECT_CLASS;
private static final CachedClass ARR_CLASS = ReflectionCache.getCachedClass(char[].class);
private static final CachedClass[] PARAM_CLASS_ARR = new CachedClass[]{INTEGER_CLASS, OBJECT_CLASS};

public CharacterArrayPutAtMetaMethod() {
parameterTypes = PARAM_CLASS_ARR;
}
private static final CachedClass ARRAY_CLASS = ReflectionCache.getCachedClass(char[].class);

@Override
public final CachedClass getDeclaringClass() {
return ARR_CLASS;
return ARRAY_CLASS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite;

public class DoubleArrayGetAtMetaMethod extends ArrayGetAtMetaMethod {
private static final CachedClass ARR_CLASS = ReflectionCache.getCachedClass(double[].class);
private static final CachedClass ARRAY_CLASS = ReflectionCache.getCachedClass(double[].class);

@Override
public Class getReturnType() {
Expand All @@ -35,7 +35,7 @@ public Class getReturnType() {

@Override
public final CachedClass getDeclaringClass() {
return ARR_CLASS;
return ARRAY_CLASS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,11 @@
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;

public class DoubleArrayPutAtMetaMethod extends ArrayPutAtMetaMethod {
private static final CachedClass OBJECT_CLASS = ReflectionCache.OBJECT_CLASS;
private static final CachedClass ARR_CLASS = ReflectionCache.getCachedClass(double[].class);
private static final CachedClass[] PARAM_CLASS_ARR = new CachedClass[]{INTEGER_CLASS, OBJECT_CLASS};

public DoubleArrayPutAtMetaMethod() {
parameterTypes = PARAM_CLASS_ARR;
}
private static final CachedClass ARRAY_CLASS = ReflectionCache.getCachedClass(double[].class);

@Override
public final CachedClass getDeclaringClass() {
return ARR_CLASS;
return ARRAY_CLASS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite;

public class FloatArrayGetAtMetaMethod extends ArrayGetAtMetaMethod {
private static final CachedClass ARR_CLASS = ReflectionCache.getCachedClass(float[].class);
private static final CachedClass ARRAY_CLASS = ReflectionCache.getCachedClass(float[].class);

@Override
public Class getReturnType() {
Expand All @@ -35,7 +35,7 @@ public Class getReturnType() {

@Override
public final CachedClass getDeclaringClass() {
return ARR_CLASS;
return ARRAY_CLASS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,11 @@
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;

public class FloatArrayPutAtMetaMethod extends ArrayPutAtMetaMethod {
private static final CachedClass OBJECT_CLASS = ReflectionCache.OBJECT_CLASS;
private static final CachedClass ARR_CLASS = ReflectionCache.getCachedClass(float[].class);
private static final CachedClass[] PARAM_CLASS_ARR = new CachedClass[]{INTEGER_CLASS, OBJECT_CLASS};

public FloatArrayPutAtMetaMethod() {
parameterTypes = PARAM_CLASS_ARR;
}
private static final CachedClass ARRAY_CLASS = ReflectionCache.getCachedClass(float[].class);

@Override
public final CachedClass getDeclaringClass() {
return ARR_CLASS;
return ARRAY_CLASS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite;

public class IntegerArrayGetAtMetaMethod extends ArrayGetAtMetaMethod {
private static final CachedClass ARR_CLASS = ReflectionCache.getCachedClass(int[].class);
private static final CachedClass ARRAY_CLASS = ReflectionCache.getCachedClass(int[].class);

@Override
public Class getReturnType() {
Expand All @@ -35,7 +35,7 @@ public Class getReturnType() {

@Override
public final CachedClass getDeclaringClass() {
return ARR_CLASS;
return ARRAY_CLASS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,11 @@
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;

public class IntegerArrayPutAtMetaMethod extends ArrayPutAtMetaMethod {
private static final CachedClass OBJECT_CLASS = ReflectionCache.OBJECT_CLASS;
private static final CachedClass ARR_CLASS = ReflectionCache.getCachedClass(int[].class);
private static final CachedClass[] PARAM_CLASS_ARR = new CachedClass[]{INTEGER_CLASS, OBJECT_CLASS};

public IntegerArrayPutAtMetaMethod() {
parameterTypes = PARAM_CLASS_ARR;
}
private static final CachedClass ARRAY_CLASS = ReflectionCache.getCachedClass(int[].class);

@Override
public final CachedClass getDeclaringClass() {
return ARR_CLASS;
return ARRAY_CLASS;
}

@Override
Expand Down
Loading

0 comments on commit 6153dfd

Please sign in to comment.