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

Adding a field to an anonymous class in a method produces invalid Java code #2330

Open
dpetroff opened this issue Jun 17, 2022 · 6 comments
Open

Comments

@dpetroff
Copy link

import org.eclipse.emf.common.util.AbstractTreeIterator

class Test {
  def test() {
    new AbstractTreeIterator(new Object()) {
      val thing = newLinkedHashSet
      override protected getChildren(Object object) {
        throw new UnsupportedOperationException("TODO: auto-generated method stub")
      }
    }
  }
}

The 2.26.0.v20220228-0901 compiler produced invalid Java for this by defining a constructor with a final ? object argument:

  public AbstractTreeIterator test() {
    abstract class __Test_1 extends AbstractTreeIterator {
      __Test_1(final ? object) {
        super(object);
      }
      
      LinkedHashSet<Object> thing;
    }
    
    Object _object = new Object();
    return new __Test_1(_object) {
      {
        thing = CollectionLiterals.<Object>newLinkedHashSet();
      }
      @Override
      protected Iterator getChildren(final Object object) {
        throw new UnsupportedOperationException("TODO: auto-generated method stub");
      }
    };
  }

It's also a bit odd that it's creating a local named class inside the method only to leave all of the initialisation and override logic in an anonymous subclass of that class it has just defined anyway. Maybe this is related, but I can live with it if it's not.

I'm sure that I've defined a field inside an anonymous class elsewhere, so I can only assume that there's something specific about AbstractTreeIterator that breaks it. new AbstractTreeIterator<Object>(new Object()) { ... } doesn't avoid the issue.

It's easy enough to define the field as a local variable outside the anonymous class, but that's a bit naff.

@cdietrich
Copy link
Member

cdietrich commented Jun 17, 2022

is this a problem with 2.27 or 2.26 ?!? it i a regression or just not working
is this a side effect of eclipse/xtext-xtend#1301
help in fixing this would be welcome

@cdietrich
Copy link
Member

might also be related to #2256

@cdietrich
Copy link
Member

cdietrich commented Jun 17, 2022

@szarnekow any idea why the type inference in XtendJvmModelInferrer.inferLocalClass(AnonymousClass) not working? can it be we dont handle generics at all there?

@dpetroff
Copy link
Author

I've just upgraded to 2.27 and have the issue in both 2.26 and 2.27.

@cdietrich
Copy link
Member

so it is not a regression

@szarnekow
Copy link
Contributor

The compile strategy for anon inner classes makes a deliberate named class as a basetype since Xtend allows to write code like

	def void m() {
		var list = newArrayList
		list.add(new Object() {
			def void newMethod() { .. }		  
		});
		list.head.newMethod
	}

Without the abstract base class being emitted it wouldn't be valid Java source.
However, the strategy fails to compile correctly for constructors. This would need some refinement indeed.

@cdietrich cdietrich transferred this issue from eclipse/xtext-xtend Apr 17, 2023
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

3 participants