From 6cba05247ae4e6c202c4c126a535b17526f71127 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Sat, 17 Apr 2010 04:33:42 +0200 Subject: Use factories implemented as nested classes (rather than reflected constructors) to store classes to instantiate. Now every subclass of TodoElement or TodoAttr should also come with a nested subclass of TodoElement.Factory or TodoAttr.Factory which just contains a(n instance) method to call its constructor. In essecne, the factory instance is just used as a pointer to the class to create (or its constructor, as in the previous version). --- org/madore/damlengine/TodoAttr.java | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) (limited to 'org/madore/damlengine/TodoAttr.java') diff --git a/org/madore/damlengine/TodoAttr.java b/org/madore/damlengine/TodoAttr.java index 2b3df23..721b4f6 100644 --- a/org/madore/damlengine/TodoAttr.java +++ b/org/madore/damlengine/TodoAttr.java @@ -1,7 +1,5 @@ package org.madore.damlengine; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.util.Map; import java.util.HashMap; import org.w3c.dom.Element; @@ -9,16 +7,17 @@ import org.w3c.dom.Attr; public abstract class TodoAttr extends TodoItem { - protected static Map> damlAttrConstructors; + public static abstract class Factory { + public abstract TodoAttr newItem(Attr attr, Element owner, + Map context, + Map options); + } + + protected static Map damlAttrFactories; static { - damlAttrConstructors = new HashMap>(); - Class[] argTypes = new Class[]{ Attr.class, Element.class, Map.class, Map.class }; - try { - damlAttrConstructors.put("xempty", TodoXemptyAttr.class.getConstructor(argTypes)); - } catch (NoSuchMethodException e) { - // FIXME: Do something intelligent here! - } + damlAttrFactories = new HashMap(); + damlAttrFactories.put("xempty", new TodoXemptyAttr.Factory()); } Attr attr; @@ -37,21 +36,13 @@ public abstract class TodoAttr extends TodoItem { public static TodoAttr getTodoAttr(Attr attr, Element owner, Map context, Map options) { - Constructor constructor = null; + Factory factory = null; String nsuri = attr.getNamespaceURI(); if ( nsuri != null && nsuri.equals(DamlEngine.DAML_NS) ) - constructor = damlAttrConstructors.get(attr.getLocalName()); - if ( constructor != null ) - try { - return constructor.newInstance(new Object[]{attr, owner, context, options}); - } catch (InstantiationException e) { - // FIXME: Do something intelligent here! - } catch (IllegalAccessException e) { - // FIXME: Do something intelligent here! - } catch (InvocationTargetException e) { - // FIXME: Do something intelligent here! - } - return null; + factory = damlAttrFactories.get(attr.getLocalName()); + if ( factory == null ) + return null; + return factory.newItem(attr, owner, context, options); } } -- cgit v1.2.3