397 } 398 if (formatClassName == null) { 399 throw new IllegalArgumentException("Unsupported format name"); 400 } 401 try { 402 final String className = formatClassName; 403 // Try to load from the module of the IIOMetadata implementation 404 // for this plugin since the IIOMetadataImpl is part of the plugin 405 PrivilegedAction<Class<?>> pa = () -> { return getMetadataFormatClass(className); }; 406 Class<?> cls = AccessController.doPrivileged(pa); 407 Method meth = cls.getMethod("getInstance"); 408 return (IIOMetadataFormat) meth.invoke(null); 409 } catch (Exception e) { 410 RuntimeException ex = 411 new IllegalStateException ("Can't obtain format"); 412 ex.initCause(e); 413 throw ex; 414 } 415 } 416 417 private Class<?> getMetadataFormatClass(String formatClassName) { 418 Module thisModule = IIOMetadata.class.getModule(); 419 Module targetModule = this.getClass().getModule(); 420 Class<?> c = Class.forName(targetModule, formatClassName); 421 if (thisModule.equals(targetModule) || c == null) { 422 return c; 423 } 424 if (thisModule.isNamed()) { 425 int i = formatClassName.lastIndexOf("."); 426 String pn = i > 0 ? formatClassName.substring(0, i) : ""; 427 if (!targetModule.isExported(pn, thisModule)) { 428 throw new IllegalStateException("Class " + formatClassName + 429 " in named module must be exported to java.desktop module."); 430 } 431 } 432 return c; 433 } 434 435 /** 436 * Returns an XML DOM {@code Node} object that represents the 437 * root of a tree of metadata contained within this object 438 * according to the conventions defined by a given metadata 439 * format. 440 * 441 * <p> The names of the available metadata formats may be queried 442 * using the {@code getMetadataFormatNames} method. 443 * 444 * @param formatName the desired metadata format. | 397 } 398 if (formatClassName == null) { 399 throw new IllegalArgumentException("Unsupported format name"); 400 } 401 try { 402 final String className = formatClassName; 403 // Try to load from the module of the IIOMetadata implementation 404 // for this plugin since the IIOMetadataImpl is part of the plugin 405 PrivilegedAction<Class<?>> pa = () -> { return getMetadataFormatClass(className); }; 406 Class<?> cls = AccessController.doPrivileged(pa); 407 Method meth = cls.getMethod("getInstance"); 408 return (IIOMetadataFormat) meth.invoke(null); 409 } catch (Exception e) { 410 RuntimeException ex = 411 new IllegalStateException ("Can't obtain format"); 412 ex.initCause(e); 413 throw ex; 414 } 415 } 416 417 // If updating this method also see the same in ImageReaderWriterSpi.java 418 private Class<?> getMetadataFormatClass(String formatClassName) { 419 Module thisModule = IIOMetadata.class.getModule(); 420 Module targetModule = this.getClass().getModule(); 421 Class<?> c = null; 422 try { 423 ClassLoader cl = this.getClass().getClassLoader(); 424 c = Class.forName(formatClassName, false, cl); 425 if (!IIOMetadataFormat.class.isAssignableFrom(c)) { 426 return null; 427 } 428 } catch (ClassNotFoundException e) { 429 } 430 if (thisModule.equals(targetModule) || c == null) { 431 return c; 432 } 433 if (targetModule.isNamed()) { 434 int i = formatClassName.lastIndexOf("."); 435 String pn = i > 0 ? formatClassName.substring(0, i) : ""; 436 if (!targetModule.isExported(pn, thisModule)) { 437 throw new IllegalStateException("Class " + formatClassName + 438 " in named module must be exported to java.desktop module."); 439 } 440 } 441 return c; 442 } 443 444 /** 445 * Returns an XML DOM {@code Node} object that represents the 446 * root of a tree of metadata contained within this object 447 * according to the conventions defined by a given metadata 448 * format. 449 * 450 * <p> The names of the available metadata formats may be queried 451 * using the {@code getMetadataFormatNames} method. 452 * 453 * @param formatName the desired metadata format. |