eclipse-jdt / eclipse-jdt/eclipse.jdt.core

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

Open
#1,262 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 10h
Merged PRs (30d)
49

Description

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.

```java
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 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:

```java
/*
* 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:

```xml

org.springframework.boot
spring-boot-dependencies
3.1.2
pom
import

org.springframework.boot
spring-boot-starter-validation

org.springframework.boot
spring-boot-starter-web

```

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the Java record example with OpenJDK 17 in Eclipse EE 2023-09 M1. Compare the constant-valued annotation, the literal annotation, and no annotation, then narrow the failure to record accessor resolution during annotation constant lookup. Done means Eclipse resolves bar() correctly in the constant case without regressing the other cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.