Janino

Static imports of private fields can cause compiler to throw NPE

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 2.5.12
  • Fix Version/s: 2.5.13
  • Component/s: None
  • Labels:
    None
  • Environment:
    Mac OS
  • Testcase included:
    yes
  • Number of attachments :
    1

Description

As far as I could tell, the bug only shows itself when a similarly named static variable appears in two classes in two different packages with different visibility (public/private). I've zipped a minimal pair of packages that demonstrates the problem.

Steps to reproduce:

1. Unzip the attatched directory.
2. Compile class b.C3 using something like:

-----------
import java.io.File;

import org.codehaus.janino.JavaSourceClassLoader;
import org.codehaus.janino.DebuggingInformation;

public class Test1 {
public static void main(String[] args) throws Exception {
ClassLoader loader = new JavaSourceClassLoader(
Test1.class.getClassLoader(),
new File[]{new File("testsrc")},
null, DebuggingInformation.NONE);

loader.loadClass("b.C3");
}
}
---------------

Above throws following NPE:

Exception in thread "main" java.lang.NullPointerException
at org.codehaus.janino.UnitCompiler.checkAccessible(UnitCompiler.java:4258)
at org.codehaus.janino.UnitCompiler.checkAccessible(UnitCompiler.java:4235)
at org.codehaus.janino.UnitCompiler.compileGet2(UnitCompiler.java:2315)
at org.codehaus.janino.UnitCompiler.access$6300(UnitCompiler.java:51)
at org.codehaus.janino.UnitCompiler$9.visitFieldAccess(UnitCompiler.java:2283)
at org.codehaus.janino.Java$FieldAccess.accept(Java.java:2006)
at org.codehaus.janino.UnitCompiler.compileGet(UnitCompiler.java:2290)
at org.codehaus.janino.UnitCompiler.compileGet2(UnitCompiler.java:2309)
at org.codehaus.janino.UnitCompiler.access$6100(UnitCompiler.java:51)
at org.codehaus.janino.UnitCompiler$9.visitAmbiguousName(UnitCompiler.java:2281)
at org.codehaus.janino.Java$AmbiguousName.accept(Java.java:1942)
at org.codehaus.janino.UnitCompiler.compileGet(UnitCompiler.java:2290)
at org.codehaus.janino.UnitCompiler.compileGetValue(UnitCompiler.java:3231)
at org.codehaus.janino.UnitCompiler.compileArithmeticOperation(UnitCompiler.java:4574)
at org.codehaus.janino.UnitCompiler.compileGet2(UnitCompiler.java:2791)
at org.codehaus.janino.UnitCompiler.access$4400(UnitCompiler.java:51)
at org.codehaus.janino.UnitCompiler$9.visitBinaryOperation(UnitCompiler.java:2263)
at org.codehaus.janino.Java$BinaryOperation.accept(Java.java:2400)
at org.codehaus.janino.UnitCompiler.compileGet(UnitCompiler.java:2290)
at org.codehaus.janino.UnitCompiler.compileGetValue(UnitCompiler.java:3231)
at org.codehaus.janino.UnitCompiler.compileGet2(UnitCompiler.java:2870)
at org.codehaus.janino.UnitCompiler.access$5100(UnitCompiler.java:51)
at org.codehaus.janino.UnitCompiler$9.visitMethodInvocation(UnitCompiler.java:2270)
at org.codehaus.janino.Java$MethodInvocation.accept(Java.java:2530)
at org.codehaus.janino.UnitCompiler.compileGet(UnitCompiler.java:2290)
at org.codehaus.janino.UnitCompiler.compileGetValue(UnitCompiler.java:3231)
at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:1777)
at org.codehaus.janino.UnitCompiler.access$2500(UnitCompiler.java:51)
at org.codehaus.janino.UnitCompiler$6.visitMethodInvocation(UnitCompiler.java:1751)
at org.codehaus.janino.Java$MethodInvocation.accept(Java.java:2530)
at org.codehaus.janino.UnitCompiler.compile(UnitCompiler.java:1771)
at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:1070)
at org.codehaus.janino.UnitCompiler.access$800(UnitCompiler.java:51)
at org.codehaus.janino.UnitCompiler$4.visitExpressionStatement(UnitCompiler.java:625)
at org.codehaus.janino.Java$ExpressionStatement.accept(Java.java:1277)
at org.codehaus.janino.UnitCompiler.compile(UnitCompiler.java:644)
at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:663)
at org.codehaus.janino.UnitCompiler.access$700(UnitCompiler.java:51)
at org.codehaus.janino.UnitCompiler$4.visitBlock(UnitCompiler.java:624)
at org.codehaus.janino.Java$Block.accept(Java.java:1231)
at org.codehaus.janino.UnitCompiler.compile(UnitCompiler.java:644)
at org.codehaus.janino.UnitCompiler.compile(UnitCompiler.java:1662)
at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:375)
at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:261)
at org.codehaus.janino.UnitCompiler$3.visitPackageMemberClassDeclaration(UnitCompiler.java:234)
at org.codehaus.janino.Java$PackageMemberClassDeclaration.accept(Java.java:674)
at org.codehaus.janino.UnitCompiler.compile(UnitCompiler.java:240)
at org.codehaus.janino.UnitCompiler.compileUnit(UnitCompiler.java:218)
at org.codehaus.janino.JavaSourceClassLoader.generateBytecodes(JavaSourceClassLoader.java:333)
at org.codehaus.janino.JavaSourceClassLoader.findClass(JavaSourceClassLoader.java:293)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at Test1.main(Test1.java:14)

