Skip to content

Instantly share code, notes, and snippets.

@knghtbrd
Created November 9, 2017 00:33
Show Gist options
  • Save knghtbrd/c5e1aadbe68d6d1556a0c9290559f2e6 to your computer and use it in GitHub Desktop.
Save knghtbrd/c5e1aadbe68d6d1556a0c9290559f2e6 to your computer and use it in GitHub Desktop.
Help wanted with Java generics
/**
* 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;
}
@knghtbrd
Copy link
Author

knghtbrd commented Nov 9, 2017

Apparently the solution looks something like this:

diff --git a/src/com/webcodepro/applecommander/storage/filters/imagehandlers/AppleImage.java b/src/com/webcodepro/applecommander/storage/filters/imagehandlers/AppleImage.java
index 5376ef1..540c15b 100644
--- a/src/com/webcodepro/applecommander/storage/filters/imagehandlers/AppleImage.java
+++ b/src/com/webcodepro/applecommander/storage/filters/imagehandlers/AppleImage.java
@@ -49,17 +49,17 @@ public abstract class AppleImage {
 	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 };
+		Class<?> constructorArgType = AppleImage.class;
+		Class<?>[] constructorArgClasses = new Class[] { AppleImage.class };
 		Object[] constructorArgs = new Object[] {
 			new Integer(width), new Integer(height) };
 		for (int i=0; i<classes.length; i++) {
 			try {
-				Class appleImageClass = Class.forName(
+				Class<?> appleImageClass = Class.forName(
 					"com.webcodepro.applecommander.storage.filters.imagehandlers."  //$NON-NLS-1$
 					+ classes[i]);
-				Constructor constructor = 
-					appleImageClass.getConstructor(constructorArgClasses);
+				Constructor constructor =
+					constructorArgType.getConstructor(constructorArgClasses);
 				AppleImage appleImage = (AppleImage) 
 					constructor.newInstance(constructorArgs);
 				return appleImage;

I can't say I fully understand that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment