Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions gradle/verification-metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,14 @@
<trust file=".*-sources[.]jar" regex="true" reason="IDEs download sources jars during Gradle import; they are not on compile/runtime classpaths"/>
</trusted-artifacts>
<ignored-keys>
<ignored-key id="1364C5E2DF3E99C5" reason="Key couldn't be downloaded from any key server"/>
<ignored-key id="386F8FCD70E6AF64" reason="Key couldn't be downloaded from any key server"/>
<ignored-key id="4406B84C1661DCD1" reason="Key couldn't be downloaded from any key server"/>
<ignored-key id="58BD11D32974088F" reason="Key couldn't be downloaded from any key server"/>
<ignored-key id="61651257058D3757" reason="Signs org.testfx:openjfx-monocle; not published on any key server, so that jar is pinned by checksum"/>
<ignored-key id="66F8E4860BF74983" reason="Key couldn't be downloaded from any key server"/>
<ignored-key id="700E4F39BC05364B" reason="Key couldn't be downloaded from any key server"/>
<ignored-key id="739D53C0A4CF0287" reason="Signs com.h2database:h2; not published on any key server, so that jar is pinned by checksum"/>
<ignored-key id="80EF47E549604C05" reason="Key couldn't be downloaded from any key server"/>
<ignored-key id="91C65920226E26BD" reason="Key couldn't be downloaded from any key server"/>
<ignored-key id="A8EE76AA8854F700" reason="Key couldn't be downloaded from any key server"/>
Expand Down Expand Up @@ -860,6 +865,11 @@
<sha512 value="91b1127c967f765da06ca16aee7206aac74c83b28cdfd493b7ed24961abad4717c6b1230fc3e517ad5f17a8c47d01f2b12d2ab0c9d372ec218bc94b3230a0983" origin="Generated by Gradle" reason="A key couldn't be downloaded"/>
</artifact>
</component>
<component group="com.h2database" name="h2" version="2.5.252">
<artifact name="h2-2.5.252.jar">
<sha512 value="13fde1ed16eb80f169f204f0d61270f8dc6e98edd01e464d05e74502137d39ed2c712de4d8768d459392fe4b84cf385c161aec26502cfc342bb218596d205f96" origin="Generated by Gradle" reason="A key couldn't be downloaded"/>
</artifact>
</component>
<component group="com.h3xstream.findsecbugs" name="findsecbugs-plugin" version="1.13.0">
<artifact name="findsecbugs-plugin-1.13.0.jar">
<pgp value="CFC10B69382CBCF5387E51484ECE492B63E38ACF"/>
Expand Down Expand Up @@ -1208,6 +1218,12 @@
<pgp value="48B086A7D843CFA258E83286928FBF39003C0425"/>
</artifact>
</component>
<component group="io.r2dbc" name="r2dbc-spi" version="1.0.0.RELEASE">
<artifact name="r2dbc-spi-1.0.0.RELEASE.jar">
<sha256 value="a5846c59fea336431a4ae72ca14edbf5299b78486fa308eafb383f4ae0ea74e5" origin="Generated by Gradle" reason="A key couldn't be downloaded"/>
<sha512 value="c5890241d219e903b48c3666ef376ec2394a5715e7c5a8a6b5209e2d3b3676a13405d77288e6bc9a7e59782de9f254ab4b3e272a4ce970385ce3d82453886209" origin="Generated by Gradle" reason="A key couldn't be downloaded"/>
</artifact>
</component>
<component group="io.reactivex.rxjava3" name="rxjava" version="3.1.10">
<artifact name="rxjava-3.1.10.jar">
<pgp value="E9CC3CD1AE59E851E4DB3FA350FFD7487D34B5B9"/>
Expand Down Expand Up @@ -3129,6 +3145,12 @@
<sha512 value="ac2ced8c9308a6f1606dbd4e15dce428d0fecef9bbd16d3a350368067af7a4aab3773242f46a7d89b9f7422942ba4fc10582232977a3d47240686f8c1a5e66f6" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="org.jooq" name="jooq" version="3.19.36">
<artifact name="jooq-3.19.36.jar">
<sha256 value="3f155f81c017a1ab456959cedb5597bfe52a893115b7dcd91514c0a5b56138dd" origin="Generated by Gradle" reason="A key couldn't be downloaded"/>
<sha512 value="db9cfe587707e5102ecd8890e619de780517392d14cfedf6628c7ca17c3be2ef6b80a5754b458822397d68c8ff67f550b2717f9728ad6f5146e2a79ed395690d" origin="Generated by Gradle" reason="A key couldn't be downloaded"/>
</artifact>
</component>
<component group="org.jparsec" name="jparsec" version="3.1">
<artifact name="jparsec-3.1.jar">
<pgp value="E9C9747A809C424B5BC9DD4A4783A0225D80620F"/>
Expand Down Expand Up @@ -3604,6 +3626,12 @@
<sha512 value="adcc480f68828ffd68d03846be852988b595c2e1bb69224d273578dd6c2ad2773edfe96625a7c00bc40ae0f2d1cac8412eaa54b88cc8e681b0b4c0ee3b082333" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="org.reactivestreams" name="reactive-streams" version="1.0.3">
<artifact name="reactive-streams-1.0.3.jar">
<sha256 value="1dee0481072d19c929b623e155e14d2f6085dc011529a0a0dbefc84cf571d865" origin="Generated by Gradle" reason="A key couldn't be downloaded"/>
<sha512 value="aafc86186d82b4f3bd44ef7f955978481172eab267d208aa80b24f8ca218bd902d69b587fcae2fe44a888b7a9f81fc5a209e837958307fdc0ee815248f7065f9" origin="Generated by Gradle" reason="A key couldn't be downloaded"/>
</artifact>
</component>
<component group="org.reactivestreams" name="reactive-streams" version="1.0.4">
<artifact name="reactive-streams-1.0.4.jar">
<pgp value="50A628FFAF58480736B1079FD1031D14464180E0"/>
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def subprojects = [
'groovy-callsite',
'groovy-logging-test',
'groovy-ginq',
'groovy-ginq-sql',
'groovy-http-builder',
'groovy-markdown',
'groovy-macro',
Expand Down
123 changes: 122 additions & 1 deletion src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,126 @@ public static ClassNode erasure(final ClassNode type) {
return t.getPlainNodeReference();
}

/**
* Type arguments safe to store on {@code owner}. A class that declares no
* type parameters gets none. Each argument is a reference type.
* <p>
* The declaration is read from the class, not from whether a use site has
* already been resolved. A primary class records its formals on itself.
* A use-site proxy reads the redirect target. An unresolved name has no
* declaration yet, so its arguments are left in place. A source use site
* is not rewritten; the generics check reports a type argument that is
* not well-formed. Callers that build a type, such as inference or
* substitution, use this method so the type they store is one a signature
* can carry.
*
* @param owner the parameterized type
* @param arguments the arguments about to be stored
* @return the arguments to store, or {@code null} when the class declares none
*/
public static GenericsType[] legalTypeArguments(final ClassNode owner, final GenericsType[] arguments) {
if (arguments == null) return null;
if (arguments.length > 0 && declaresNoTypeParameters(owner)) return null;
GenericsType[] legal = new GenericsType[arguments.length];
boolean changed = false;
for (int i = 0; i < arguments.length; i += 1) {
legal[i] = asReferenceTypeArgument(arguments[i]);
changed |= legal[i] != arguments[i];
}
return changed ? legal : arguments;
}

/**
* Whether {@code type}'s declaration has no type parameters.
* {@link ClassNode#redirect()} is never null. A primary class is not
* resolved to a {@code Class}, and its formals are the ones the parser
* recorded. A loaded class whose formals were not copied onto the node
* answers from {@code Class.getTypeParameters()}. An unresolved use site
* answers {@code false} because the declaration is not this node; its
* own generics are the arguments.
*/
private static boolean declaresNoTypeParameters(final ClassNode type) {
if (type == null || type.isGenericsPlaceHolder()) return false;
ClassNode declared = type.redirect();
Comment thread
blackdrag marked this conversation as resolved.
Resolved
if (declared.isArray()) return false;
if (declared.isPrimaryClassNode()) {
// A source class records its formals on the primary node. A synthetic
// carrier such as the closure-hint class has no module and uses the
// node only to hold the arguments being inferred, so a missing formal
// list is not a declaration of a raw type.
if (declared.getModule() == null) return false;
GenericsType[] formals = declared.getGenericsTypes();
return formals == null || formals.length == 0;
}
if (declared != type) {
GenericsType[] formals = declared.getGenericsTypes();
if (formals != null) return formals.length == 0;
if (declared.isResolved()) {
return declared.getTypeClass().getTypeParameters().length == 0;
}
return false;
}
if (declared.isResolved()) {
return declared.getTypeClass().getTypeParameters().length == 0;
}
return false;
}

/**
* A type argument or wildcard bound is a reference type (JLS 4.5).
* A primitive is replaced by its wrapper. Nested arguments are corrected
* the same way. Placeholders are left as placeholders.
*
* @param type the type argument
* @return a type argument javac could write, or {@code type} when it already is one
*/
public static GenericsType asReferenceTypeArgument(final GenericsType type) {
if (type == null) return null;
ClassNode base = referenceType(type.getType());
ClassNode[] upper = referenceTypes(type.getUpperBounds());
ClassNode lower = referenceType(type.getLowerBound());
if (base == type.getType() && upper == type.getUpperBounds() && lower == type.getLowerBound()) {
return type;
}
GenericsType corrected = new GenericsType(base, upper, lower);
corrected.setWildcard(type.isWildcard());
return corrected;
}

private static ClassNode referenceType(final ClassNode type) {
if (type == null || type.isGenericsPlaceHolder()) return type;
if (ClassHelper.isPrimitiveType(type) && !ClassHelper.isPrimitiveVoid(type)) {
return ClassHelper.getWrapper(type);
}
GenericsType[] arguments = type.getGenericsTypes();
if (arguments == null || arguments.length == 0) return type;
GenericsType[] corrected = null;
for (int i = 0; i < arguments.length; i += 1) {
GenericsType argument = asReferenceTypeArgument(arguments[i]);
if (argument != arguments[i]) {
if (corrected == null) corrected = Arrays.copyOf(arguments, arguments.length);
corrected[i] = argument;
}
}
if (corrected == null) return type;
ClassNode copy = type.getPlainNodeReference();
copy.setGenericsTypes(corrected);
return copy;
}

private static ClassNode[] referenceTypes(final ClassNode[] types) {
if (types == null) return null;
ClassNode[] corrected = null;
for (int i = 0; i < types.length; i += 1) {
ClassNode type = referenceType(types[i]);
if (type != types[i]) {
if (corrected == null) corrected = Arrays.copyOf(types, types.length);
corrected[i] = type;
}
}
return corrected == null ? types : corrected;
}

/**
* @since 2.3.0
*/
Expand Down Expand Up @@ -525,7 +645,8 @@ public static ClassNode correctToGenericsSpecRecurse(Map<String, ClassNode> gene
}
}
}
return makeClassSafeWithGenerics(type, newgTypes);
GenericsType[] legal = legalTypeArguments(type, newgTypes);
return makeClassSafeWithGenerics(type, legal == null ? EMPTY_GENERICS_ARRAY : legal);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,43 @@ private static ClassNode parameterizeLowestUpperBound(final LowestUpperBoundCont
lubGTs[i] = basicType.asGenericsType();
}
}
int declared = declaredTypeParameterCount(lub);
if (declared == 0) {
GenericsType[] existing = lub.getGenericsTypes();
if (existing != null && existing.length > 0) {
return lub.getPlainNodeReference();
}
return lub;
}
if (declared > 0 && declared != n) {
return lub.getPlainNodeReference();
}
return GenericsUtils.makeClassSafe0(lub, lubGTs);
}

