janino-compiler / janino

Janino is a super-small, super-fast Java™ compiler.
http://janino-compiler.github.io/janino
Other
1.21k stars 205 forks source link

CompilerException with static inner classes #210

Closed Slava96 closed 5 months ago

Slava96 commented 8 months ago

Hello! I try to compile java files generated with Protobuf compiler v24.4, but i receive CompilationException here with such message

org.codehaus.commons.compiler.CompileException: File '/Users/slavajoy/IdeaProjects/protobuf-conversions-generator/generated/java/org/spbstu/paramonov/BestTestClass.java', Line 1065, Column 5: File '/Users/slavajoy/IdeaProjects/protobuf-conversions-generator/generated/java/org/spbstu/paramonov/BestTestClass.java', Line 1065, Column 5: Type "Builder" is ambiguous: com.google.protobuf.MessageLite$Builder vs. com.google.protobuf.AbstractMessageLite$Builder vs. com.google.protobuf.AbstractMessage$Builder vs. org.spbstu.paramonov.BestTestClass$Builder vs. com.google.protobuf.Message$Builder vs. com.google.protobuf.GeneratedMessageV3$Builder.

I attach the file to the issue. Can you give me an answer - how can i fix that? I don't understand why this type is ambigious, if i have such class directly on this file? Also, this files successfully compiles with Java standart compilers.

BestTestClass.txt

Slava96 commented 8 months ago

Please answer urgently, because my diploma project depends on your's compiler. Wait for your feedback in any form.

aunkrig commented 8 months ago

All the superclasses of BestTestClass.Builder declare their own Builder class, and in line 1065 all six are in scope. So, according to JLS 17 6.5.5.1, the simple type name Builder is ambiguous:

If a type name consists of a single Identifier, then the identifier must occur in the scope of exactly one declaration of a class, interface, or type parameter with this name (§6.3), or a compile-time error occurs.

Accordingly, every JLS-conformant Java compiler should report an error here.

Can you please provide the complete stack trace of the error?

aunkrig commented 8 months ago

One workaround would be that you use the fully qualified name org.spbstu.paramonov.BestTestClass.Builder instead of the simple name Builder to resolve the ambiguity.

aunkrig commented 7 months ago

I think I fixed it. The JLS is very unclear here, but obviously the declaring class and its enclosing classes take precedence over the superclasses and their nested classes:

public class Base1 {

    public static void setFirstName(String firstName) {}

    public static class Builder {
        public Builder setFirstName(String firstName) { return this; }
    }
}

public class Base2 extends Base1 {

    public static void setLastName(String lastName) {}

    public static class Builder extends Base1.Builder {
        public Builder setLastName(String lastName) { return this; }
    }     //   ^^^^^^^ <= Does this mean "Base1.Builder" or "Base2.Builder"?
}

public class Derived {

    public static void setAge(int age) {}

    public static class Builder extends Base2.Builder {
        public Builder setAge(int age) { return this; }
    }
}

public class MyClass {
    public static boolean main() {
        new Derived.Builder().setAge(17);
        return true;
    }
}

Now the test case for this issue passes, and the regression test suite still succeeds.

Please test.

aunkrig commented 7 months ago

Ping

Slava96 commented 5 months ago

I will try, thank you!

aunkrig commented 5 months ago

FYI: Version 3.1.12 is out the door and includes this fix. Please test!