Now for the problem. Right now I have a method inside the AbstractInstaller class that is supposed to return a new reference to the currentState object. At the moment it returns the reference and not a new object, which is a potential security risk. To fix this I tried to do something like this:
public T getCurrentState() {
return new T(this.currentState);
}
But I get an error stating "cannot instantiate the type T". Not quite sure where to go from here. Anyone have any idea?
Here is the rest of my code:
AbstractInstaller:
package com.installer.core;
import java.util.*;
import java.io.*;
/* A class meant to be the super class of all installer objects
* used with the Installer API.
*/
public abstract class AbstractInstaller<T extends AbstractState<?>> {
private File installDirectory;
private Stack<T> stateStack, backStack;
private T currentState;
/* Constructor that takes a Stack of type T and a File that points
* to the desired install directory.
*/
public AbstractInstaller(Stack<T> states, File installDir) {
this.stateStack = states;
this.installDirectory = installDir;
this.currentState = stateStack.pop();
this.backStack = new Stack<T>();
}
/* Constructor that takes a Vector of type T and a File that points
* to the desired install directory.
*/
public AbstractInstaller(Vector<T> states, File installDir) {
this.stateStack = new Stack<T>();
for(int i=states.size()-1; i>0; i--) {
stateStack.push(states.get(i));
}
this.installDirectory = installDir;
this.currentState = stateStack.pop();
this.backStack = new Stack<T>();
}
/* Sets the installDirectory parameter to the value passed in
* by installDir
*/
public void setInstallDirectroy(File installDir) {
this.installDirectory = installDir;
}
/* Returns a new File object with pointing to the directory
* in installDirectory.
*/
public File getInstallDirectory() {
return new File(this.installDirectory.getPath());
}
/* Adds the current value of currentState to the backStack,
* adds the next value from stateStack to currentState and
* returns the value in currentValue. Throws an IndexOutOfBounds
* exception if stateStack is empty.
*/
public T goForward() throws IndexOutOfBoundsException {
try {
stateStack.peek();
backStack.push(currentState);
currentState = stateStack.pop();
}
catch(EmptyStackException ex) {
throw new IndexOutOfBoundsException("Can't go forward");
}
return currentState;
}
/* Adds the current value of currentState to the stateStack,
* adds the next value from backStack to currentState and
* returns the value in currentValue. Throws an IndexOutOfBounds
* exception if backStack is empty.
*/
public T goBackward() throws IndexOutOfBoundsException {
try {
backStack.peek();
stateStack.push(currentState);
currentState = backStack.pop();
}
catch(EmptyStackException ex) {
throw new IndexOutOfBoundsException("Can't go backward");
}
return currentState;
}
/*Returns a reference to the object in the current state.
*/
public T getCurrentState() {
return new T(this.currentState);
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
return "State Stack: " + stateStack.toString() + "\nBack Stack: " + backStack.toString() +
"\nCurrent State: " + getCurrentState() + "\nInstall Directory: " + installDirectory.getPath();
}
}
AbstractState:
package com.installer.core;
public abstract class AbstractState<T> {
private T label;
private int key;
public AbstractState(T label, int key) {
this.label = label;
this.key = key;
}
public AbstractState(AbstractState<T> state) {
this(state.getLabel(), state.getKey());
}
public T getLabel() {
return this.label;
}
public int getKey() {
return this.key;
}
public String toString() {
return "[" + key + ", " + label.toString() + "]";
}
}
Edit: Just noticed that I forgot to change the title once I realized the wildcard had nothing to do with the problem. Would change it but I already submitted. Sorry for the misleading title.
This post has been edited by giggly kisses: 29 April 2012 - 07:02 PM

New Topic/Question
Reply



MultiQuote





|