Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JDK 14 + preview-features not working correctly #1167

Closed
mojo2012 opened this issue Oct 1, 2020 · 7 comments
Closed

JDK 14 + preview-features not working correctly #1167

mojo2012 opened this issue Oct 1, 2020 · 7 comments
Assignees
Milestone

Comments

@mojo2012
Copy link

mojo2012 commented Oct 1, 2020

Hello,

our project uses maven with eclipse 4.16 and 4.17, latest greclipse release (also same on snapshot) java 14, groovy 3 and joint compilation, configured like this:

<plugin>
	<artifactId>maven-compiler-plugin</artifactId>
	<extensions>true</extensions>
	<dependencies>
		<dependency>
			<groupId>org.codehaus.groovy</groupId>
			<artifactId>groovy-eclipse-compiler</artifactId>
			<version>3.6.0-03</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.groovy</groupId>
			<artifactId>groovy-eclipse-batch</artifactId>
			<version>3.0.5-01</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.groovy</groupId>
			<artifactId>groovy</artifactId>
			<version>3.05</version>
			<classifier>indy</classifier>
		</dependency>
	</dependencies>
	<configuration>
		<compilerId>groovy-eclipse-compiler</compilerId>
		<compilerArgs>
			--enable-preview
		</compilerArgs>
		<compilerArguments>
			<indy />
			<configScript>${pom.basedir}/config.groovy</configScript>
		</compilerArguments>
		<failOnWarning>false</failOnWarning>
		<showWarnings>false</showWarnings>
		<verbose>false</verbose>
	</configuration>
</plugin>

Today I updated the groovy plugin and all of a sudden the code in eclipse doesnot compile anymore.
This error is reported:
Groovy:General error during conversion: java.lang.UnsupportedClassVersionError: Preview features are not enabled for Expression$47d4a77a$cf5f$4775$8b57$07d99aa7e980 (class file version 58.65535). Try running with '--enable-preview' PriceData.groovy

Preview features are enabled on the Eclipse jvm compiler settings too. And it worked up till now!

Yesterday (or the day before) there was an update too and there it still worked.

@eric-milles
Copy link
Member

eric-milles commented Oct 1, 2020

58.65535 says to me that the class file was built with preview features enabled. If I understand the error message correctly, it is saying that when the class file is loaded by the JVM, that preview features are not enabled. That is, enable-preview was true for compile but not for execute.

I think a workaround would be to set "--enable-preview" on the java that runs your eclipse (or maven as the case may be).

Update: I was able to generate a similar error and resolve it for a maven build like this: https://github.com/groovy/groovy-eclipse/tree/master/extras/groovy-eclipse-compiler-tests/src/it/enable-preview/pom.xml

I did not see an error during conversion. I will need more context to understand that.

@eric-milles
Copy link
Member

This may not mean much, but for reference this is the code that generates the "Expression$..." class (from org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport):

    public static Object evaluateExpression(Expression expr, CompilerConfiguration config) {
        String className = "Expression$" + UUID.randomUUID().toString().replace('-', '$');
        ClassNode node = new ClassNode(className, Opcodes.ACC_PUBLIC, OBJECT_TYPE);
        ReturnStatement code = new ReturnStatement(expr);
        addGeneratedMethod(node, "eval", Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, code);
        CompilerConfiguration copyConf = new CompilerConfiguration(config);
        CompilationUnit cu = new CompilationUnit(copyConf);
        cu.addClassNode(node);
        cu.compile(Phases.CLASS_GENERATION);
        List<GroovyClass> classes = (List<GroovyClass>) cu.getClasses();
        Class aClass = cu.getClassLoader().defineClass(className, classes.get(0).getBytes());
        try {
            return aClass.getMethod("eval").invoke(null);
        } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            throw new GroovyBugError(e);
        }
    }

Your groovy source (PriceData.grooy I think) would be run through this if you use @ClosureParams or @DelegatesTo annotations.

@mojo2012
Copy link
Author

mojo2012 commented Oct 7, 2020

This is the source of the classes

@edu.umd.cs.findbugs.annotations.SuppressFBWarnings([
	"MF_CLASS_MASKS_FIELD", "EI_EXPOSE_REP", "EI_EXPOSE_REP2", "HE_EQUALS_NO_HASHCODE",
	"EQ_DOESNT_OVERRIDE_EQUALS", "ES_COMPARING_STRINGS_WITH_EQ", "HE_INHERITS_EQUALS_USE_HASHCODE",
	"EC_UNRELATED_TYPES_USING_POINTER_EQUALITY", "EC_UNRELATED_TYPES_USING_POINTER_EQUALITY"
])
@JsonInclude(Include.NON_NULL)
@Sortable(includes = ["value", "currencyCode"])
@Canonical
@ToString(includes = ["currencyCode", "value"], ignoreNulls = true, includeNames = true, includePackage = false)
class PriceData extends Bean {

	Boolean customPrice
	Boolean fromPrice
	Boolean maxWeightExceeded

	@JsonInclude(Include.ALWAYS)
	BigDecimal value

	String currencyCode

	PriceData() {
	}

	PriceData(Currency currency) {
		this.currencyCode = currency?.getCurrencyCode()
	}

	PriceData(double value, Currency currency) {
		this(BigDecimal.valueOf(value), currency)
	}

	PriceData(BigDecimal value, Currency currency) {
		this(currency)
		this.value = value
	}

	boolean isFromPrice() {
		return fromPrice != null ? fromPrice : false
	}

	boolean isCustomPrice() {
		return customPrice != null ? customPrice : false
	}

	void setCustomPrice(Boolean customPrice) {
		this.customPrice = customPrice;
	}

	@JsonIgnore
	Currency getCurrency() {
		return currencyCode != null ? Currency.getInstance(currencyCode) : null
	}
}

import org.apache.commons.lang3.builder.EqualsBuilder
import org.apache.commons.lang3.builder.HashCodeBuilder
import org.apache.commons.lang3.builder.ReflectionToStringBuilder
import org.apache.commons.lang3.builder.ToStringStyle

@edu.umd.cs.findbugs.annotations.SuppressFBWarnings([ "MF_CLASS_MASKS_FIELD",
	"EI_EXPOSE_REP", "EI_EXPOSE_REP2", "HE_EQUALS_NO_HASHCODE",
	"EQ_DOESNT_OVERRIDE_EQUALS", "EQ_UNUSUAL" ])
@JsonInclude(value = Include.NON_NULL)
@JsonIgnoreProperties(["metaClass"])
abstract class Bean {

	LocalDateTime createdAt
	LocalDateTime modifiedAt

	@Override
	int hashCode() {
		return HashCodeBuilder.reflectionHashCode(this, true)
	}

	@Override
	boolean equals(Object obj) {
		return EqualsBuilder.reflectionEquals(this, obj, true)
	}
	
	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this, ToStringStyle.JSON_STYLE);
	}
}

@eric-milles
Copy link
Member

And what does your config.groovy script look like? There must be some extra imports at least.

@eric-milles eric-milles added this to the v4.0.0 milestone Oct 7, 2020
@mojo2012
Copy link
Author

mojo2012 commented Oct 7, 2020

import org.codehaus.groovy.ast.ClassNode

import groovy.transform.CompileStatic

withConfig(configuration) {

	// inject compile static annotation into every class except for test classes
	source(classValidator: { ClassNode cn -> !cn.name.endsWith("Spec") && !cn.name.endsWith("SpecIT") }) {
		ast(CompileStatic)
//		ast(TypeChecked)
	}
	
	//enable new groovy 3/java 11 syntax
	//	configuration.pluginFactory = ParserPluginFactory.antlr4(configuration)
}

@eric-milles
Copy link
Member

The latest snapshot builds should not run into this problem for expression evaluation. Any other class loading from your project may still have this issue. You'll have to let me know if you get any more of these with a different stack trace.

If you add --enable-preview to your eclipse.ini below the -vmargs line, this should stop completely.

@eric-milles eric-milles self-assigned this Oct 8, 2020
@mojo2012
Copy link
Author

Seems to work fine again! Thanks for the quick fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants