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 2020/07/11 22:32:49 UTC
[groovy] branch GROOVY-9631 updated: Replace `Index` and
`MethodIndex` with `IndexMap`
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch GROOVY-9631
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY-9631 by this push:
new 0f330c4 Replace `Index` and `MethodIndex` with `IndexMap`
0f330c4 is described below
commit 0f330c443be08cc51691b256daa7ce0e10e9d2ed
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sun Jul 12 06:32:27 2020 +0800
Replace `Index` and `MethodIndex` with `IndexMap`
---
src/main/java/groovy/lang/MetaClassImpl.java | 90 ++++++++++++++++------------
1 file changed, 53 insertions(+), 37 deletions(-)
diff --git a/src/main/java/groovy/lang/MetaClassImpl.java b/src/main/java/groovy/lang/MetaClassImpl.java
index f26af35..2e63b94 100644
--- a/src/main/java/groovy/lang/MetaClassImpl.java
+++ b/src/main/java/groovy/lang/MetaClassImpl.java
@@ -147,13 +147,13 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
protected final boolean isMap;
protected final MetaMethodIndex metaMethodIndex;
- private final Index classPropertyIndex = new MethodIndex();
- private final SingleKeyHashMap staticPropertyIndex = new SingleKeyHashMap();
+ private final IndexMap<CachedClass, IndexMap> classPropertyIndex = new IndexMap<>();
+ private final IndexMap staticPropertyIndex = new IndexMap();
private final Map<String, MetaMethod> listeners = new HashMap<>();
private final List<MetaMethod> allMethods = new ArrayList<>();
// we only need one of these that can be reused over and over.
private final MetaProperty arrayLengthProperty = new MetaArrayLengthProperty();
- private final Index classPropertyIndexForSuper = new MethodIndex();
+ private final IndexMap<CachedClass, IndexMap> classPropertyIndexForSuper = new IndexMap<>();
private final Set<MetaMethod> newGroovyMethodsSet = new HashSet<>();
private final MetaMethod[] myNewMetaMethods;
private final MetaMethod[] additionalMetaMethods;
@@ -282,12 +282,12 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
public MetaProperty getMetaProperty(String name) {
MetaProperty metaProperty = null;
- SingleKeyHashMap propertyMap = classPropertyIndex.getNotNull(theCachedClass);
+ IndexMap propertyMap = classPropertyIndex.getOrPut(theCachedClass, new IndexMap());
metaProperty = (MetaProperty) propertyMap.get(name);
if (metaProperty == null) {
metaProperty = (MetaProperty) staticPropertyIndex.get(name);
if (metaProperty == null) {
- propertyMap = classPropertyIndexForSuper.getNotNull(theCachedClass);
+ propertyMap = classPropertyIndexForSuper.getOrPut(theCachedClass, new IndexMap());
metaProperty = (MetaProperty) propertyMap.get(name);
if (metaProperty == null) {
MetaBeanProperty property = findPropertyInClassHierarchy(name, theCachedClass);
@@ -2174,16 +2174,16 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
*/
public List<MetaProperty> getProperties() {
checkInitalised();
- SingleKeyHashMap propertyMap = classPropertyIndex.getNullable(theCachedClass);
+ IndexMap propertyMap = classPropertyIndex.get(theCachedClass);
if (propertyMap == null) {
// GROOVY-6903: May happen in some special environment, like under Android, due
// to classloading issues
- propertyMap = new SingleKeyHashMap();
+ propertyMap = new IndexMap();
}
// simply return the values of the metaproperty map as a List
List ret = new ArrayList(propertyMap.size());
- for (ComplexKeyHashMap.EntryIterator iter = propertyMap.getEntrySetIterator(); iter.hasNext(); ) {
- MetaProperty element = (MetaProperty) ((SingleKeyHashMap.Entry) iter.next()).value;
+ for (Iterator iter = propertyMap.entrySet().iterator(); iter.hasNext(); ) {
+ MetaProperty element = (MetaProperty) ((Map.Entry) iter.next()).getValue();
if (element instanceof CachedField) continue;
// filter out DGM beans
if (element instanceof MetaBeanProperty) {
@@ -2345,9 +2345,9 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
superInterfaces.sort(CACHED_CLASS_NAME_COMPARATOR);
}
- SingleKeyHashMap iPropertyIndex = classPropertyIndex.getNotNull(theCachedClass);
+ IndexMap iPropertyIndex = classPropertyIndex.getOrPut(theCachedClass, new IndexMap());
for (CachedClass iclass : superInterfaces) {
- SingleKeyHashMap sPropertyIndex = classPropertyIndex.getNotNull(iclass);
+ IndexMap sPropertyIndex = classPropertyIndex.getOrPut(iclass, new IndexMap());
copyNonPrivateFields(sPropertyIndex, iPropertyIndex, null);
addFields(iclass, iPropertyIndex);
}
@@ -2368,7 +2368,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
// if this an Array, then add the special read-only "length" property
if (theCachedClass.isArray) {
- SingleKeyHashMap map = new SingleKeyHashMap();
+ IndexMap map = new IndexMap();
map.put("length", arrayLengthProperty);
classPropertyIndex.put(theCachedClass, map);
}
@@ -2387,9 +2387,9 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
}
private void makeStaticPropertyIndex() {
- SingleKeyHashMap propertyMap = classPropertyIndex.getNotNull(theCachedClass);
- for (ComplexKeyHashMap.EntryIterator iter = propertyMap.getEntrySetIterator(); iter.hasNext(); ) {
- SingleKeyHashMap.Entry entry = ((SingleKeyHashMap.Entry) iter.next());
+ IndexMap propertyMap = classPropertyIndex.getOrPut(theCachedClass, new IndexMap());
+ for (Iterator iter = propertyMap.entrySet().iterator(); iter.hasNext(); ) {
+ Map.Entry entry = ((Map.Entry) iter.next());
MetaProperty mp = (MetaProperty) entry.getValue();
if (mp instanceof CachedField) {
@@ -2458,32 +2458,32 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
return result;
}
- private void copyClassPropertyIndexForSuper(Index dest) {
- for (ComplexKeyHashMap.EntryIterator iter = classPropertyIndex.getEntrySetIterator(); iter.hasNext(); ) {
- SingleKeyHashMap.Entry entry = (SingleKeyHashMap.Entry) iter.next();
- SingleKeyHashMap newVal = new SingleKeyHashMap();
- dest.put((CachedClass) entry.getKey(), newVal);
+ private void copyClassPropertyIndexForSuper(IndexMap dest) {
+ for (Iterator iter = classPropertyIndex.entrySet().iterator(); iter.hasNext(); ) {
+ Map.Entry entry = (Map.Entry) iter.next();
+ IndexMap newVal = new IndexMap();
+ dest.put(entry.getKey(), newVal);
}
}
private void inheritStaticInterfaceFields(LinkedList superClasses, Set interfaces) {
for (Object anInterface : interfaces) {
CachedClass iclass = (CachedClass) anInterface;
- SingleKeyHashMap iPropertyIndex = classPropertyIndex.getNotNull(iclass);
+ IndexMap iPropertyIndex = classPropertyIndex.getOrPut(iclass, new IndexMap());
addFields(iclass, iPropertyIndex);
for (Object superClass : superClasses) {
CachedClass sclass = (CachedClass) superClass;
if (!iclass.getTheClass().isAssignableFrom(sclass.getTheClass())) continue;
- SingleKeyHashMap sPropertyIndex = classPropertyIndex.getNotNull(sclass);
+ IndexMap sPropertyIndex = classPropertyIndex.getOrPut(sclass, new IndexMap());
copyNonPrivateFields(iPropertyIndex, sPropertyIndex, null);
}
}
}
private void inheritFields(LinkedList<CachedClass> superClasses) {
- SingleKeyHashMap last = null;
+ IndexMap last = null;
for (CachedClass klass : superClasses) {
- SingleKeyHashMap propertyIndex = classPropertyIndex.getNotNull(klass);
+ IndexMap propertyIndex = classPropertyIndex.getOrPut(klass, new IndexMap());
if (last != null) {
copyNonPrivateFields(last, propertyIndex, klass);
}
@@ -2492,15 +2492,15 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
}
}
- private static void addFields(CachedClass klass, SingleKeyHashMap propertyIndex) {
+ private static void addFields(CachedClass klass, IndexMap propertyIndex) {
for (CachedField field : klass.getFields()) {
propertyIndex.put(field.getName(), field);
}
}
- private static void copyNonPrivateFields(SingleKeyHashMap from, SingleKeyHashMap to, @Nullable CachedClass klass) {
- for (ComplexKeyHashMap.EntryIterator it = from.getEntrySetIterator(); it.hasNext(); ) {
- SingleKeyHashMap.Entry entry = (SingleKeyHashMap.Entry) it.next();
+ private static void copyNonPrivateFields(IndexMap from, IndexMap to, @Nullable CachedClass klass) {
+ for (Iterator it = from.entrySet().iterator(); it.hasNext(); ) {
+ Map.Entry entry = (Map.Entry) it.next();
CachedField field = (CachedField) entry.getValue();
int modifiers = field.getModifiers();
if (Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers) || (!Modifier.isPrivate(modifiers)
@@ -2510,11 +2510,11 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
}
}
- private void applyStrayPropertyMethods(LinkedList<CachedClass> superClasses, Index classPropertyIndex, boolean isThis) {
+ private void applyStrayPropertyMethods(LinkedList<CachedClass> superClasses, IndexMap<CachedClass, IndexMap> classPropertyIndex, boolean isThis) {
// now look for any stray getters that may be used to define a property
for (CachedClass klass : superClasses) {
MetaMethodIndex.Header header = metaMethodIndex.getHeader(klass.getTheClass());
- SingleKeyHashMap propertyIndex = classPropertyIndex.getNotNull(klass);
+ IndexMap propertyIndex = classPropertyIndex.getOrPut(klass, new IndexMap());
for (MetaMethodIndex.Entry e = header.head; e != null; e = e.nextClassEntry) {
String methodName = e.name;
// name too short?
@@ -2598,7 +2598,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
}
}
- private static void createMetaBeanProperty(SingleKeyHashMap propertyIndex, String propName, boolean isGetter, MetaMethod propertyMethod) {
+ private static void createMetaBeanProperty(IndexMap propertyIndex, String propName, boolean isGetter, MetaMethod propertyMethod) {
// is this property already accounted for?
MetaProperty mp = (MetaProperty) propertyIndex.get(propName);
MetaProperty newMp = makeReplacementMetaProperty(mp, propName, isGetter, propertyMethod);
@@ -2654,7 +2654,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
if (staticProperty != null) {
staticPropertyIndex.put(mp.getName(), mp);
} else {
- SingleKeyHashMap propertyMap = classPropertyIndex.getNotNull(theCachedClass);
+ IndexMap propertyMap = classPropertyIndex.getOrPut(theCachedClass, new IndexMap());
//keep field
CachedField field;
MetaProperty old = (MetaProperty) propertyMap.get(mp.getName());
@@ -2851,13 +2851,13 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
CachedClass clazz = ReflectionCache.getCachedClass(_clazz);
while (true) {
- SingleKeyHashMap propertyMap;
+ IndexMap propertyMap;
if (useStatic) {
propertyMap = staticPropertyIndex;
} else if (useSuper) {
- propertyMap = classPropertyIndexForSuper.getNullable(clazz);
+ propertyMap = classPropertyIndexForSuper.get(clazz);
} else {
- propertyMap = classPropertyIndex.getNullable(clazz);
+ propertyMap = classPropertyIndex.get(clazz);
}
if (propertyMap == null) {
if (clazz != theCachedClass) {
@@ -2873,11 +2873,11 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
private MetaProperty getMetaProperty(String name, boolean useStatic) {
CachedClass clazz = theCachedClass;
- SingleKeyHashMap propertyMap;
+ IndexMap propertyMap;
if (useStatic) {
propertyMap = staticPropertyIndex;
} else {
- propertyMap = classPropertyIndex.getNullable(clazz);
+ propertyMap = classPropertyIndex.get(clazz);
}
if (propertyMap == null) {
return null;
@@ -3902,6 +3902,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
metaMethodIndex.clearCaches();
}
+ @Deprecated
private static final SingleKeyHashMap.Copier NAME_INDEX_COPIER = value -> {
if (value instanceof FastArray) {
return ((FastArray) value).copy();
@@ -3910,8 +3911,19 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
}
};
+ @Deprecated
private static final SingleKeyHashMap.Copier METHOD_INDEX_COPIER = value -> SingleKeyHashMap.copy(new SingleKeyHashMap(false), (SingleKeyHashMap) value, NAME_INDEX_COPIER);
+ private static class IndexMap<K, V> extends HashMap<K, V> {
+ public V getOrPut(K key, V value) {
+ return this.computeIfAbsent(key, k -> value);
+ }
+ }
+
+ /**
+ * @deprecated use {@link IndexMap} instead
+ */
+ @Deprecated
static class MethodIndex extends Index {
public MethodIndex(boolean b) {
super(false);
@@ -3934,6 +3946,10 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
}
}
+ /**
+ * @deprecated use {@link IndexMap} instead
+ */
+ @Deprecated
public static class Index extends SingleKeyHashMap {
public Index(int size) {