Cannot compile a java library with Gradle, works with the IDE

In order to build the project, use fully qualified names for implemented interfaces for static nested classes defined in com.vinaysshenoy.types.util.Util:

  public static final class ParameterizedTypeImpl implements java.lang.reflect.ParameterizedType { ... }

  public static final class GenericArrayTypeImpl implements java.lang.reflect.GenericArrayType { ... }

  public static final class WildcardTypeImpl implements java.lang.reflect.WildcardType { ... }

Also, if you don't want to use fully qualified names, then just extract static nested classes to top level.

Notes:

  1. The project compiles without errors, when JDK compiler is used:
javac -cp path_to_jsr305-3.0.2.jar -d bin src/main/java/com/vinaysshenoy/types/Types.java src/main/java/com/vinaysshenoy/types/util/Util.java
  1. This problem seems to be dependent on platform. I've tried to build project on Windows 10/Oracle JDK 1.8.0_111 - and no issues were observed. However, the issue reproduced exactly as described on Ubuntu 16.04/Oracle JDK 1.8.0_201 & Ubuntu 16.04/OpenJDK 1.8.0_191.

Solution

@Zgurskyi’s answer is a good workaround, however, I believe it only cures the symptom of the actual problem (see below). Here’s another, IMHO cleaner way to fix the underlying issue: make the imports of the nested types of com.vinaysshenoy.types.util.Util from com.vinaysshenoy.types.Types non-static:

diff --git a/src/main/java/com/vinaysshenoy/types/Types.java b/src/main/java/com/vinaysshenoy/types/Types.java
index e3a44d8..92ac237 100644
--- a/src/main/java/com/vinaysshenoy/types/Types.java
+++ b/src/main/java/com/vinaysshenoy/types/Types.java
@@ -17,9 +17,9 @@ package com.vinaysshenoy.types;


 import static com.vinaysshenoy.types.util.Util.EMPTY_TYPE_ARRAY;
-import static com.vinaysshenoy.types.util.Util.GenericArrayTypeImpl;
-import static com.vinaysshenoy.types.util.Util.ParameterizedTypeImpl;
-import static com.vinaysshenoy.types.util.Util.WildcardTypeImpl;
+import com.vinaysshenoy.types.util.Util.GenericArrayTypeImpl;
+import com.vinaysshenoy.types.util.Util.ParameterizedTypeImpl;
+import com.vinaysshenoy.types.util.Util.WildcardTypeImpl;
 import static com.vinaysshenoy.types.util.Util.getGenericSupertype;
 import static com.vinaysshenoy.types.util.Util.resolve;

(BTW, other than @Zgurskyi I can also reproduce this with a manual javac call. I only have one JDK installed; maybe @Zgurskyi’s javac on the command line is not from the same JDK that Gradle uses.)

Actual Problem

You statically import nested classes (not just class members), although this should never be necessary. I’m actually surprised that this seems to usually work, but apparently some Java compilers at least choke on it under certain circumstances.

In your case, the Java compiler used by Gradle couldn’t correctly resolve the imports in the following scenario (only a rough, not very technical description of what the compiler does):

  1. when processing com.vinaysshenoy.types.util.Util, the compiler finds a static import of com.vinaysshenoy.types.Types.arrayOf, so the compiler looks at the com.vinaysshenoy.types.Types class.
  2. com.vinaysshenoy.types.Types has a static import of com.vinaysshenoy.types.util.Util.GenericArrayTypeImpl, so the compiler looks at the nested GenericArrayTypeImpl class.
  3. com.vinaysshenoy.types.util.Util.GenericArrayTypeImpl uses (java.lang.reflect.)GenericArrayType, however, the import for that type was not yet processed at this point which leads to the “cannot find symbol” error.

Arguably, it could also be considered to be a JDK bug that this works with some compilers but not with others.

Tags:

Java

Gradle