You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2020/02/08 16:39:11 UTC
[groovy] 01/02: minor edits
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit ce40fc99911453c387c2a2804e10a797901c05f8
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sat Feb 8 10:09:52 2020 -0600
minor edits
---
src/main/java/groovy/lang/ProxyMetaClass.java | 96 ++++++------
.../groovy/runtime/callsite/AbstractCallSite.java | 163 ++++++++++++---------
.../groovy/runtime/callsite/MetaClassSite.java | 5 +-
.../groovy/runtime/callsite/PogoMetaClassSite.java | 19 +--
src/test/groovy/CategoryTest.groovy | 53 ++++---
5 files changed, 192 insertions(+), 144 deletions(-)
diff --git a/src/main/java/groovy/lang/ProxyMetaClass.java b/src/main/java/groovy/lang/ProxyMetaClass.java
index ed1d8dd..06dcf4f 100644
--- a/src/main/java/groovy/lang/ProxyMetaClass.java
+++ b/src/main/java/groovy/lang/ProxyMetaClass.java
@@ -18,6 +18,9 @@
*/
package groovy.lang;
+import java.util.Objects;
+import java.util.function.Supplier;
+
/**
* As subclass of MetaClass, ProxyMetaClass manages calls from Groovy Objects to POJOs.
* It enriches MetaClass with the feature of making method invocations interceptable by
@@ -33,14 +36,13 @@ package groovy.lang;
*/
public class ProxyMetaClass extends MetaClassImpl implements AdaptingMetaClass {
- protected MetaClass adaptee = null;
- protected Interceptor interceptor = null;
-
+ protected MetaClass adaptee;
+ protected Interceptor interceptor;
/**
* convenience factory method for the most usual case.
*/
- public static ProxyMetaClass getInstance(Class theClass) {
+ public static ProxyMetaClass getInstance(final Class theClass) {
MetaClassRegistry metaRegistry = GroovySystem.getMetaClassRegistry();
MetaClass meta = metaRegistry.getMetaClass(theClass);
return new ProxyMetaClass(metaRegistry, theClass, meta);
@@ -49,24 +51,48 @@ public class ProxyMetaClass extends MetaClassImpl implements AdaptingMetaClass {
/**
* @param adaptee the MetaClass to decorate with interceptability
*/
- public ProxyMetaClass(MetaClassRegistry registry, Class theClass, MetaClass adaptee) {
+ public ProxyMetaClass(final MetaClassRegistry registry, final Class theClass, final MetaClass adaptee) {
super(registry, theClass);
- this.adaptee = adaptee;
- if (null == adaptee) throw new IllegalArgumentException("adaptee must not be null");
+ this.adaptee = Objects.requireNonNull(adaptee, "adaptee must not be null");
super.initialize();
}
+ @Override
public synchronized void initialize() {
this.adaptee.initialize();
}
+ @Override
+ public MetaClass getAdaptee() {
+ return this.adaptee;
+ }
+
+ @Override
+ public void setAdaptee(final MetaClass metaClass) {
+ this.adaptee = metaClass;
+ }
+
+ /**
+ * @return the interceptor in use or null if no interceptor is used
+ */
+ public Interceptor getInterceptor() {
+ return interceptor;
+ }
+
+ /**
+ * @param interceptor may be null to reset any interception
+ */
+ public void setInterceptor(final Interceptor interceptor) {
+ this.interceptor = interceptor;
+ }
+
/**
* Use the ProxyMetaClass for the given Closure.
* Cares for balanced register/unregister.
*
* @param closure piece of code to be executed with registered ProxyMetaClass
*/
- public Object use(Closure closure) {
+ public Object use(final Closure closure) {
// grab existing meta (usually adaptee but we may have nested use calls)
MetaClass origMetaClass = registry.getMetaClass(theClass);
registry.setMetaClass(theClass, this);
@@ -83,7 +109,7 @@ public class ProxyMetaClass extends MetaClassImpl implements AdaptingMetaClass {
*
* @param closure piece of code to be executed with ProxyMetaClass
*/
- public Object use(GroovyObject object, Closure closure) {
+ public Object use(final GroovyObject object, final Closure closure) {
// grab existing meta (usually adaptee but we may have nested use calls)
MetaClass origMetaClass = object.getMetaClass();
object.setMetaClass(this);
@@ -95,25 +121,12 @@ public class ProxyMetaClass extends MetaClassImpl implements AdaptingMetaClass {
}
/**
- * @return the interceptor in use or null if no interceptor is used
- */
- public Interceptor getInterceptor() {
- return interceptor;
- }
-
- /**
- * @param interceptor may be null to reset any interception
- */
- public void setInterceptor(Interceptor interceptor) {
- this.interceptor = interceptor;
- }
-
- /**
* Call invokeMethod on adaptee with logic like in MetaClass unless we have an Interceptor.
* With Interceptor the call is nested in its beforeInvoke and afterInvoke methods.
* The method call is suppressed if Interceptor.doInvoke() returns false.
* See Interceptor for details.
*/
+ @Override
public Object invokeMethod(final Object object, final String methodName, final Object[] arguments) {
return doCall(object, methodName, arguments, interceptor, () -> adaptee.invokeMethod(object, methodName, arguments));
}
@@ -135,6 +148,7 @@ public class ProxyMetaClass extends MetaClassImpl implements AdaptingMetaClass {
* The method call is suppressed if Interceptor.doInvoke() returns false.
* See Interceptor for details.
*/
+ @Override
public Object invokeStaticMethod(final Object object, final String methodName, final Object[] arguments) {
return doCall(object, methodName, arguments, interceptor, () -> adaptee.invokeStaticMethod(object, methodName, arguments));
}
@@ -145,6 +159,7 @@ public class ProxyMetaClass extends MetaClassImpl implements AdaptingMetaClass {
* The method call is suppressed if Interceptor.doInvoke() returns false.
* See Interceptor for details.
*/
+ @Override
public Object invokeConstructor(final Object[] arguments) {
return doCall(theClass, "ctor", arguments, interceptor, () -> adaptee.invokeConstructor(arguments));
}
@@ -157,20 +172,20 @@ public class ProxyMetaClass extends MetaClassImpl implements AdaptingMetaClass {
* @param property the property name
* @return the value of the property
*/
- public Object getProperty(Class aClass, Object object, String property, boolean b, boolean b1) {
+ public Object getProperty(final Class aClass, final Object object, final String property, final boolean useSuper, final boolean fromInsideClass) {
if (null == interceptor) {
- return super.getProperty(aClass, object, property, b, b1);
+ return super.getProperty(aClass, object, property, useSuper, fromInsideClass);
}
if (interceptor instanceof PropertyAccessInterceptor) {
PropertyAccessInterceptor pae = (PropertyAccessInterceptor) interceptor;
Object result = pae.beforeGet(object, property);
if (interceptor.doInvoke()) {
- result = super.getProperty(aClass, object, property, b, b1);
+ result = super.getProperty(aClass, object, property, useSuper, fromInsideClass);
}
return result;
}
- return super.getProperty(aClass, object, property, b, b1);
+ return super.getProperty(aClass, object, property, useSuper, fromInsideClass);
}
/**
@@ -181,42 +196,29 @@ public class ProxyMetaClass extends MetaClassImpl implements AdaptingMetaClass {
* @param property The property name to set
* @param newValue The new value of the property
*/
- public void setProperty(Class aClass, Object object, String property, Object newValue, boolean b, boolean b1) {
+ public void setProperty(final Class aClass, final Object object, final String property, final Object newValue, final boolean useSuper, final boolean fromInsideClass) {
if (null == interceptor) {
- super.setProperty(aClass, object, property, newValue, b, b1);
+ super.setProperty(aClass, object, property, newValue, useSuper, fromInsideClass);
}
if (interceptor instanceof PropertyAccessInterceptor) {
PropertyAccessInterceptor pae = (PropertyAccessInterceptor) interceptor;
pae.beforeSet(object, property, newValue);
if (interceptor.doInvoke()) {
- super.setProperty(aClass, object, property, newValue, b, b1);
+ super.setProperty(aClass, object, property, newValue, useSuper, fromInsideClass);
}
} else {
- super.setProperty(aClass, object, property, newValue, b, b1);
+ super.setProperty(aClass, object, property, newValue, useSuper, fromInsideClass);
}
}
- public MetaClass getAdaptee() {
- return this.adaptee;
- }
-
- public void setAdaptee(MetaClass metaClass) {
- this.adaptee = metaClass;
- }
-
- // since Java has no Closures...
- private interface Callable {
- Object call();
- }
-
- private Object doCall(Object object, String methodName, Object[] arguments, Interceptor interceptor, Callable howToInvoke) {
+ private Object doCall(final Object object, final String methodName, final Object[] arguments, final Interceptor interceptor, final Supplier<Object> howToInvoke) {
if (null == interceptor) {
- return howToInvoke.call();
+ return howToInvoke.get();
}
Object result = interceptor.beforeInvoke(object, methodName, arguments);
if (interceptor.doInvoke()) {
- result = howToInvoke.call();
+ result = howToInvoke.get();
}
result = interceptor.afterInvoke(object, methodName, arguments, result);
return result;
diff --git a/src/main/java/org/codehaus/groovy/runtime/callsite/AbstractCallSite.java b/src/main/java/org/codehaus/groovy/runtime/callsite/AbstractCallSite.java
index fb40680..e191fdd 100644
--- a/src/main/java/org/codehaus/groovy/runtime/callsite/AbstractCallSite.java
+++ b/src/main/java/org/codehaus/groovy/runtime/callsite/AbstractCallSite.java
@@ -37,89 +37,101 @@ import org.codehaus.groovy.runtime.wrappers.Wrapper;
import java.lang.reflect.Method;
/**
- * Base class for all call sites
+ * Base class for all call sites.
*/
public class AbstractCallSite implements CallSite {
+
protected final int index;
protected final String name;
protected final CallSiteArray array;
- public AbstractCallSite(CallSiteArray array, int index, String name) {
+ public AbstractCallSite(final CallSiteArray array, final int index, final String name) {
this.name = name;
this.index = index;
this.array = array;
}
- public AbstractCallSite(CallSite prev) {
+ public AbstractCallSite(final CallSite prev) {
this.name = prev.getName();
this.index = prev.getIndex();
this.array = prev.getArray();
}
+ @Override
public int getIndex() {
return index;
}
+ @Override
public CallSiteArray getArray() {
return array;
}
+ @Override
public String getName() {
return name;
}
- public final Object callSafe(Object receiver, Object[] args) throws Throwable {
+ @Override
+ public final Object callSafe(final Object receiver, final Object[] args) throws Throwable {
if (receiver == null)
return null;
return call(receiver, args);
}
- public final Object callSafe(Object receiver) throws Throwable {
+ @Override
+ public final Object callSafe(final Object receiver) throws Throwable {
if (receiver == null)
return null;
return call(receiver);
}
- public final Object callSafe(Object receiver, Object arg1) throws Throwable {
+ @Override
+ public final Object callSafe(final Object receiver, final Object arg1) throws Throwable {
if (receiver == null)
return null;
return call(receiver, arg1);
}
- public final Object callSafe(Object receiver, Object arg1, Object arg2) throws Throwable {
+ @Override
+ public final Object callSafe(final Object receiver, final Object arg1, final Object arg2) throws Throwable {
if (receiver == null)
return null;
return call(receiver, arg1, arg2);
}
- public final Object callSafe(Object receiver, Object arg1, Object arg2, Object arg3) throws Throwable {
+ @Override
+ public final Object callSafe(final Object receiver, final Object arg1, final Object arg2, final Object arg3) throws Throwable {
if (receiver == null)
return null;
return call(receiver, arg1, arg2, arg3);
}
- public Object callSafe(Object receiver, Object arg1, Object arg2, Object arg3, Object arg4) throws Throwable {
+ @Override
+ public Object callSafe(final Object receiver, final Object arg1, final Object arg2, final Object arg3, final Object arg4) throws Throwable {
if (receiver == null)
return null;
return call(receiver, arg1, arg2, arg3, arg4);
}
-
- public Object call(Object receiver, Object[] args) throws Throwable {
+ @Override
+ public Object call(final Object receiver, final Object[] args) throws Throwable {
return CallSiteArray.defaultCall(this, receiver, args);
}
- public Object call(Object receiver) throws Throwable {
+ @Override
+ public Object call(final Object receiver) throws Throwable {
return call(receiver, CallSiteArray.NOPARAM);
}
- public Object call(Object receiver, Object arg1) throws Throwable {
+ @Override
+ public Object call(final Object receiver, final Object arg1) throws Throwable {
CallSite stored = array.array[index];
if (stored!=this) {
return stored.call(receiver, arg1);
@@ -127,7 +139,8 @@ public class AbstractCallSite implements CallSite {
return call(receiver, ArrayUtil.createArray(arg1));
}
- public Object call(Object receiver, Object arg1, Object arg2) throws Throwable {
+ @Override
+ public Object call(final Object receiver, final Object arg1, final Object arg2) throws Throwable {
CallSite stored = array.array[index];
if (stored!=this) {
return stored.call(receiver, arg1, arg2);
@@ -135,7 +148,8 @@ public class AbstractCallSite implements CallSite {
return call(receiver, ArrayUtil.createArray(arg1, arg2));
}
- public Object call(Object receiver, Object arg1, Object arg2, Object arg3) throws Throwable {
+ @Override
+ public Object call(final Object receiver, final Object arg1, final Object arg2, final Object arg3) throws Throwable {
CallSite stored = array.array[index];
if (stored!=this) {
return stored.call(receiver, arg1, arg2, arg3);
@@ -143,7 +157,8 @@ public class AbstractCallSite implements CallSite {
return call(receiver, ArrayUtil.createArray(arg1, arg2, arg3));
}
- public Object call(Object receiver, Object arg1, Object arg2, Object arg3, Object arg4) throws Throwable {
+ @Override
+ public Object call(final Object receiver, final Object arg1, final Object arg2, final Object arg3, final Object arg4) throws Throwable {
CallSite stored = array.array[index];
if (stored!=this) {
return stored.call(receiver, arg1, arg2, arg3, arg4);
@@ -151,16 +166,18 @@ public class AbstractCallSite implements CallSite {
return call(receiver, ArrayUtil.createArray(arg1, arg2, arg3, arg4));
}
-
- public Object callCurrent(GroovyObject receiver, Object[] args) throws Throwable {
+ @Override
+ public Object callCurrent(final GroovyObject receiver, final Object[] args) throws Throwable {
return CallSiteArray.defaultCallCurrent(this, receiver, args);
}
- public Object callCurrent(GroovyObject receiver) throws Throwable {
+ @Override
+ public Object callCurrent(final GroovyObject receiver) throws Throwable {
return callCurrent(receiver, CallSiteArray.NOPARAM);
}
- public Object callCurrent(GroovyObject receiver, Object arg1) throws Throwable {
+ @Override
+ public Object callCurrent(final GroovyObject receiver, final Object arg1) throws Throwable {
CallSite stored = array.array[index];
if (stored!=this) {
return stored.callCurrent(receiver, arg1);
@@ -168,7 +185,8 @@ public class AbstractCallSite implements CallSite {
return callCurrent(receiver, ArrayUtil.createArray(arg1));
}
- public Object callCurrent(GroovyObject receiver, Object arg1, Object arg2) throws Throwable {
+ @Override
+ public Object callCurrent(final GroovyObject receiver, final Object arg1, final Object arg2) throws Throwable {
CallSite stored = array.array[index];
if (stored!=this) {
return stored.callCurrent(receiver, arg1, arg2);
@@ -176,7 +194,8 @@ public class AbstractCallSite implements CallSite {
return callCurrent(receiver, ArrayUtil.createArray(arg1, arg2));
}
- public Object callCurrent(GroovyObject receiver, Object arg1, Object arg2, Object arg3) throws Throwable {
+ @Override
+ public Object callCurrent(final GroovyObject receiver, final Object arg1, final Object arg2, final Object arg3) throws Throwable {
CallSite stored = array.array[index];
if (stored!=this) {
return stored.callCurrent(receiver, arg1, arg2, arg3);
@@ -184,7 +203,8 @@ public class AbstractCallSite implements CallSite {
return callCurrent(receiver, ArrayUtil.createArray(arg1, arg2, arg3));
}
- public Object callCurrent(GroovyObject receiver, Object arg1, Object arg2, Object arg3, Object arg4) throws Throwable {
+ @Override
+ public Object callCurrent(final GroovyObject receiver, final Object arg1, final Object arg2, final Object arg3, final Object arg4) throws Throwable {
CallSite stored = array.array[index];
if (stored!=this) {
return stored.callCurrent(receiver, arg1, arg2, arg3, arg4);
@@ -192,15 +212,18 @@ public class AbstractCallSite implements CallSite {
return callCurrent(receiver, ArrayUtil.createArray(arg1, arg2, arg3, arg4));
}
- public Object callStatic(Class receiver, Object[] args) throws Throwable {
+ @Override
+ public Object callStatic(final Class receiver, final Object[] args) throws Throwable {
return CallSiteArray.defaultCallStatic(this, receiver, args);
}
- public Object callStatic(Class receiver) throws Throwable {
+ @Override
+ public Object callStatic(final Class receiver) throws Throwable {
return callStatic(receiver, CallSiteArray.NOPARAM);
}
- public Object callStatic(Class receiver, Object arg1) throws Throwable {
+ @Override
+ public Object callStatic(final Class receiver, final Object arg1) throws Throwable {
CallSite stored = array.array[index];
if (stored!=this) {
return stored.callStatic(receiver, arg1);
@@ -208,7 +231,8 @@ public class AbstractCallSite implements CallSite {
return callStatic(receiver, ArrayUtil.createArray(arg1));
}
- public Object callStatic(Class receiver, Object arg1, Object arg2) throws Throwable {
+ @Override
+ public Object callStatic(final Class receiver, final Object arg1, final Object arg2) throws Throwable {
CallSite stored = array.array[index];
if (stored!=this) {
return stored.callStatic(receiver, arg1, arg2);
@@ -216,7 +240,8 @@ public class AbstractCallSite implements CallSite {
return callStatic(receiver, ArrayUtil.createArray(arg1, arg2));
}
- public Object callStatic(Class receiver, Object arg1, Object arg2, Object arg3) throws Throwable {
+ @Override
+ public Object callStatic(final Class receiver, final Object arg1, final Object arg2, final Object arg3) throws Throwable {
CallSite stored = array.array[index];
if (stored!=this) {
return stored.callStatic(receiver, arg1, arg2, arg3);
@@ -224,7 +249,8 @@ public class AbstractCallSite implements CallSite {
return callStatic(receiver, ArrayUtil.createArray(arg1, arg2, arg3));
}
- public Object callStatic(Class receiver, Object arg1, Object arg2, Object arg3, Object arg4) throws Throwable {
+ @Override
+ public Object callStatic(final Class receiver, final Object arg1, final Object arg2, final Object arg3, final Object arg4) throws Throwable {
CallSite stored = array.array[index];
if (stored!=this) {
return stored.callStatic(receiver, arg1, arg2, arg3, arg4);
@@ -232,16 +258,18 @@ public class AbstractCallSite implements CallSite {
return callStatic(receiver, ArrayUtil.createArray(arg1, arg2, arg3, arg4));
}
-
- public Object callConstructor(Object receiver, Object[] args) throws Throwable {
+ @Override
+ public Object callConstructor(final Object receiver, final Object[] args) throws Throwable {
return CallSiteArray.defaultCallConstructor(this, receiver, args);
}
- public Object callConstructor(Object receiver) throws Throwable {
+ @Override
+ public Object callConstructor(final Object receiver) throws Throwable {
return callConstructor(receiver, CallSiteArray.NOPARAM);
}
- public Object callConstructor(Object receiver, Object arg1) throws Throwable {
+ @Override
+ public Object callConstructor(final Object receiver, final Object arg1) throws Throwable {
CallSite stored = array.array[index];
if (stored!=this) {
return stored.callConstructor(receiver, arg1);
@@ -249,7 +277,8 @@ public class AbstractCallSite implements CallSite {
return callConstructor(receiver, ArrayUtil.createArray(arg1));
}
- public Object callConstructor(Object receiver, Object arg1, Object arg2) throws Throwable {
+ @Override
+ public Object callConstructor(final Object receiver, final Object arg1, final Object arg2) throws Throwable {
CallSite stored = array.array[index];
if (stored!=this) {
return stored.callConstructor(receiver, arg1, arg2);
@@ -257,7 +286,8 @@ public class AbstractCallSite implements CallSite {
return callConstructor(receiver, ArrayUtil.createArray(arg1, arg2));
}
- public Object callConstructor(Object receiver, Object arg1, Object arg2, Object arg3) throws Throwable {
+ @Override
+ public Object callConstructor(final Object receiver, final Object arg1, final Object arg2, final Object arg3) throws Throwable {
CallSite stored = array.array[index];
if (stored!=this) {
return stored.callConstructor(receiver, arg1, arg2, arg3);
@@ -265,7 +295,8 @@ public class AbstractCallSite implements CallSite {
return callConstructor(receiver, ArrayUtil.createArray(arg1, arg2, arg3));
}
- public Object callConstructor(Object receiver, Object arg1, Object arg2, Object arg3, Object arg4) throws Throwable {
+ @Override
+ public Object callConstructor(final Object receiver, final Object arg1, final Object arg2, final Object arg3, final Object arg4) throws Throwable {
CallSite stored = array.array[index];
if (stored!=this) {
return stored.callConstructor(receiver, arg1, arg2, arg3, arg4);
@@ -273,7 +304,7 @@ public class AbstractCallSite implements CallSite {
return callConstructor(receiver, ArrayUtil.createArray(arg1, arg2, arg3, arg4));
}
- static boolean noCoerce(ParameterTypes metaMethod, Object[] args) {
+ static boolean noCoerce(final ParameterTypes metaMethod, final Object[] args) {
final CachedClass[] paramClasses = metaMethod.getParameterTypes();
if (paramClasses.length != args.length)
return false;
@@ -286,19 +317,20 @@ public class AbstractCallSite implements CallSite {
return false;
}
- static boolean noWrappers(Object[] args) {
- for (int i = 0; i != args.length; ++i)
+ static boolean noWrappers(final Object[] args) {
+ for (int i = 0; i != args.length; i += 1)
if (args[i] instanceof Wrapper)
return false;
return true;
}
-
- public Object callGetProperty(Object receiver) throws Throwable {
+ @Override
+ public Object callGetProperty(final Object receiver) throws Throwable {
return acceptGetProperty(receiver).getProperty(receiver);
}
- public Object callGroovyObjectGetProperty(Object receiver) throws Throwable {
+ @Override
+ public Object callGroovyObjectGetProperty(final Object receiver) throws Throwable {
if (receiver == null) {
try {
return InvokerHelper.getProperty(NullObject.getNullObject(), name);
@@ -310,27 +342,27 @@ public class AbstractCallSite implements CallSite {
}
}
- public CallSite acceptGetProperty(Object receiver) {
+ public CallSite acceptGetProperty(final Object receiver) {
return createGetPropertySite(receiver);
}
- public CallSite acceptGroovyObjectGetProperty(Object receiver) {
+ public CallSite acceptGroovyObjectGetProperty(final Object receiver) {
return createGroovyObjectGetPropertySite(receiver);
}
- protected final CallSite createGetPropertySite(Object receiver) {
+ protected final CallSite createGetPropertySite(final Object receiver) {
if (receiver == null) {
return new NullCallSite(this);
} else if (receiver instanceof GroovyObject) {
return createGroovyObjectGetPropertySite(receiver);
} else if (receiver instanceof Class) {
- return createClassMetaClassGetPropertySite((Class) receiver);
+ return createClassMetaClassGetPropertySite((Class<?>) receiver);
}
return createPojoMetaClassGetPropertySite(receiver);
}
- protected final CallSite createGroovyObjectGetPropertySite(Object receiver) {
- Class aClass = receiver.getClass();
+ protected final CallSite createGroovyObjectGetPropertySite(final Object receiver) {
+ Class<?> aClass = receiver.getClass();
try {
final Method method = aClass.getMethod("getProperty", String.class);
if (method != null && (method.isSynthetic() || isMarkedInternal(method)) && ((GroovyObject) receiver).getMetaClass() instanceof MetaClassImpl)
@@ -339,21 +371,22 @@ public class AbstractCallSite implements CallSite {
// fall threw
}
if (receiver instanceof Class) {
- return createClassMetaClassGetPropertySite((Class) receiver);
+ return createClassMetaClassGetPropertySite((Class<?>) receiver);
} else {
return createPogoGetPropertySite(aClass);
}
}
- private boolean isMarkedInternal(Method method) {
+ private boolean isMarkedInternal(final Method method) {
return method.getAnnotation(Internal.class) != null;
}
- public Object getProperty(Object receiver) throws Throwable {
+ @Override
+ public Object getProperty(final Object receiver) throws Throwable {
throw new UnsupportedOperationException();
}
- private CallSite createPojoMetaClassGetPropertySite(Object receiver) {
+ private CallSite createPojoMetaClassGetPropertySite(final Object receiver) {
final MetaClass metaClass = InvokerHelper.getMetaClass(receiver);
CallSite site;
@@ -375,13 +408,13 @@ public class AbstractCallSite implements CallSite {
return site;
}
- private CallSite createClassMetaClassGetPropertySite(Class aClass) {
+ private CallSite createClassMetaClassGetPropertySite(final Class<?> aClass) {
CallSite site = new ClassMetaClassGetPropertySite(this, aClass);
array.array[index] = site;
return site;
}
- private CallSite createPogoMetaClassGetPropertySite(GroovyObject receiver) {
+ private CallSite createPogoMetaClassGetPropertySite(final GroovyObject receiver) {
MetaClass metaClass = receiver.getMetaClass();
CallSite site;
@@ -403,25 +436,21 @@ public class AbstractCallSite implements CallSite {
return site;
}
- private CallSite createPogoGetPropertySite(Class aClass) {
+ private CallSite createPogoGetPropertySite(final Class<?> aClass) {
CallSite site = new PogoGetPropertySite(this, aClass);
array.array[index] = site;
return site;
}
- public final Object callGetPropertySafe(Object receiver) throws Throwable {
- if (receiver == null)
- return null;
- else
- return callGetProperty(receiver);
+ @Override
+ public final Object callGetPropertySafe(final Object receiver) throws Throwable {
+ if (receiver == null) return null;
+ return callGetProperty(receiver);
}
- public final Object callGroovyObjectGetPropertySafe(Object receiver) throws Throwable {
- if (receiver == null)
- return null;
- else
- return callGroovyObjectGetProperty(receiver);
+ @Override
+ public final Object callGroovyObjectGetPropertySafe(final Object receiver) throws Throwable {
+ if (receiver == null) return null;
+ return callGroovyObjectGetProperty(receiver);
}
-
}
-
diff --git a/src/main/java/org/codehaus/groovy/runtime/callsite/MetaClassSite.java b/src/main/java/org/codehaus/groovy/runtime/callsite/MetaClassSite.java
index f9fb77b..e275510 100644
--- a/src/main/java/org/codehaus/groovy/runtime/callsite/MetaClassSite.java
+++ b/src/main/java/org/codehaus/groovy/runtime/callsite/MetaClassSite.java
@@ -21,12 +21,13 @@ package org.codehaus.groovy.runtime.callsite;
import groovy.lang.MetaClass;
/**
- * Call site which holds reference to meta class
+ * Call site which holds reference to meta class.
*/
public abstract class MetaClassSite extends AbstractCallSite {
+
protected final MetaClass metaClass;
- public MetaClassSite(CallSite site, MetaClass metaClass) {
+ public MetaClassSite(final CallSite site, final MetaClass metaClass) {
super(site);
this.metaClass = metaClass;
}
diff --git a/src/main/java/org/codehaus/groovy/runtime/callsite/PogoMetaClassSite.java b/src/main/java/org/codehaus/groovy/runtime/callsite/PogoMetaClassSite.java
index 4b16fcb..902694a 100644
--- a/src/main/java/org/codehaus/groovy/runtime/callsite/PogoMetaClassSite.java
+++ b/src/main/java/org/codehaus/groovy/runtime/callsite/PogoMetaClassSite.java
@@ -26,11 +26,12 @@ import org.codehaus.groovy.runtime.ScriptBytecodeAdapter;
import org.codehaus.groovy.runtime.metaclass.MissingMethodExecutionFailed;
public class PogoMetaClassSite extends MetaClassSite {
- public PogoMetaClassSite(CallSite site, MetaClass metaClass) {
+
+ public PogoMetaClassSite(final CallSite site, final MetaClass metaClass) {
super(site, metaClass);
}
- public final Object call(Object receiver, Object[] args) throws Throwable {
+ public final Object call(final Object receiver, final Object[] args) throws Throwable {
if (checkCall(receiver)) {
try {
try {
@@ -53,21 +54,17 @@ public class PogoMetaClassSite extends MetaClassSite {
}
}
- protected final boolean checkCall(Object receiver) {
- return receiver instanceof GroovyObject && ((GroovyObject)receiver).getMetaClass() == metaClass;
- }
-
- public final Object callCurrent(GroovyObject receiver, Object[] args) throws Throwable {
+ public final Object callCurrent(final GroovyObject receiver, final Object[] args) throws Throwable {
if (checkCall(receiver)) {
try {
try {
return metaClass.invokeMethod(array.owner, receiver, name, args, false, true);
} catch (MissingMethodException e) {
if (e instanceof MissingMethodExecutionFailed) {
- throw (MissingMethodException)e.getCause();
+ throw (MissingMethodException) e.getCause();
} else if (receiver.getClass() == e.getType() && e.getMethod().equals(name)) {
// in case there's nothing else, invoke the object's own invokeMethod()
- return ((GroovyObject)receiver).invokeMethod(name, args);
+ return receiver.invokeMethod(name, args);
} else {
throw e;
}
@@ -79,4 +76,8 @@ public class PogoMetaClassSite extends MetaClassSite {
return CallSiteArray.defaultCallCurrent(this, receiver, args);
}
}
+
+ protected final boolean checkCall(final Object receiver) {
+ return (receiver instanceof GroovyObject && ((GroovyObject) receiver).getMetaClass() == metaClass);
+ }
}
diff --git a/src/test/groovy/CategoryTest.groovy b/src/test/groovy/CategoryTest.groovy
index 56bd4e1..1dbbb99 100644
--- a/src/test/groovy/CategoryTest.groovy
+++ b/src/test/groovy/CategoryTest.groovy
@@ -20,7 +20,7 @@ package groovy
import groovy.test.GroovyTestCase
-class CategoryTest extends GroovyTestCase {
+final class CategoryTest extends GroovyTestCase {
void setUp() {
def dummy = null
@@ -67,7 +67,7 @@ class CategoryTest extends GroovyTestCase {
assert something == "nihao"
}
}
-
+
void testCategoryReplacedPropertyAccessMethod() {
def cth = new CategoryTestHelper()
cth.aProperty = "aValue"
@@ -79,7 +79,7 @@ class CategoryTest extends GroovyTestCase {
}
assert cth.aProperty == "aValue"
}
-
+
void testCategoryHiddenByClassMethod() {
assertScript """
class A{}
@@ -91,7 +91,7 @@ class CategoryTest extends GroovyTestCase {
}
"""
}
-
+
void testCategoryOverridingClassMethod() {
assertScript """
class A {def m(){1}}
@@ -111,7 +111,7 @@ class CategoryTest extends GroovyTestCase {
}
"""
}
-
+
void testCategoryWithMixedOverriding() {
assertScript """
class A{def m(){0}}
@@ -123,7 +123,7 @@ class CategoryTest extends GroovyTestCase {
}
"""
}
-
+
void testCategoryInheritance() {
assertScript """
public class Foo {
@@ -131,19 +131,19 @@ class CategoryTest extends GroovyTestCase {
"Foo.foo()"
}
}
-
+
public class Bar extends Foo{
static Object bar(Object obj) {
"Bar.bar()"
}
}
-
+
def obj = new Object()
-
+
use(Foo){
assert obj.foo() == "Foo.foo()"
}
-
+
use(Bar){
assert obj.bar() == "Bar.bar()"
assert obj.foo() == "Foo.foo()"
@@ -157,17 +157,17 @@ class CategoryTest extends GroovyTestCase {
// in call site caching this triggers the usage of POJOMetaClassSite,
// which was missing a null check for the receiver. The last foo call
// uses null to exaclty check that path. I use multiple calls with foo(1)
- // before to ensure for example indy will do the right things as well,
+ // before to ensure for example indy will do the right things as well,
// since indy may need more than one call here.
assertScript """
class Cat {
- public static findAll(Integer x, Closure cl) {1}
+ public static findAll(Integer x, Closure cl) {1}
}
def foo(x) {
x.findAll {}
}
-
+
use (Cat) {
assert foo(1) == 1
assert foo(1) == 1
@@ -213,8 +213,25 @@ class CategoryTest extends GroovyTestCase {
assert foo(x) == 1
}
- //GROOVY-6263
- void testCallToPrivateMethod() {
+ void testCallToPrivateMethod1() {
+ assertScript '''
+ class A {
+ private foo() { 1 }
+ def baz() { foo() }
+ }
+
+ class B extends A {}
+
+ class C {}
+
+ use(C) {
+ assert new B().baz() == 1
+ }
+ '''
+ }
+
+ // GROOVY-6263
+ void testCallToPrivateMethod2() {
assertScript '''
class A {
private foo(a) { 1 }
@@ -230,6 +247,7 @@ class CategoryTest extends GroovyTestCase {
}
'''
}
+
// GROOVY-3867
void testPropertyMissing() {
def x = new X()
@@ -274,9 +292,6 @@ class CategoryTest extends GroovyTestCase {
}
}
}
-
-
-
}
class X{ def bar(){1}}
@@ -311,4 +326,4 @@ class CategoryTestHelperPropertyReplacer {
private static aVal = "anotherValue"
static getaProperty(CategoryTestHelper self) { return aVal }
static void setaProperty(CategoryTestHelper self, newValue) { aVal = newValue }
-}
\ No newline at end of file
+}