eclipse-xtext / eclipse-xtext/xtext

Can't create an anonymous object with private field

Open
#2,256 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
831
Forks
330
Avg merge
3d 7h
Merged PRs (30d)
12

Description

It's not possible to create an anonymous object with a private field in a constructor that already calls the super because in the generated code an abstract class will be generated before the super call.

package foo

import java.util.ArrayList

class Test extends ArrayList<Integer> {

	val Comparable<Integer> a

	new() {
		super() // error here: Java problem: Constructor call must be the first statement in a constructor
		this.a = new Comparable<Integer>() {
			private Integer foo = 5
			override compareTo(Integer i) {
				return foo
			}
		}
		a.compareTo(5)
	}
}

This is however allowed in Java.

package foo;

import java.util.ArrayList;

public class Test extends ArrayList<Integer> {
	private final Comparable<Integer> a;

	public Test() {
		super();
		this.a = new Comparable<Integer>() {
			private Integer foo = 5;

			@Override
			public int compareTo(Integer i) {
				return foo;
			}
		};
		a.compareTo(5);
	}
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducing source example in the issue and inspect the generated Java for the constructor and anonymous object. Compare its ordering with the valid Java example; done means the example compiles without the super-call error while preserving the private field and its use.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
compilers
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.