You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2022/08/06 13:32:07 UTC
[groovy] branch danielsun/refactor-20220806 updated (5c3e84dd0a -> f4448b3624)
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a change to branch danielsun/refactor-20220806
in repository https://gitbox.apache.org/repos/asf/groovy.git
discard 5c3e84dd0a Minor tweak: avoid throwing exceptions and extract variables
new f4448b3624 Minor tweak: avoid throwing exceptions and extract variables
This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version. This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:
* -- * -- B -- O -- O -- O (5c3e84dd0a)
\
N -- N -- N refs/heads/danielsun/refactor-20220806 (f4448b3624)
You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.
Any revisions marked "omit" are not gone; other references still
refer to them. Any revisions marked "discard" are gone forever.
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../groovy/reflection/stdclasses/CachedSAMClass.java | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
[groovy] 01/01: Minor tweak: avoid throwing exceptions and extract variables
Posted by su...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch danielsun/refactor-20220806
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit f4448b3624af664c7e24c10409cddb68cd29ae62
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Aug 6 21:19:33 2022 +0800
Minor tweak: avoid throwing exceptions and extract variables
---
.../reflection/stdclasses/CachedSAMClass.java | 84 ++++++++++++----------
1 file changed, 46 insertions(+), 38 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/reflection/stdclasses/CachedSAMClass.java b/src/main/java/org/codehaus/groovy/reflection/stdclasses/CachedSAMClass.java
index 60d545c183..893d2207b1 100644
--- a/src/main/java/org/codehaus/groovy/reflection/stdclasses/CachedSAMClass.java
+++ b/src/main/java/org/codehaus/groovy/reflection/stdclasses/CachedSAMClass.java
@@ -37,11 +37,12 @@ import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
public class CachedSAMClass extends CachedClass {
- private static final int ABSTRACT_STATIC_PRIVATE =
- Modifier.ABSTRACT|Modifier.PRIVATE|Modifier.STATIC;
+ private static final int ABSTRACT_STATIC_PRIVATE = Modifier.ABSTRACT | Modifier.PRIVATE | Modifier.STATIC;
private static final int VISIBILITY = 5; // public|protected
private static final Method[] EMPTY_METHOD_ARRAY = new Method[0];
private final Method method;
@@ -49,7 +50,7 @@ public class CachedSAMClass extends CachedClass {
public CachedSAMClass(Class klazz, ClassInfo classInfo) {
super(klazz, classInfo);
method = getSAMMethod(klazz);
- if (method==null) throw new GroovyBugError("assigned method should not have been null!");
+ if (method == null) throw new GroovyBugError("assigned method should not have been null!");
}
@Override
@@ -66,26 +67,24 @@ public class CachedSAMClass extends CachedClass {
/* Should we make the following method private? */
@SuppressWarnings("unchecked")
public static Object coerceToSAM(Closure argument, Method method, Class clazz, boolean isInterface) {
- if (argument!=null && clazz.isAssignableFrom(argument.getClass())) {
+ if (argument != null && clazz.isAssignableFrom(argument.getClass())) {
return argument;
}
+
+ final String name = method.getName();
if (isInterface) {
if (Traits.isTrait(clazz)) {
- Map<String,Closure> impl = Collections.singletonMap(
- method.getName(),
- argument
- );
- return ProxyGenerator.INSTANCE.instantiateAggregate(impl,Collections.singletonList(clazz));
+ Map<String, Closure> impl = Collections.singletonMap(name, argument);
+ return ProxyGenerator.INSTANCE.instantiateAggregate(impl, Collections.singletonList(clazz));
}
return Proxy.newProxyInstance(
clazz.getClassLoader(),
- new Class[]{clazz},
+ new Class[] { clazz },
new ConvertedClosure(argument));
} else {
- Map<String, Object> m = new HashMap<String,Object>();
- m.put(method.getName(), argument);
- return ProxyGenerator.INSTANCE.
- instantiateAggregateFromBaseClass(m, clazz);
+ Map<String, Object> m = new HashMap<>();
+ m.put(name, argument);
+ return ProxyGenerator.INSTANCE.instantiateAggregateFromBaseClass(m, clazz);
}
}
@@ -110,39 +109,41 @@ public class CachedSAMClass extends CachedClass {
return EMPTY_METHOD_ARRAY;
}
- private static void getAbstractMethods(Class c, List<Method> current) {
- if (c==null || !Modifier.isAbstract(c.getModifiers())) return;
+ private static void getAbstractMethods(Class<?> c, List<Method> current) {
+ if (c == null || !Modifier.isAbstract(c.getModifiers())) return;
getAbstractMethods(c.getSuperclass(), current);
- for (Class ci : c.getInterfaces()) {
+ for (Class<?> ci : c.getInterfaces()) {
getAbstractMethods(ci, current);
}
for (Method m : getDeclaredMethods(c)) {
- if (Modifier.isPrivate(m.getModifiers())) continue;
- if (Modifier.isAbstract(m.getModifiers())) current.add(m);
+ final int modifiers = m.getModifiers();
+ if (Modifier.isPrivate(modifiers)) continue;
+ if (Modifier.isAbstract(modifiers)) current.add(m);
}
}
- private static boolean hasUsableImplementation(Class c, Method m) {
- if (c==m.getDeclaringClass()) return false;
+ private static boolean hasUsableImplementation(Class<?> c, Method m) {
+ if (c == m.getDeclaringClass()) return false;
Method found;
try {
found = c.getMethod(m.getName(), m.getParameterTypes());
- int asp = found.getModifiers() & ABSTRACT_STATIC_PRIVATE;
- int visible = found.getModifiers() & VISIBILITY;
+ final int modifiers = found.getModifiers();
+ int asp = modifiers & ABSTRACT_STATIC_PRIVATE;
+ int visible = modifiers & VISIBILITY;
if (visible !=0 && asp == 0) return true;
- } catch (NoSuchMethodException e) {/*ignore*/}
- if (c==Object.class) return false;
+ } catch (NoSuchMethodException ignore) {
+ }
+ if (c == Object.class) return false;
return hasUsableImplementation(c.getSuperclass(), m);
}
private static Method getSingleNonDuplicateMethod(List<Method> current) {
- if (current.isEmpty()) return null;
- if (current.size()==1) return current.get(0);
+ final int size = current.size();
+ if (size == 0) return null;
+ if (size == 1) return current.get(0);
Method m = current.remove(0);
for (Method m2 : current) {
- if (m.getName().equals(m2.getName()) &&
- Arrays.equals(m.getParameterTypes(), m2.getParameterTypes()))
- {
+ if (m.getName().equals(m2.getName()) && Arrays.equals(m.getParameterTypes(), m2.getParameterTypes())) {
continue;
}
return null;
@@ -168,32 +169,39 @@ public class CachedSAMClass extends CachedClass {
// if the class is not abstract there is no abstract method
if (!Modifier.isAbstract(c.getModifiers())) return null;
if (c.isInterface()) {
- Method[] methods = c.getMethods();
// res stores the first found abstract method
Method res = null;
- for (Method mi : methods) {
+ for (Method mi : c.getMethods()) {
// ignore methods, that are not abstract and from Object
if (!Modifier.isAbstract(mi.getModifiers())) continue;
// ignore trait methods which have a default implementation
- if (mi.getAnnotation(Traits.Implemented.class)!=null) continue;
+ if (mi.getAnnotation(Traits.Implemented.class) != null) continue;
+
try {
- Object.class.getMethod(mi.getName(), mi.getParameterTypes());
+ final String name = mi.getName();
+ // avoid throwing `NoSuchMethodException` as possible as we could
+ if (OBJECT_METHOD_NAME_SET.contains(name)) {
+ Object.class.getMethod(name, mi.getParameterTypes());
+ }
continue;
- } catch (NoSuchMethodException e) {/*ignore*/}
+ } catch (NoSuchMethodException ignore) {
+ }
// we have two methods, so no SAM
- if (res!=null) return null;
+ if (res != null) return null;
res = mi;
}
return res;
} else {
-
- LinkedList<Method> methods = new LinkedList();
+ List<Method> methods = new LinkedList<>();
getAbstractMethods(c, methods);
if (methods.isEmpty()) return null;
methods.removeIf(m -> hasUsableImplementation(c, m));
return getSingleNonDuplicateMethod(methods);
}
}
+
+ private static final Set<String> OBJECT_METHOD_NAME_SET =
+ Collections.unmodifiableSet(Arrays.stream(Object.class.getMethods()).map(m -> m.getName()).collect(Collectors.toSet()));
}