Skip to content

Commit

Permalink
minor edits
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Nov 11, 2021
1 parent 4e7f4f4 commit 8d19017
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 97 deletions.
4 changes: 3 additions & 1 deletion src/main/java/groovy/lang/GroovyShell.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,10 @@ public void setProperty(String property, Object newValue) {
setVariable(property, newValue);
try {
super.setProperty(property, newValue);
} catch (ReadOnlyPropertyException e) {
throw e;
} catch (GroovyRuntimeException e) {
// ignore, was probably a dynamic property
// probably a dynamic property
}
}

Expand Down
35 changes: 17 additions & 18 deletions src/main/java/org/codehaus/groovy/ast/ClassNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ void remove(Object key, MethodNode value) {
protected List<InnerClassNode> innerClasses;
private List<ClassNode> permittedSubclasses = new ArrayList<>(4);
private List<AnnotationNode> typeAnnotations = Collections.emptyList();
private List<RecordComponentNode> recordComponentNodes = Collections.emptyList();
private boolean isRecord = false;
private List<RecordComponentNode> recordComponents = Collections.emptyList();

/**
* The AST Transformations to be applied during compilation.
Expand Down Expand Up @@ -1087,9 +1086,7 @@ protected void setCompileUnit(CompileUnit cu) {
if (compileUnit != null) compileUnit = cu;
}

/**
* @return {@code true} if the two arrays are of the same size and have the same contents
*/
@Deprecated
protected boolean parametersEqual(Parameter[] a, Parameter[] b) {
return ParameterUtils.parametersEqual(a, b);
}
Expand Down Expand Up @@ -1365,48 +1362,50 @@ public boolean isInterface() {
* Check instead for the {@code RecordType} annotation if looking for records and record-like classes.
*
* @return {@code true} if the instance represents a native {@code record}
*
* @since 4.0.0
*/
public boolean isRecord() {
return getUnresolvedSuperClass() != null && "java.lang.Record".equals(getUnresolvedSuperClass().getName());
}

@Deprecated
public List<RecordComponentNode> getRecordComponentNodes() {
return getRecordComponents();
}

/**
* Get the record components of record type
* Gets the record components of record type.
*
* @return {@code RecordComponentNode} instances
*
* @since 4.0.0
*/
public List<RecordComponentNode> getRecordComponents() {
if (redirect != null)
return redirect.getRecordComponents();
lazyClassInit();
return recordComponentNodes;
return recordComponents;
}

@Deprecated
public void setRecordComponentNodes(List<RecordComponentNode> recordComponentNodes) {
setRecordComponents(recordComponentNodes);
public List<RecordComponentNode> getRecordComponentNodes() {
return getRecordComponents();
}

/**
* Set the record components for record type
* Sets the record components for record type.
*
* @since 4.0.0
*/
public void setRecordComponents(List<RecordComponentNode> recordComponentNodes) {
public void setRecordComponents(List<RecordComponentNode> recordComponents) {
if (redirect != null) {
redirect.setRecordComponents(recordComponentNodes);
redirect.setRecordComponents(recordComponents);
} else {
this.recordComponentNodes = recordComponentNodes;
this.recordComponents = recordComponents;
}
}

@Deprecated
public void setRecordComponentNodes(List<RecordComponentNode> recordComponentNodes) {
setRecordComponents(recordComponentNodes);
}

public boolean isAbstract() {
return (getModifiers() & ACC_ABSTRACT) != 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private Variable findClassMember(final ClassNode node, final String name) {
if (name.equals(pn.getName())) return pn;
}

FieldNode fn = new FieldNode(name, mn.getModifiers() & 0xF, ClassHelper.OBJECT_TYPE, cn, null);
FieldNode fn = new FieldNode(name, mn.getModifiers() & 0xF, ClassHelper.dynamicType(), cn, null);
fn.setHasNoRealSourcePosition(true);
fn.setDeclaringClass(cn);
fn.setSynthetic(true);
Expand All @@ -198,9 +198,11 @@ private Variable findClassMember(final ClassNode node, final String name) {
}
}

for (ClassNode face : cn.getAllInterfaces()) {
FieldNode fn = face.getDeclaredField(name);
for (ClassNode in : cn.getAllInterfaces()) {
FieldNode fn = in.getDeclaredField(name);
if (fn != null) return fn;
PropertyNode pn = in.getProperty(name);
if (pn != null) return pn;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/codehaus/groovy/classgen/Verifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ private static FieldNode getMetaClassField(final ClassNode node) {
return ret;
}
ClassNode current = node;
while (!(isObjectType(current))) {
while (!isObjectType(current)) {
current = current.getSuperClass();
if (current == null) break;
ret = current.getDeclaredField("metaClass");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
* This is commonly used when you want to wire a new AST Transformation into the compilation.
*/
public class CompilationUnit extends ProcessingUnit {
private static final int COMPUTE_MAX_STACK_AND_FRAMES = ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES;

/** The overall AST for this CompilationUnit. */
protected CompileUnit ast; // TODO: Switch to private and access through getAST().
Expand Down Expand Up @@ -772,8 +771,9 @@ protected SourceUnit getSourceUnit() {
//
// Handle any callback that's been set
//
Optional.ofNullable(getClassgenCallback())
.ifPresent(callback -> callback.call(classVisitor, classNode));
if (classgenCallback != null) {
classgenCallback.call(classVisitor, classNode);
}

//
// Recurse for inner classes
Expand All @@ -791,7 +791,7 @@ public boolean needSortedInput() {
};

protected ClassVisitor createClassVisitor() {
return new ClassWriter(COMPUTE_MAX_STACK_AND_FRAMES) {
return new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS) {
private ClassNode getClassNode(String name) {
// try classes under compilation
CompileUnit cu = getAST();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.codehaus.groovy.ast.expr.ConstructorCallExpression;
import org.codehaus.groovy.ast.expr.DeclarationExpression;
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.expr.VariableExpression;
import org.codehaus.groovy.transform.trait.Traits;

import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.isUnboundedWildcard;
Expand Down Expand Up @@ -106,10 +107,10 @@ public void visitConstructorCallExpression(final ConstructorCallExpression expre
public void visitDeclarationExpression(final DeclarationExpression expression) {
if (expression.isMultipleAssignmentDeclaration()) {
for (Expression e : expression.getTupleExpression().getExpressions()) {
checkGenericsUsage(e.getType());
checkGenericsUsage(((VariableExpression) e).getOriginType());
}
} else {
checkGenericsUsage(expression.getVariableExpression().getType());
checkGenericsUsage(expression.getVariableExpression().getOriginType());
}

super.visitDeclarationExpression(expression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import org.codehaus.groovy.ast.stmt.EmptyStatement;
import org.codehaus.groovy.ast.stmt.ExpressionStatement;
import org.codehaus.groovy.ast.stmt.Statement;
import org.codehaus.groovy.ast.tools.GenericsUtils;
import org.codehaus.groovy.classgen.VariableScopeVisitor;
import org.codehaus.groovy.control.CompilationUnit;
import org.codehaus.groovy.control.CompilePhase;
Expand All @@ -61,10 +60,6 @@
import static org.apache.groovy.ast.tools.ClassNodeUtils.hasExplicitConstructor;
import static org.apache.groovy.ast.tools.ConstructorNodeUtils.checkPropNamesS;
import static org.apache.groovy.ast.tools.VisibilityUtils.getVisibility;
import static org.codehaus.groovy.ast.ClassHelper.MAP_TYPE;
import static org.codehaus.groovy.ast.ClassHelper.isObjectType;
import static org.codehaus.groovy.ast.ClassHelper.make;
import static org.codehaus.groovy.ast.ClassHelper.makeWithoutCaching;
import static org.codehaus.groovy.ast.tools.GeneralUtils.args;
import static org.codehaus.groovy.ast.tools.GeneralUtils.assignS;
import static org.codehaus.groovy.ast.tools.GeneralUtils.block;
Expand Down Expand Up @@ -94,25 +89,27 @@
public class TupleConstructorASTTransformation extends AbstractASTTransformation implements CompilationUnitAware {

private CompilationUnit compilationUnit;
static final Class MY_CLASS = TupleConstructor.class;
static final ClassNode MY_TYPE = make(MY_CLASS);

static final Class<?> MY_CLASS = TupleConstructor.class;
static final ClassNode MY_TYPE = ClassHelper.make(MY_CLASS);
static final String MY_TYPE_NAME = "@" + MY_TYPE.getNameWithoutPackage();

private static final String NAMED_ARGS = "__namedArgs";
private static final ClassNode LHMAP_TYPE = makeWithoutCaching(LinkedHashMap.class, false);
private static final ClassNode POJO_TYPE = make(POJO.class);
private static final ClassNode LHMAP_TYPE = ClassHelper.makeWithoutCaching(LinkedHashMap.class, false);
private static final ClassNode POJO_TYPE = ClassHelper.make(POJO.class);

@Override
public String getAnnotationName() {
return MY_TYPE_NAME;
}

@Override
public void setCompilationUnit(CompilationUnit unit) {
public void setCompilationUnit(final CompilationUnit unit) {
compilationUnit = unit;
}

@Override
public void visit(ASTNode[] nodes, SourceUnit source) {
public void visit(final ASTNode[] nodes, final SourceUnit source) {
init(nodes, source);
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode anno = (AnnotationNode) nodes[0];
Expand All @@ -134,8 +131,8 @@ public void visit(ASTNode[] nodes, SourceUnit source) {
return;
if (!checkPropertyList(cNode, excludes, "excludes", anno, MY_TYPE_NAME, includeFields, includeSuperProperties, allProperties, includeSuperFields, false))
return;
final GroovyClassLoader classLoader = compilationUnit != null ? compilationUnit.getTransformLoader() : source.getClassLoader();
final PropertyHandler handler = PropertyHandler.createPropertyHandler(this, classLoader, cNode);
GroovyClassLoader classLoader = compilationUnit != null ? compilationUnit.getTransformLoader() : source.getClassLoader();
PropertyHandler handler = PropertyHandler.createPropertyHandler(this, classLoader, cNode);
if (handler == null) return;
if (!handler.validateAttributes(this, anno)) return;

Expand Down Expand Up @@ -163,13 +160,13 @@ public void visit(ASTNode[] nodes, SourceUnit source) {
}
}

private static void createConstructor(AbstractASTTransformation xform, AnnotationNode anno, ClassNode cNode, boolean includeFields,
boolean includeProperties, boolean includeSuperFields, boolean includeSuperProperties,
List<String> excludes, final List<String> includes, boolean allNames, boolean allProperties,
SourceUnit sourceUnit, PropertyHandler handler, ClosureExpression pre, ClosureExpression post) {
private static void createConstructor(final AbstractASTTransformation xform, final AnnotationNode anno, final ClassNode cNode, final boolean includeFields,
final boolean includeProperties, final boolean includeSuperFields, final boolean includeSuperProperties,
final List<String> excludes, final List<String> includes, final boolean allNames, final boolean allProperties,
final SourceUnit sourceUnit, final PropertyHandler handler, final ClosureExpression pre, final ClosureExpression post) {
boolean callSuper = xform.memberHasValue(anno, "callSuper", true);
boolean force = xform.memberHasValue(anno, "force", true);
boolean defaults = !xform.memberHasValue(anno, "defaults", false);
boolean force = xform.memberHasValue(anno, "force", true);
boolean namedVariant = xform.memberHasValue(anno, "namedVariant", true);
Set<String> names = new HashSet<>();
List<PropertyNode> superList;
Expand All @@ -188,9 +185,9 @@ private static void createConstructor(AbstractASTTransformation xform, Annotatio
// no processing if existing constructors found unless forced or ImmutableBase in play
if (hasExplicitConstructor(null, cNode) && !force && !makeImmutable) return;

final List<Parameter> params = new ArrayList<>();
final List<Expression> superParams = new ArrayList<>();
final BlockStatement preBody = new BlockStatement();
List<Parameter> params = new ArrayList<>();
List<Expression> superParams = new ArrayList<>();
BlockStatement preBody = new BlockStatement();
boolean superInPre = false;
if (pre != null) {
superInPre = copyStatementsWithSuperAdjustment(pre, preBody);
Expand All @@ -200,7 +197,7 @@ private static void createConstructor(AbstractASTTransformation xform, Annotatio
}
}

final BlockStatement body = new BlockStatement();
BlockStatement body = new BlockStatement();

List<PropertyNode> tempList = new ArrayList<>(list);
tempList.addAll(superList);
Expand Down Expand Up @@ -246,16 +243,14 @@ private static void createConstructor(AbstractASTTransformation xform, Annotatio
}

if (includes != null) {
Comparator<Parameter> includeComparator = Comparator.comparingInt(p -> includes.indexOf(p.getName()));
params.sort(includeComparator);
params.sort(Comparator.comparingInt(p -> includes.indexOf(p.getName())));
}

boolean hasMapCons = AnnotatedNodeUtils.hasAnnotation(cNode, MapConstructorASTTransformation.MY_TYPE);
int modifiers = getVisibility(anno, cNode, ConstructorNode.class, ACC_PUBLIC);
ConstructorNode consNode = addGeneratedConstructor(cNode, modifiers, params.toArray(Parameter.EMPTY_ARRAY), ClassNode.EMPTY_ARRAY, body);
if (namedVariant) {
BlockStatement inner = new BlockStatement();
Parameter mapParam = param(GenericsUtils.nonGeneric(MAP_TYPE), NAMED_ARGS);
Parameter mapParam = param(ClassHelper.MAP_TYPE.getPlainNodeReference(), NAMED_ARGS);
List<Parameter> genParams = new ArrayList<>();
genParams.add(mapParam);
ArgumentListExpression args = new ArgumentListExpression();
Expand All @@ -280,16 +275,17 @@ private static void createConstructor(AbstractASTTransformation xform, Annotatio
// we don't do it for LinkedHashMap for now (would lead to duplicate signature)
// or if there is only one Map property (for backwards compatibility)
// or if there is already a @MapConstructor annotation
if (!params.isEmpty() && defaults && !hasMapCons && specialNamedArgCase) {
if (defaults && specialNamedArgCase && !params.isEmpty()
&& !AnnotatedNodeUtils.hasAnnotation(cNode, MapConstructorASTTransformation.MY_TYPE)) {
ClassNode firstParamType = params.get(0).getType();
if (params.size() > 1 || isObjectType(firstParamType)) {
if (params.size() > 1 || ClassHelper.isObjectType(firstParamType)) {
String message = "The class " + cNode.getName() + " was incorrectly initialized via the map constructor with null.";
addSpecialMapConstructors(modifiers, cNode, message, false);
}
}
}

private static Parameter createParam(FieldNode fNode, String name, boolean defaults, AbstractASTTransformation xform, boolean makeImmutable) {
private static Parameter createParam(final FieldNode fNode, final String name, final boolean defaults, final AbstractASTTransformation xform, final boolean makeImmutable) {
Parameter param = new Parameter(fNode.getType(), name);
if (defaults) {
param.setInitialExpression(providedOrDefaultInitialValue(fNode));
Expand All @@ -312,7 +308,7 @@ private static Expression providedOrDefaultInitialValue(final FieldNode fNode) {
return init;
}

public static void addSpecialMapConstructors(int modifiers, ClassNode cNode, String message, boolean addNoArg) {
public static void addSpecialMapConstructors(final int modifiers, final ClassNode cNode, final String message, final boolean addNoArg) {
Parameter[] parameters = params(new Parameter(LHMAP_TYPE, NAMED_ARGS));
BlockStatement code = new BlockStatement();
VariableExpression namedArgs = varX(NAMED_ARGS);
Expand All @@ -329,11 +325,11 @@ public static void addSpecialMapConstructors(int modifiers, ClassNode cNode, Str
}
}

private static BlockStatement illegalArgumentBlock(String message) {
return block(throwS(ctorX(make(IllegalArgumentException.class), args(constX(message)))));
private static BlockStatement illegalArgumentBlock(final String message) {
return block(throwS(ctorX(ClassHelper.make(IllegalArgumentException.class), args(constX(message)))));
}

private static BlockStatement processArgsBlock(ClassNode cNode, VariableExpression namedArgs) {
private static BlockStatement processArgsBlock(final ClassNode cNode, final VariableExpression namedArgs) {
BlockStatement block = new BlockStatement();
List<PropertyNode> props = new ArrayList<>();
for (PropertyNode pNode : cNode.getProperties()) {
Expand Down
Loading

0 comments on commit 8d19017

Please sign in to comment.