Skip to content

Commit 2e38fd8

Browse files
committed
GROOVY-10177
1 parent 64dcf11 commit 2e38fd8

File tree

5 files changed

+63
-24
lines changed

5 files changed

+63
-24
lines changed

base/org.codehaus.groovy25/src/org/codehaus/groovy/ast/ImmutableClassNode.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2021 the original author or authors.
2+
* Copyright 2009-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -83,6 +83,14 @@ public void setSynthetic(boolean b) {}
8383

8484
// ClassNode overrides:
8585

86+
@Override
87+
public void addProperty(PropertyNode pn) {}
88+
89+
@Override
90+
public List<PropertyNode> getProperties() {
91+
return Collections.emptyList();
92+
}
93+
8694
@Override
8795
public List<MethodNode> getDeclaredMethods(String name) {
8896
if (lazyInitDone && !writeProtected) {

base/org.codehaus.groovy30/src/org/codehaus/groovy/ast/ImmutableClassNode.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2021 the original author or authors.
2+
* Copyright 2009-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -83,6 +83,14 @@ public void setSynthetic(boolean b) {}
8383

8484
// ClassNode overrides:
8585

86+
@Override
87+
public void addProperty(PropertyNode pn) {}
88+
89+
@Override
90+
public List<PropertyNode> getProperties() {
91+
return Collections.emptyList();
92+
}
93+
8694
@Override
8795
public List<MethodNode> getDeclaredMethods(String name) {
8896
if (lazyInitDone && !writeProtected) {

base/org.codehaus.groovy40/src/org/codehaus/groovy/ast/ImmutableClassNode.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2021 the original author or authors.
2+
* Copyright 2009-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -83,6 +83,9 @@ public void setSynthetic(boolean b) {}
8383

8484
// ClassNode overrides:
8585

86+
@Override
87+
public void addProperty(PropertyNode pn) {}
88+
8689
@Override
8790
public void addTypeAnnotation(AnnotationNode an) {}
8891

@@ -119,6 +122,11 @@ public List<MethodNode> getDeclaredMethods(String name) {
119122
return super.getDeclaredMethods(name);
120123
}
121124

125+
@Override
126+
public List<PropertyNode> getProperties() {
127+
return Collections.emptyList();
128+
}
129+
122130
@Override
123131
public void setAnnotated(boolean b) {}
124132

base/org.eclipse.jdt.groovy.core/src/org/codehaus/jdt/groovy/internal/compiler/ast/JDTClassNode.java

+31-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2021 the original author or authors.
2+
* Copyright 2009-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,9 @@
1515
*/
1616
package org.codehaus.jdt.groovy.internal.compiler.ast;
1717

18+
import static org.eclipse.jdt.groovy.search.AccessorSupport.isGetter;
19+
20+
import java.beans.Introspector;
1821
import java.util.ArrayList;
1922
import java.util.Arrays;
2023
import java.util.Collections;
@@ -581,13 +584,13 @@ void setUpGenerics() {
581584

582585
@Override
583586
public void addProperty(final PropertyNode node) {
584-
throw new UnsupportedOperationException("JDTClassNode is immutable, should not be called to add property: " + node.getName());
587+
throw new UnsupportedOperationException("JDTClassNode cannot accept property: " + node.getName());
585588
}
586589

587590
@Override
588591
public PropertyNode addProperty(final String name, final int modifiers, final ClassNode type,
589592
final Expression initialValueExpression, final Statement getterBlock, final Statement setterBlock) {
590-
throw new UnsupportedOperationException("JDTClassNode is immutable, should not be called to add property: " + name);
593+
throw new UnsupportedOperationException("JDTClassNode cannot accept property: " + name);
591594
}
592595

593596
@Override
@@ -616,6 +619,22 @@ public List<PropertyNode> getProperties() {
616619

617620
nodes.add(clone);
618621
}
622+
} else {
623+
// hydrate properties from getters
624+
for (MethodNode mn : getMethods()) {
625+
if (mn.isPublic() && isGetter(mn) && isGenerated(mn) && !"getMetaClass".equals(mn.getName())) {
626+
String propertyName = Introspector.decapitalize(mn.getName().substring(mn.getName().startsWith("is") ? 2 : 3));
627+
// check for field with same name/type
628+
FieldNode fn = getField(propertyName);
629+
if (fn != null && fn.isPrivate() && fn.getType().equals(mn.getReturnType())) {
630+
PropertyNode pn = new PropertyNode(fn, fn.getModifiers() & (Flags.AccFinal | Flags.AccStatic), null, null);
631+
pn.addAnnotations(fn.getAnnotations());
632+
pn.setDeclaringClass(this);
633+
634+
super.getProperties().add(pn);
635+
}
636+
}
637+
}
619638
}
620639
bits |= PROPERTIES_INITIALIZED;
621640
}
@@ -625,6 +644,15 @@ public List<PropertyNode> getProperties() {
625644
return Collections.unmodifiableList(super.getProperties());
626645
}
627646

647+
private static boolean isGenerated(MethodNode mn) {
648+
for (AnnotationNode an : mn.getAnnotations()) {
649+
if (an.getClassNode().getName().equals("groovy.transform.Generated")) {
650+
return true;
651+
}
652+
}
653+
return false;
654+
}
655+
628656
@Override
629657
public Iterator<InnerClassNode> getInnerClasses() {
630658
if ((bits & INNER_TYPES_INITIALIZED) == 0) {

base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/VariableScope.java

+5-18
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static org.codehaus.groovy.ast.tools.GenericsUtils.parseClassNodesFromString;
2020
import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.evaluateExpression;
2121

22-
import java.beans.Introspector;
2322
import java.io.DataInputStream;
2423
import java.io.DataOutputStream;
2524
import java.io.File;
@@ -61,7 +60,6 @@
6160
import org.codehaus.groovy.ast.MethodNode;
6261
import org.codehaus.groovy.ast.ModuleNode;
6362
import org.codehaus.groovy.ast.Parameter;
64-
import org.codehaus.groovy.ast.PropertyNode;
6563
import org.codehaus.groovy.ast.Variable;
6664
import org.codehaus.groovy.ast.expr.BinaryExpression;
6765
import org.codehaus.groovy.ast.expr.ClassExpression;
@@ -97,10 +95,10 @@ public class VariableScope implements Iterable<VariableScope.VariableInfo> {
9795
public static final ClassNode VOID_CLASS_NODE = ClassHelper.VOID_TYPE; // void.class
9896
public static final ClassNode VOID_WRAPPER_CLASS_NODE = ClassHelper.void_WRAPPER_TYPE; // Void.class
9997

98+
public static final ClassNode CLASS_CLASS_NODE = ClassHelper.CLASS_Type;
10099
public static final ClassNode OBJECT_CLASS_NODE = ClassHelper.OBJECT_TYPE;
101100
public static final ClassNode GROOVY_OBJECT_CLASS_NODE = ClassHelper.GROOVY_OBJECT_TYPE;
102101
public static final ClassNode GROOVY_SUPPORT_CLASS_NODE = ClassHelper.GROOVY_OBJECT_SUPPORT_TYPE;
103-
public static final ClassNode CLOSURE_CLASS_NODE = ClassHelper.CLOSURE_TYPE;
104102
public static final ClassNode ENUMERATION_CLASS_NODE = ClassHelper.make(Enumeration.class);
105103
public static final ClassNode COLLECTION_CLASS_NODE = ClassHelper.make(Collection.class);
106104
public static final ClassNode ITERABLE_CLASS_NODE = ClassHelper.make(Iterable.class);
@@ -110,11 +108,12 @@ public class VariableScope implements Iterable<VariableScope.VariableInfo> {
110108
public static final ClassNode ENTRY_CLASS_NODE = ClassHelper.make(Map.Entry.class);
111109
public static final ClassNode RANGE_CLASS_NODE = ClassHelper.RANGE_TYPE;
112110
public static final ClassNode TUPLE_CLASS_NODE = ClassHelper.make(Tuple.class);
113-
public static final ClassNode STRING_CLASS_NODE = ClassHelper.STRING_TYPE;
114-
public static final ClassNode GSTRING_CLASS_NODE = ClassHelper.GSTRING_TYPE;
115-
public static final ClassNode NUMBER_CLASS_NODE = ClassHelper.Number_TYPE;
116111
public static final ClassNode BIG_DECIMAL_CLASS = ClassHelper.BigDecimal_TYPE;
117112
public static final ClassNode BIG_INTEGER_CLASS = ClassHelper.BigInteger_TYPE;
113+
public static final ClassNode NUMBER_CLASS_NODE = ClassHelper.Number_TYPE;
114+
public static final ClassNode STRING_CLASS_NODE = ClassHelper.STRING_TYPE;
115+
public static final ClassNode GSTRING_CLASS_NODE = ClassHelper.GSTRING_TYPE;
116+
public static final ClassNode CLOSURE_CLASS_NODE = ClassHelper.CLOSURE_TYPE;
118117
public static final ClassNode PATTERN_CLASS_NODE = ClassHelper.PATTERN_TYPE;
119118
public static final ClassNode MATCHER_CLASS_NODE = ClassHelper.make(Matcher.class);
120119

@@ -144,18 +143,6 @@ public class VariableScope implements Iterable<VariableScope.VariableInfo> {
144143
public static final ClassNode FLOAT_CLASS_NODE = ClassHelper.Float_TYPE;
145144
public static final ClassNode DOUBLE_CLASS_NODE = ClassHelper.Double_TYPE;
146145

147-
// don't cache because we have to add properties
148-
public static final ClassNode CLASS_CLASS_NODE = initializeProperties(ClassHelper.makeWithoutCaching(Class.class));
149-
150-
// NOTE: JDTClassNode contains very similar method
151-
private static ClassNode initializeProperties(ClassNode node) {
152-
node.getMethods().stream().filter(AccessorSupport::isGetter).forEach(methodNode -> {
153-
String propertyName = Introspector.decapitalize(methodNode.getName().substring(methodNode.getName().startsWith("is") ? 2 : 3));
154-
node.addProperty(new PropertyNode(propertyName, methodNode.getModifiers(), methodNode.getReturnType(), null, null, null, null));
155-
});
156-
return node;
157-
}
158-
159146
//--------------------------------------------------------------------------
160147

161148
/**

0 commit comments

Comments
 (0)