Created
November 9, 2017 00:33
-
-
Save knghtbrd/c5e1aadbe68d6d1556a0c9290559f2e6 to your computer and use it in GitHub Desktop.
Help wanted with Java generics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* AppleImage is an abstract class that represents a generic interface | |
* for handing graphic images. This abstraction is needed because not | |
* all graphic routines use BufferedImage. AppleImage itself acts as | |
* a factory for creating "the best" AppleImage subclass. | |
* <p> | |
* Date Created: Mar 25, 2003 | |
* @author Rob Greene | |
*/ | |
public abstract class AppleImage { | |
/** | |
* The specific file extension to use. | |
*/ | |
private String fileExtension; | |
/** | |
* All available image formats. This is instance specific since | |
* (in theory) there could be different AppleImages being used. | |
*/ | |
private String[] availableExtensions; | |
/** | |
* Create a specific instance of AppleImage. This has been coded | |
* using Reflection to ease native compilation for the most part. | |
*/ | |
public static AppleImage create(int width, int height) { | |
String[] classes = { | |
"ImageIoImage", "SunJpegImage", "SwtImage" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |
Class[] constructorArgClasses = new Class[] { | |
int.class, int.class }; | |
Object[] constructorArgs = new Object[] { | |
new Integer(width), new Integer(height) }; | |
for (int i=0; i<classes.length; i++) { | |
try { | |
Class appleImageClass = Class.forName( | |
"com.webcodepro.applecommander.storage.filters.imagehandlers." //$NON-NLS-1$ | |
+ classes[i]); | |
Constructor constructor = | |
appleImageClass.getConstructor(constructorArgClasses); | |
AppleImage appleImage = (AppleImage) | |
constructor.newInstance(constructorArgs); | |
return appleImage; | |
} catch (Exception ignored) { | |
// There are multiple exceptions that can be thrown here. | |
// For the most part, this is expected and simply means that | |
// the image handler is not available on the platform. | |
} | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Apparently the solution looks something like this:
I can't say I fully understand that.