587 } 588 } 589 if (formatClassName == null) { 590 throw new IllegalArgumentException("Unsupported format name"); 591 } 592 try { 593 // Try to load from the same location as the module of the SPI 594 final String className = formatClassName; 595 PrivilegedAction<Class<?>> pa = () -> { return getMetadataFormatClass(className); }; 596 Class<?> cls = AccessController.doPrivileged(pa); 597 Method meth = cls.getMethod("getInstance"); 598 return (IIOMetadataFormat) meth.invoke(null); 599 } catch (Exception e) { 600 RuntimeException ex = 601 new IllegalStateException ("Can't obtain format"); 602 ex.initCause(e); 603 throw ex; 604 } 605 } 606 607 private Class<?> getMetadataFormatClass(String formatClassName) { 608 Module thisModule = ImageReaderWriterSpi.class.getModule(); 609 Module targetModule = this.getClass().getModule(); 610 Class<?> c = Class.forName(targetModule, formatClassName); 611 if (thisModule.equals(targetModule) || c == null) { 612 return c; 613 } 614 if (thisModule.isNamed()) { 615 int i = formatClassName.lastIndexOf("."); 616 String pn = i > 0 ? formatClassName.substring(0, i) : ""; 617 if (!targetModule.isExported(pn, thisModule)) { 618 throw new IllegalStateException("Class " + formatClassName + 619 " in named module must be exported to java.desktop module."); 620 } 621 } 622 return c; 623 } 624 } | 587 } 588 } 589 if (formatClassName == null) { 590 throw new IllegalArgumentException("Unsupported format name"); 591 } 592 try { 593 // Try to load from the same location as the module of the SPI 594 final String className = formatClassName; 595 PrivilegedAction<Class<?>> pa = () -> { return getMetadataFormatClass(className); }; 596 Class<?> cls = AccessController.doPrivileged(pa); 597 Method meth = cls.getMethod("getInstance"); 598 return (IIOMetadataFormat) meth.invoke(null); 599 } catch (Exception e) { 600 RuntimeException ex = 601 new IllegalStateException ("Can't obtain format"); 602 ex.initCause(e); 603 throw ex; 604 } 605 } 606 607 // If updating this method also see the same in IIOMetadata.java 608 private Class<?> getMetadataFormatClass(String formatClassName) { 609 Module thisModule = ImageReaderWriterSpi.class.getModule(); 610 Module targetModule = this.getClass().getModule(); 611 Class<?> c = null; 612 try { 613 ClassLoader cl = this.getClass().getClassLoader(); 614 c = Class.forName(formatClassName, false, cl); 615 if (!IIOMetadataFormat.class.isAssignableFrom(c)) { 616 return null; 617 } 618 } catch (ClassNotFoundException e) { 619 } 620 if (thisModule.equals(targetModule) || c == null) { 621 return c; 622 } 623 if (targetModule.isNamed()) { 624 int i = formatClassName.lastIndexOf("."); 625 String pn = i > 0 ? formatClassName.substring(0, i) : ""; 626 if (!targetModule.isExported(pn, thisModule)) { 627 throw new IllegalStateException("Class " + formatClassName + 628 " in named module must be exported to java.desktop module."); 629 } 630 } 631 return c; 632 } 633 } |