Process finished with exit code 1

Activity

Hide
Philip Milne added a comment -

I probably should have used a higher priorty when reporting this since it turned out to be a showstopper for us. We subsequently found some more bugs - all of which seem to point to problems in the way that Janino does name resolution across multiple files. For now we have cut over to the 1.6 compiler API but we would like to use Janino so we can deploy to JREs - so would be interested in any comments you have. If you'd like to contact me directly I'm at philip<at>pmilne<dot>net.

Show
Philip Milne added a comment - I probably should have used a higher priorty when reporting this since it turned out to be a showstopper for us. We subsequently found some more bugs - all of which seem to point to problems in the way that Janino does name resolution across multiple files. For now we have cut over to the 1.6 compiler API but we would like to use Janino so we can deploy to JREs - so would be interested in any comments you have. If you'd like to contact me directly I'm at philip<at>pmilne<dot>net.
Hide
Arno Unkrig added a comment -

If you apply this patch

### Eclipse Workspace Patch 1.0
#P janino
Index: src/org/codehaus/janino/UnitCompiler.java
===================================================================
RCS file: /home/projects/janino/scm/janino/src/org/codehaus/janino/UnitCompiler.java,v
retrieving revision 1.56
diff -u -r1.56 UnitCompiler.java
--- src/org/codehaus/janino/UnitCompiler.java	25 Dec 2007 22:17:12 -0000	1.56
+++ src/org/codehaus/janino/UnitCompiler.java	26 Feb 2008 22:17:25 -0000
@@ -5292,7 +5292,11 @@
         // JLS3 6.5.2.BL1.B1.B2.1 Static field imported through single static import.
         {
             Object o = this.singleStaticImports.get(identifier);
-            if (o instanceof IField) return new FieldAccess(location, new SimpleType(location, ((IField) o).getDeclaringIClass()), (IField) o);
+            if (o instanceof IField) {
+                FieldAccess fieldAccess = new FieldAccess(location, new SimpleType(location, ((IField) o).getDeclaringIClass()), (IField) o);
+                fieldAccess.setEnclosingBlockStatement(enclosingBlockStatement);
+                return fieldAccess;
+            }
         }
 
         // JLS3 6.5.2.BL1.B1.B2.2 Static field imported through static-import-on-demand.