/**
* Number of type parameters {@code type}'s declaration has, or {@code -1}
* when that declaration is not known. A lowest upper bound is not a
* generic class of its own.
*/
private static int declaredTypeParameterCount(final ClassNode type) {
if (type == null || type.isGenericsPlaceHolder() || type.isArray()) return -1;
if (type instanceof LowestUpperBoundClassNode) return -1;
ClassNode declared = type.redirect();
if (declared instanceof LowestUpperBoundClassNode || declared.isArray()) return -1;
if (declared.isPrimaryClassNode()) {
if (declared.getModule() == null) return -1;
GenericsType[] formals = declared.getGenericsTypes();
return formals == null ? 0 : formals.length;
}
if (!declared.isResolved()) return -1;
if (declared != type) {
GenericsType[] formals = declared.getGenericsTypes();
if (formals != null) return formals.length;
}
return declared.getTypeClass().getTypeParameters().length;
}

/**
* Tracks pairs of types whose LUB is currently being computed by
* {@link #lowestUpperBound(ClassNode, ClassNode)}, so the recursion can
Expand Down Expand Up @@ -635,15 +669,14 @@ public LowestUpperBoundClassNode(final String name, final ClassNode upper, final
for (ClassNode i: interfaces) sj.add(i.getText());
text = sj.toString();

if (isUsingGenerics()) {
List<GenericsType> generics = new ArrayList<>();
if (upper.getGenericsTypes() != null)
Collections.addAll(generics, upper.getGenericsTypes());
for (ClassNode i : interfaces) {
if (i.getGenericsTypes() != null)
Collections.addAll(generics, i.getGenericsTypes());
}
setGenericsTypes(generics.toArray(GenericsType.EMPTY_ARRAY));
// Arguments belong to the class this node is written as. Copying
// every interface's arguments would parameterize a class that
// declares none when getName() reports that class (Serializable
// sorted ahead of Comparable).
GenericsType[] representative = compileTimeClassNode.getGenericsTypes();
if (representative != null && representative.length > 0
&& representative.length == declaredTypeParameterCount(compileTimeClassNode)) {
setGenericsTypes(representative);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.codehaus.groovy.classgen.asm;

import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.GenericsType;

/**
* Writes the JVMS 4.7.9.1 nested form of a parameterized type for a Signature
Expand All @@ -42,11 +43,54 @@ static void writeParameterizedClass(final StringBuilder ret, final ClassNode pri
writeParameterizedClass(ret, owner);
ret.append('.');
ret.append(innerClassSimpleName(printType, owner));
BytecodeHelper.addSubTypes(ret, printType.getGenericsTypes(), "<", ">");
BytecodeHelper.addSubTypes(ret, signatureArguments(printType), "<", ">");
return;
}
ret.append(BytecodeHelper.getTypeDescription(printType, false));
BytecodeHelper.addSubTypes(ret, printType.getGenericsTypes(), "<", ">");
BytecodeHelper.addSubTypes(ret, signatureArguments(printType), "<", ">");
}

/**
* Type arguments safe to write for a resolved declaration. A signature
* cannot name arguments a class does not declare, so a count that does
* not match the declaration is omitted. An unresolved name is left
* unchanged. A source use site that does not match is rejected before
* bytecode; a type built later, such as a lowest upper bound, may still
* carry arguments this method has to drop.
*/
private static GenericsType[] signatureArguments(final ClassNode printType) {
GenericsType[] useSite = printType.getGenericsTypes();
if (useSite == null || useSite.length == 0) {
return useSite;
}
int declared = declaredTypeParameters(printType);
if (declared >= 0 && declared != useSite.length) {
return null;
}
return useSite;
}

