Commit edaac404 by Anton Sudak

Add generic parameter to the factory methods in AbstractBlock

parent 1beee8ec
......@@ -161,36 +161,24 @@ public abstract class AbstractBlock extends Model implements Constraints.Validat
/**
* Create and add a sub-block of the given class.
*/
public AbstractBlock addSubBlock(Class<? extends AbstractBlock> newBlockClass) throws ClassNotFoundException {
public <T extends AbstractBlock> T addSubBlock(Class<T> newBlockClass) throws ClassNotFoundException {
// Instantiate from the selected class
AbstractBlock newBlock = null;
try {
newBlock = newBlockClass.newInstance();
T newBlock = newBlockClass.newInstance();
addSubBlock(newBlock);
newBlock.save();
return newBlock;
} catch (Exception e) {
String msg = "Could not instantiate new block of type: " + newBlockClass.getName();
logger.error(msg, e);
throw new ClassNotFoundException(msg);
}
addSubBlock(newBlock);
newBlock.save();
return newBlock;
}
public AbstractBlock addSubBlock(String blockClassName) throws ClassNotFoundException {
Class<? extends AbstractBlock> newBlockClass = null;
try {
//newBlockClass = findBlockClassForName(blockClassName);
newBlockClass = (Class<? extends AbstractBlock>) Class.forName(blockClassName, true, Play.current().classloader());
} catch (ClassNotFoundException e) {
String msg = "Could not find class for new block: " + blockClassName;
logger.error(msg, e);
throw new ClassNotFoundException(msg);
}
public <T extends AbstractBlock> T addSubBlock(String blockClassName) throws ClassNotFoundException {
Class<T> newBlockClass = (Class<T>) Class.forName(blockClassName, true, Play.current().classloader());
return addSubBlock(newBlockClass);
}
......@@ -199,19 +187,17 @@ public abstract class AbstractBlock extends Model implements Constraints.Validat
*
* @throws ClassNotFoundException
**/
public static AbstractBlock newInstanceOf(String blockClassName) throws ClassNotFoundException {
Class<? extends AbstractBlock> newBlockClass = null;
public static <T extends AbstractBlock> T newInstanceOf(String blockClassName) throws ClassNotFoundException {
Class<T> newBlockClass;
try {
//newBlockClass = findBlockClassForName(blockClassName);
newBlockClass = (Class<? extends AbstractBlock>) Class.forName(blockClassName, true, Play.current().classloader());
newBlockClass = (Class<T>) Class.forName(blockClassName, true, Play.current().classloader());
} catch (ClassNotFoundException e) {
String msg = "Could not find class for new block: " + blockClassName;
logger.error(msg, e);
throw new ClassNotFoundException(msg);
}
AbstractBlock newBlock = null;
T newBlock;
try {
newBlock = newBlockClass.newInstance();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment