Skip to content

Commit

Permalink
Merge pull request #332 from V-F/bugfix_Illegal_reflective_access_to_…
Browse files Browse the repository at this point in the history
…method_sun.font.FontUtilities.getFont2D

Bugfix illegal reflective access operation
  • Loading branch information
andreasrosdal authored Jan 30, 2020
2 parents 85c99de + e3e9b5b commit 4d18b3a
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion openpdf/src/main/java/com/lowagie/text/pdf/PdfGraphics2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -1768,6 +1768,10 @@ static boolean isSupported() {
return SUPPORTED;
}

private static final String GET_MODULE_METHOD_NAME = "getModule";
private static final String IS_OPEN_METHOD_NAME = "isOpen";
private static final String ADD_OPENS_METHOD_NAME = "addOpens";

private static final String COMPOSITE_FONT_CLASS_NAME = "sun.font.CompositeFont";
private static final Class<?> COMPOSITE_FONT_CLASS;
private static final String GET_NUM_SLOTS_METHOD_NAME = "getNumSlots";
Expand All @@ -1794,6 +1798,7 @@ static boolean isSupported() {
boolean macOS = osName.startsWith("Mac");
if (!macOS) {
FONT_UTILITIES_CLASS = getClassForName(FONT_UTILITIES_CLASS_NAME);
updateModuleToOpenPackage(FONT_UTILITIES_CLASS, "sun.font");
GET_FONT2D_METHOD = getMethod(FONT_UTILITIES_CLASS, GET_FONT2D_METHOD_NAME, Font.class);
COMPOSITE_FONT_CLASS = getClassForName(COMPOSITE_FONT_CLASS_NAME);
GET_NUM_SLOTS_METHOD = getMethod(COMPOSITE_FONT_CLASS, GET_NUM_SLOTS_METHOD_NAME);
Expand All @@ -1817,6 +1822,46 @@ static boolean isSupported() {
GET_SLOT_FONT_METHOD != null && CAN_DYSPLAY_METHOD != null && GET_FONT_NAME_METHOD != null;
}

/**
* Update module of the given class to open the given
* package to the target module if the target module
* is opened for the current module.<br/>
* This helps to avoid warnings for the <code>--illegal-access=permit<code/>.
* Actually (java 9-13) "permit" is default mode, but in the future java
* releases the default mode will be "deny". It's also important to
* add <code>--add-opens<code/> for the given package if it's need.
*/
private static void updateModuleToOpenPackage(Class<?> classInModule, String packageName) {
if (classInModule == null || packageName == null) {
return;
}
Method getModuleMethod = getMethod(Class.class, GET_MODULE_METHOD_NAME);
if (getModuleMethod == null) {
return;
}
try {
Object targetModule = getModuleMethod.invoke(classInModule);
if (targetModule == null) {
return;
}
Class<?> moduleClass = targetModule.getClass();
Object callerModule = getModuleMethod.invoke(CompositeFontDrawer.class);
Method isOpenMethod = getMethod(moduleClass, IS_OPEN_METHOD_NAME, String.class, moduleClass);
if (isOpenMethod == null) {
return;
}
Object isOpened = isOpenMethod.invoke(targetModule, packageName, callerModule);
if (isOpened instanceof Boolean && ((Boolean) isOpened)) {
Method addOpensMethod = getMethod(moduleClass, ADD_OPENS_METHOD_NAME, String.class, moduleClass);
if (callerModule != null) {
addOpensMethod.invoke(targetModule, packageName, callerModule);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

private static Class<?> getClassForName(String className) {
try {
return Class.forName(className);
Expand All @@ -1832,7 +1877,9 @@ private static Method getMethod(Class<?> clazz, String methodName, Class<?>... p
Method method;
try {
method = clazz.getDeclaredMethod(methodName, parameterTypes);
method.setAccessible(true);
if (!method.isAccessible()) {
method.setAccessible(true);
}
} catch (Exception e) {
method = null;
}
Expand Down

0 comments on commit 4d18b3a

Please sign in to comment.