Skip to content

Commit 9642e85

Browse files
authored
GROOVY-11641: index trait field under original name (#2206)
* refactor compile mode inputs * GROOVY-11641: index trait field under original name
1 parent 263ad7a commit 9642e85

File tree

5 files changed

+530
-513
lines changed

5 files changed

+530
-513
lines changed

src/main/java/groovy/lang/MetaClassImpl.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
import java.util.concurrent.ConcurrentHashMap;
111111
import java.util.concurrent.ConcurrentMap;
112112
import java.util.function.BiConsumer;
113+
import java.util.regex.Pattern;
113114

114115
import static groovy.lang.Tuple.tuple;
115116
import static java.lang.Character.isUpperCase;
@@ -144,6 +145,11 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
144145
private static final MetaMethod AMBIGUOUS_LISTENER_METHOD = new DummyMetaMethod();
145146
private static final Comparator<CachedClass> CACHED_CLASS_NAME_COMPARATOR = Comparator.comparing(CachedClass::getName);
146147
private static final boolean PERMISSIVE_PROPERTY_ACCESS = SystemUtil.getBooleanSafe("groovy.permissive.property.access");
148+
private static final Pattern TRAIT_FIELD;
149+
static {
150+
final var identifier = "[\\p{javaJavaIdentifierStart}&&[^_$]][\\p{javaJavaIdentifierPart}&&[^_$]]*"; // _ and $ are special
151+
TRAIT_FIELD = Pattern.compile("(?:" + identifier + "_)*" + identifier + "(?:\\$" + identifier + ")*__(" + identifier + ")");
152+
}
147153
private static final VMPlugin VM_PLUGIN = VMPluginFactory.getPlugin();
148154

149155
protected final Class theClass;
@@ -2358,8 +2364,15 @@ private void fillStaticPropertyIndex() {
23582364
} else {
23592365
prop = null; // ignore all other types
23602366
}
2361-
2362-
if (prop != null) staticPropertyIndex.put(name, prop);
2367+
if (prop != null) {
2368+
staticPropertyIndex.put(name, prop);
2369+
if (!name.startsWith("$") && name.contains("__")) {
2370+
var matcher = TRAIT_FIELD.matcher(name);
2371+
if (matcher.matches()) { // GROOVY-11641
2372+
staticPropertyIndex.putIfAbsent(matcher.group(1), prop);
2373+
}
2374+
}
2375+
}
23632376
};
23642377

23652378
classPropertyIndex.computeIfAbsent(theCachedClass, x -> new LinkedHashMap<>()).forEach(indexStaticProperty);

src/main/java/org/codehaus/groovy/transform/trait/TraitReceiverTransformer.java

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import static org.codehaus.groovy.ast.tools.GeneralUtils.propX;
5656
import static org.codehaus.groovy.ast.tools.GeneralUtils.ternaryX;
5757
import static org.codehaus.groovy.ast.tools.GeneralUtils.varX;
58+
import static org.codehaus.groovy.transform.stc.StaticTypesMarker.DYNAMIC_RESOLUTION;
5859

5960
/**
6061
* This expression transformer is used internally by the {@link org.codehaus.groovy.transform.trait.TraitASTTransformation
@@ -123,6 +124,7 @@ public Expression transform(final Expression exp) {
123124
}
124125
} else if (accessedVariable instanceof DynamicVariable && !inClosure) { // GROOVY-9386
125126
PropertyExpression propertyExpression = propX(varX(weaved), vexp.getName());
127+
propertyExpression.putNodeMetaData(DYNAMIC_RESOLUTION, Boolean.TRUE);
126128
propertyExpression.getProperty().setSourcePosition(exp);
127129
return propertyExpression;
128130
}

src/main/java/org/codehaus/groovy/transform/trait/Traits.java

-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ public static Method getBridgeMethodTarget(Method someMethod) {
244244
return null;
245245
}
246246

247-
248247
/**
249248
* Converts a class implementing some trait into a target class. If the trait is a dynamic proxy and
250249
* that the target class is assignable to the target object of the proxy, then the target object is

src/main/java/org/codehaus/groovy/vmplugin/v8/Selector.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ private void handleBoolean() {
285285
private static class PropertySelector extends MethodSelector {
286286
private boolean insertName;
287287

288-
public PropertySelector(CacheableCallSite callSite, Class<?> sender, String methodName, CallType callType, boolean safeNavigation, boolean thisCall, boolean spreadCall, Object[] arguments) {
289-
super(callSite, sender, methodName, callType, safeNavigation, thisCall, spreadCall, arguments);
288+
public PropertySelector(CacheableCallSite callSite, Class<?> sender, String propertyName, CallType callType, boolean safeNavigation, boolean thisCall, boolean spreadCall, Object[] arguments) {
289+
super(callSite, sender, propertyName, callType, safeNavigation, thisCall, spreadCall, arguments);
290290
}
291291

292292
/**

0 commit comments

Comments
 (0)