@@ -5301,7 +5305,11 @@
             IField[] flds = iClass.getDeclaredIFields();
             for (int i = 0 ; i < flds.length; ++i) {
                 IField f = flds[i];
-                if (f.getName().equals(identifier)) return new FieldAccess(location, new SimpleType(location, iClass), f);
+                if (f.getName().equals(identifier)) {
+                    FieldAccess fieldAccess = new FieldAccess(location, new SimpleType(location, iClass), f);
+                    fieldAccess.setEnclosingBlockStatement(enclosingBlockStatement);
+                    return fieldAccess;
+                }
             }
         }

, then you get

c:\tmp\testsrc\b\C3.java, Line 8, Column 27: Private member cannot be accessed from type "b.C3"."

, which is right according to the JLS3.

Is that the behavior that you expect, too?

CU

Arno

Show
Arno Unkrig added a comment - If you apply this patch
### Eclipse Workspace Patch 1.0
#P janino
Index: src/org/codehaus/janino/UnitCompiler.java
===================================================================
RCS file: /home/projects/janino/scm/janino/src/org/codehaus/janino/UnitCompiler.java,v
retrieving revision 1.56
diff -u -r1.56 UnitCompiler.java
--- src/org/codehaus/janino/UnitCompiler.java	25 Dec 2007 22:17:12 -0000	1.56
+++ src/org/codehaus/janino/UnitCompiler.java	26 Feb 2008 22:17:25 -0000
@@ -5292,7 +5292,11 @@
         // JLS3 6.5.2.BL1.B1.B2.1 Static field imported through single static import.
         {
             Object o = this.singleStaticImports.get(identifier);
-            if (o instanceof IField) return new FieldAccess(location, new SimpleType(location, ((IField) o).getDeclaringIClass()), (IField) o);
+            if (o instanceof IField) {
+                FieldAccess fieldAccess = new FieldAccess(location, new SimpleType(location, ((IField) o).getDeclaringIClass()), (IField) o);
+                fieldAccess.setEnclosingBlockStatement(enclosingBlockStatement);
+                return fieldAccess;
+            }
         }
 
         // JLS3 6.5.2.BL1.B1.B2.2 Static field imported through static-import-on-demand.
@@ -5301,7 +5305,11 @@
             IField[] flds = iClass.getDeclaredIFields();
             for (int i = 0 ; i < flds.length; ++i) {
                 IField f = flds[i];
-                if (f.getName().equals(identifier)) return new FieldAccess(location, new SimpleType(location, iClass), f);
+                if (f.getName().equals(identifier)) {
+                    FieldAccess fieldAccess = new FieldAccess(location, new SimpleType(location, iClass), f);
+                    fieldAccess.setEnclosingBlockStatement(enclosingBlockStatement);
+                    return fieldAccess;
+                }
             }
         }
, then you get c:\tmp\testsrc\b\C3.java, Line 8, Column 27: Private member cannot be accessed from type "b.C3"." , which is right according to the JLS3. Is that the behavior that you expect, too? CU Arno
Hide
Philip Milne added a comment -

Many thanks for taking a look. Its been a while but no, I don't think so. Javacc seems to compile
all three classes wihtout error as field A is inaccessible from class C2 but accessbile from C1.

Regards,

Philip

Show
Philip Milne added a comment - Many thanks for taking a look. Its been a while but no, I don't think so. Javacc seems to compile all three classes wihtout error as field A is inaccessible from class C2 but accessbile from C1. Regards, Philip
Hide
Arno Unkrig added a comment -

Yep, you may be right. JLS3 "7.5.4 Static-Import-on-Demand Declaration" says:

A static-import-on-demand declaration allows all accessible (§6.6) static members declared in the type named by a canonical name to be imported as needed.

Fixed UnitCompiler accordingly; updated the JUnit suite.

Please test with version 2.5.13.

Show
Arno Unkrig added a comment - Yep, you may be right. JLS3 "7.5.4 Static-Import-on-Demand Declaration" says:
A static-import-on-demand declaration allows all accessible (§6.6) static members declared in the type named by a canonical name to be imported as needed.
Fixed UnitCompiler accordingly; updated the JUnit suite. Please test with version 2.5.13.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: