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

Eclipse EE 2023-09 M1 doesn't find record accessor using annotation with constant. #1262

Open
garretwilson opened this issue Aug 2, 2023 · 5 comments

Comments

@garretwilson
Copy link

I just upgraded to Eclipse EE 2023-09 M1 (from 2023-06) and it breaks one of my Java record definitions. I'm using OpenJDK 17 on Windows. I'm using Spring Boot 3.1.2, but I'm guessing the bug probably has to do with annotations, not with Spring Boot itself.

I have the following record class.

package com.example;

import java.net.URI;
import java.util.Optional;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(FooConfigurationProperties.CONFIG_KEY_BASE)
public record FooConfigurationProperties(URI bar) {

  public static final String CONFIG_KEY_BASE = "foo";

  public Optional<URI> findBar() {
    return Optional.ofNullable(bar());
  }

}

Eclipse claims that the line return Optional.ofNullable(bar()) contains an error, because it can't find a bar() method. Of course we know that this is a record type, and Java generates a bar() method automatically.

Now here is the strange behavior; likely it will give you a clue to where the problem is:

  • If I change @ConfigurationProperties(FooConfigurationProperties.CONFIG_KEY_BASE) to @ConfigurationProperties("foo"), then the code work! Both versions should work, because FooConfigurationProperties.CONFIG_KEY_BASE is a compile-time literal constant. And the annotation should have no bearing on the accessor to a record property. But it does.
  • If I remove @ConfigurationProperties altogether, the code works as well!

There's something to do with the compiler needing to look up the compile-time constant to include it in an annotation that is making it confused about what accessors are present in the record. (Perhaps the annotation compiler code changed and a reference to the record class got criss-crossed with a reference to the annotation class, so it's searching for bar() on the annotation? Just a wild guess.)

The Spring Annotation @ConfigurationProperties is the only thing in this case that is not pure Java. The annotation is org.springframework.boot.context.properties.ConfigurationProperties. I'll include here in abridged form for your convenience:

/*
 * Copyright 2012-2023 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * …
 */

package org.springframework.boot.context.properties;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.boot.context.properties.bind.ConstructorBinding;
import org.springframework.core.annotation.AliasFor;
import org.springframework.stereotype.Indexed;

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface ConfigurationProperties {

  @AliasFor("prefix")
  String value() default "";

  @AliasFor("value")
  String prefix() default "";

  boolean ignoreInvalidFields() default false;

  boolean ignoreUnknownFields() default true;

}

Here is the Maven declaration of the Spring Boot dependencies:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-dependencies</artifactId>
  <version>3.1.2</version>
  <type>pom</type>
  <scope>import</scope>
</dependency>

…

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
@garretwilson
Copy link
Author

garretwilson commented Aug 2, 2023

There's something weird going on with record accessors in Eclipse EE 2023-09 M1.

I had a separate record Foo which was showing the same error accessing bar(), yet there was no Spring annotation, or any annotations at all! In order to try to narrow down the issue, I copied the record to Foo2. Suddenly the error went away both in Foo and in Foo2, so I was able to delete Foo2 and the original code Foo was working again.

@garretwilson
Copy link
Author

Unfortunately the other class (the one without the annotation, in my other comment above) keeps reverting to the error not not being able to find bar().

It's even worse now: another class is trying to access foo.bar(), and that class is giving errors saying it can't find bar(). Eclipse EE 2023-09 M1 broke something with Java record accessors.

I need to work; I lost the whole morning just dealing with this and #1261 (along with eclipse-m2e/m2e-core#1414). I'm going to have to downgrade to Eclipse EE 2023-06 for now.

@iloveeclipse
Copy link
Member

@stephan-herrmann : I might be wrong, this is probably duplicate of #1258 and should be fixed on master.

@garretwilson: If you can, please update to latest I Build from https://download.eclipse.org/eclipse/downloads/ and check.

@garretwilson
Copy link
Author

garretwilson commented Aug 4, 2023

@iloveeclipse thanks for getting back to me on this.

I'd love to help verify that it's been fixed, but I also have to balance my ability to progress in work. If you think this issue and #1261 have both been fixed in 2023-09 M2, I'll gladly update and check it out when it's released. Otherwise I'll wait for an M/RC build that fixes both before updating.

@stephan-herrmann
Copy link
Contributor

this is probably duplicate of #1258 and should be fixed on master

Yes, it looks very much like that.

To verify I added an almost empty @interface ConfigurationProperties to the original example and compared ecj versions:

  • 4.19M1: "The method bar() is undefined for the type FooConfigurationProperties"
  • I20230804-1800: no error reported.

I did not test other variations of the problem.

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

No branches or pull requests

3 participants