phax / jcodemodel

A heavily extended fork of the com.sun.codemodel (from 2013/09)
Other
93 stars 34 forks source link

Missing qualification for inner classes in generic parameters of anonymous classes #84

Closed fbaro closed 4 years ago

fbaro commented 4 years ago

I'm creating a class, called Outer, and an inner class inside it, called Inner.

I'm then using the inner class as a generic parameter of an anonymous class in another class in the same package.

The result is that in the generic parameter the inner class is written unqualified and not imported, resulting in a compile error.

Here is an example code

        JCodeModel cm = new JCodeModel();

        JDefinedClass outerClass = cm._class("test.Outer");
        JDefinedClass innerClass = outerClass._class("Inner");

        JDefinedClass testClass = cm._class("test.Test");
        JAnonymousClass anonymousClass = cm.anonymousClass(cm.ref(List.class).narrow(innerClass));
        testClass.field(0, cm.ref(List.class).narrow(innerClass), "field", JExpr._new(anonymousClass));

And this is the result

package test;

import java.util.List;

public class Test {
    List<Outer.Inner> field = new List<Inner>() {
    }
    ;
}

The declaration List<Outer.Inner> field is correct, but the instantiation new List<Inner> is not.

fbaro commented 4 years ago

I think I have a suggestion for a fix. Inside JAnonymousClass the generate method should be

@Override
  public void generate (final IJFormatter f)
  {
    m_aBaseClass.generate(f);
  }

instead of f.type (m_aBaseClass);

phax commented 4 years ago

Thanks for the suggestion - will check later. Working on sthg else atm.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

phax commented 4 years ago

Part of 3.4.0 release