JDK 11: specify a module when getting a parent package.
Change on 2019/02/05 by antoniocortes
@@ -30,6 +30,7 @@ import com.google.j2objc.annotations.RetainedWith;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
+import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
@@ -542,8 +543,18 @@ public final class ElementUtil {
return null;
}
name = name.substring(0, name.lastIndexOf('.'));
+ // Try the Java 9+ API where the module needs to be specified to find the package.
+ try {
+ Method getModuleOf = Elements.class.getMethod("getModuleOf", Element.class);
+ Object module = getModuleOf.invoke(javacElements, element);
+ Method getPackageElement = Elements.class
+ .getMethod("getPackageElement", getModuleOf.getReturnType(), CharSequence.class);
+ return (PackageElement) getPackageElement.invoke(javacElements, module, name);
+ } catch (ReflectiveOperationException e) {
+ // Default behavior: Java 8.
return javacElements.getPackageElement(name);
}
+ }
public String getBinaryName(TypeElement e) {
if (e instanceof GeneratedTypeElement) {