/**
* Number of type parameters the class declares, or {@code -1} when this
* writer does not know. Unresolved names are not loaded here. A primary
* node with no module is not a source class, so generics on it are not
* a declaration.
*/
private static int declaredTypeParameters(final ClassNode printType) {
if (printType.isGenericsPlaceHolder()) return -1;
ClassNode declared = printType.redirect();
if (declared.isArray()) return -1;
if (declared.isPrimaryClassNode()) {
if (declared.getModule() == null) return 0;
GenericsType[] formals = declared.getGenericsTypes();
return formals == null ? 0 : formals.length;
}
if (!declared.isResolved()) return -1;
if (declared != printType) {
GenericsType[] formals = declared.getGenericsTypes();
if (formals != null) return formals.length;
}
return declared.getTypeClass().getTypeParameters().length;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,19 @@ protected ClassNode createClosureClass(final ClosureExpression expression, final
if (SystemUtil.getBooleanSafe(CompilerConfiguration.CLOSURE_SAM_CONVERSION)) {
SamAdapterFactory.prepare(controller, closureClass, doCallMethod, expression);
}
closureClass.putNodeMetaData(StaticCompilationMetadataKeys.STATIC_COMPILE_NODE, Boolean.TRUE);
// A closure marked DYNAMIC_RESOLUTION is emitted with the dynamic writer.
// The enclosing method stays static. Nested lambdas then use closure
// capture, which a static lambda does not do for a variable of an
// enclosing closure. The marker is Boolean.TRUE only for that switch;
// a type-checking extension's per-expression makeDynamic stores a type.
boolean dynamic = Boolean.TRUE.equals(expression.getNodeMetaData(DYNAMIC_RESOLUTION));
Boolean mode = dynamic ? Boolean.FALSE : Boolean.TRUE;
closureClass.putNodeMetaData(StaticCompilationMetadataKeys.STATIC_COMPILE_NODE, mode);
for (MethodNode method : closureClass.getMethods()) {
if (method.getDeclaringClass() == closureClass) {
method.putNodeMetaData(StaticCompilationMetadataKeys.STATIC_COMPILE_NODE, mode);
}
}
return closureClass;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ public StaticTypesMethodReferenceExpressionWriter(final WriterController control
/** {@inheritDoc} */
@Override
public void writeMethodReferenceExpression(final MethodReferenceExpression methodReferenceExpression) {
// Marked pointers are Groovy method pointers. The metafactory would
// insert a cast the dynamic pointer does not.
if (methodReferenceExpression.getNodeMetaData(StaticTypesMarker.DYNAMIC_RESOLUTION) != null) {
super.writeMethodReferenceExpression(methodReferenceExpression);
return;
}
FunctionalInterfaceContext functionalInterface = resolveFunctionalInterfaceContext(methodReferenceExpression);
if (functionalInterface == null) {
// generate the default bytecode -- most likely a method closure
Expand Down
Loading
Loading