Ignore:
Timestamp:
2017-01-19T00:08:10+01:00 (8 years ago)
Author:
donvip
Message:

see #josm13302 - potential fix, unable to test for sure

Location:
applications/editors/josm/plugins/print
Files:
8 added
2 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/print/.classpath

    r32680 r33118  
    22<classpath>
    33        <classpathentry kind="src" path="src"/>
     4        <classpathentry kind="src" path="test/unit"/>
    45        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
    56        <classpathentry combineaccessrules="false" kind="src" path="/JOSM"/>
     7        <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
    68        <classpathentry kind="output" path="bin"/>
    79</classpath>
  • applications/editors/josm/plugins/print/src/org/openstreetmap/josm/plugins/print/PrintDialog.java

    r33062 r33118  
    1515import java.awt.print.PrinterException;
    1616import java.awt.print.PrinterJob;
     17import java.lang.reflect.Constructor;
    1718import java.lang.reflect.Field;
    1819import java.lang.reflect.InvocationTargetException;
     20import java.lang.reflect.Method;
    1921import java.lang.reflect.Modifier;
    2022import java.text.ParseException;
     
    5860import org.openstreetmap.josm.Main;
    5961import org.openstreetmap.josm.tools.GBC;
     62import org.openstreetmap.josm.tools.Utils;
    6063import org.openstreetmap.josm.tools.WindowGeometry;
    6164
     
    490493
    491494    @SuppressWarnings("unchecked")
    492     protected Attribute unmarshallPrintSetting(Collection<String> setting) throws
     495    static Attribute unmarshallPrintSetting(Collection<String> setting) throws
    493496        ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
    494497
     
    505508        } else if (syntax.equals(EnumSyntax.class)) {
    506509            int intValue = Integer.parseInt(value);
     510            // First method - access static enum fields by reflection for classes that do not implement getEnumValueTable
    507511            for (Field f : realClass.getFields()) {
    508512                if (Modifier.isStatic(f.getModifiers()) && category.isAssignableFrom(f.getType())) {
     
    513517                }
    514518            }
    515             throw new IllegalArgumentException("Invalid enum: "+realClass+" - "+value);
     519            // Second method - get instance from getEnumValueTable by reflection
     520            try {
     521                Method getEnumValueTable = realClass.getDeclaredMethod("getEnumValueTable");
     522                Constructor<? extends Attribute> constructor = realClass.getDeclaredConstructor(int.class);
     523                Utils.setObjectsAccessible(getEnumValueTable, constructor);
     524                Attribute fakeInstance = constructor.newInstance(Integer.MAX_VALUE);
     525                EnumSyntax[] enumTable = (EnumSyntax[]) getEnumValueTable.invoke(fakeInstance);
     526                return (Attribute) enumTable[intValue];
     527            } catch (ReflectiveOperationException | ArrayIndexOutOfBoundsException e) {
     528                throw new IllegalArgumentException("Invalid enum: "+realClass+" - "+value, e);
     529            }
    516530        } else if (syntax.equals(IntegerSyntax.class)) {
    517531            return realClass.getConstructor(int.class).newInstance(Integer.parseInt(value));
Note: See TracChangeset for help on using the changeset viewer.