From: <sha...@us...> - 2011-07-27 04:46:49
|
Revision: 2968 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=2968&view=rev Author: shadowtm Date: 2011-07-27 04:46:43 +0000 (Wed, 27 Jul 2011) Log Message: ----------- Added example showing how to use annotations for fields which were not of a basic type. Modified Paths: -------------- trunk/components-core/pom.xml Added Paths: ----------- trunk/components-core/src/test/java/org/dllearner/examples/ trunk/components-core/src/test/java/org/dllearner/examples/AnnotationExample.java trunk/components-core/src/test/java/org/dllearner/examples/ConfigOption.java trunk/components-core/src/test/java/org/dllearner/examples/ConfiguredObject.java trunk/components-core/src/test/java/org/dllearner/examples/ObjectPropertyEditor.java trunk/components-core/src/test/java/org/dllearner/examples/OurStringTrimmerEditor.java Modified: trunk/components-core/pom.xml =================================================================== --- trunk/components-core/pom.xml 2011-07-26 11:37:49 UTC (rev 2967) +++ trunk/components-core/pom.xml 2011-07-27 04:46:43 UTC (rev 2968) @@ -247,5 +247,11 @@ <version>1.0</version> </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-beans</artifactId> + <version>3.0.5.RELEASE</version> + <scope>test</scope> + </dependency> </dependencies> </project> Added: trunk/components-core/src/test/java/org/dllearner/examples/AnnotationExample.java =================================================================== --- trunk/components-core/src/test/java/org/dllearner/examples/AnnotationExample.java (rev 0) +++ trunk/components-core/src/test/java/org/dllearner/examples/AnnotationExample.java 2011-07-27 04:46:43 UTC (rev 2968) @@ -0,0 +1,58 @@ +package org.dllearner.examples; + + +import javax.xml.transform.Source; +import java.beans.PropertyEditor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by IntelliJ IDEA. + * User: Chris + * Date: 7/26/11 + * Time: 9:03 PM + * <p/> + * An example showing the use of Annotations with non basic types/classes. + */ +public class AnnotationExample { + + + public static void main(String[] args) throws Exception { + + /** The Configuration map comes in from somewhere - The keys here correspond to the configuration options in ConfiguredObject*/ + Map<String, String> propertiesMap = new HashMap<String, String>(); + propertiesMap.put("initString", "My Initialization String"); + propertiesMap.put("objectProperty", "My Object Property"); + + /** Here's my new configured object */ + ConfiguredObject obj = new ConfiguredObject(); + + Method[] methods = obj.getClass().getMethods(); + for (Method method : methods) { + ConfigOption option = method.getAnnotation(ConfigOption.class); + /** If Annotated with Config Option*/ + if (option != null) { + + /** Retrieve the actual String value from the properties map */ + String configValue = propertiesMap.get(option.name()); + + /** Might be a good idea to catch a class cast exception here if we're wrong */ + PropertyEditor editor = (PropertyEditor) option.propertyEditorClass().newInstance(); + + /** This line causes the property editors to convert the string to the object*/ + editor.setAsText(configValue); + + /** This line grabs the object we want and invokes the method we're currently working on - with this scheme, only annotations should go on set methods */ + method.invoke(obj, editor.getValue()); + } + } + + /** Now show that we actually stored the objects on the ConfiguredObject itself */ + System.out.println("Information on Configured Object:"); + System.out.println("Initialization String: " + obj.getInitializationString()); + System.out.println("ObjectProperty: " + obj.getObjectProperty().getName()); + + } +} Added: trunk/components-core/src/test/java/org/dllearner/examples/ConfigOption.java =================================================================== --- trunk/components-core/src/test/java/org/dllearner/examples/ConfigOption.java (rev 0) +++ trunk/components-core/src/test/java/org/dllearner/examples/ConfigOption.java 2011-07-27 04:46:43 UTC (rev 2968) @@ -0,0 +1,36 @@ +package org.dllearner.examples; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * Created by IntelliJ IDEA. + * User: Chris + * Date: 7/26/11 + * Time: 8:55 PM + * + * This is an example annotation class allowing one to configure a field with a name, description, and corresponding property editor. + * + * Note: Only put this on Setters that take the actual object you want to end up with as the example expects it to be on the setter + */ +@Retention(RetentionPolicy.RUNTIME) +public @interface ConfigOption { + + /** + * The name of this config option. + * @return The name of this config option. + */ + String name(); + + /** + * The description of this config option + * @return + */ + String description(); + + /** + * An implementation of the Property Editor to use + * @return + */ + Class propertyEditorClass(); +} Added: trunk/components-core/src/test/java/org/dllearner/examples/ConfiguredObject.java =================================================================== --- trunk/components-core/src/test/java/org/dllearner/examples/ConfiguredObject.java (rev 0) +++ trunk/components-core/src/test/java/org/dllearner/examples/ConfiguredObject.java 2011-07-27 04:46:43 UTC (rev 2968) @@ -0,0 +1,59 @@ +package org.dllearner.examples; + +import org.dllearner.core.owl.ObjectProperty; +import org.dllearner.examples.ConfigOption; +import org.springframework.beans.propertyeditors.StringTrimmerEditor; + +/** + * Created by IntelliJ IDEA. + * User: Chris + * Date: 7/26/11 + * Time: 8:57 PM + * + * In this example, this is the object that is actually getting configured. It has the fields that we want to define + * configuration on and hence that's why we use the annotations. + */ +public class ConfiguredObject { + + + + + /** Two options that can be configured for this object */ + private String initializationString; + private ObjectProperty objectProperty; + + + /** + * Default constructor + */ + public ConfiguredObject() { + + } + + + /** + * Get the Initialization String. + * @return The Initialization String + */ + public String getInitializationString() { + return initializationString; + } + + /** + * Set the Initialization String + * @param initializationString + */ + @ConfigOption(name="initString", description = "An Initialization String", propertyEditorClass = OurStringTrimmerEditor.class) + public void setInitializationString(String initializationString) { + this.initializationString = initializationString; + } + + /** Corresponds with the name above */ + public ObjectProperty getObjectProperty() { + return objectProperty; + } + @ConfigOption(name="objectProperty", description = "Some Object Property", propertyEditorClass = ObjectPropertyEditor.class) + public void setObjectProperty(ObjectProperty objectProperty) { + this.objectProperty = objectProperty; + } +} Added: trunk/components-core/src/test/java/org/dllearner/examples/ObjectPropertyEditor.java =================================================================== --- trunk/components-core/src/test/java/org/dllearner/examples/ObjectPropertyEditor.java (rev 0) +++ trunk/components-core/src/test/java/org/dllearner/examples/ObjectPropertyEditor.java 2011-07-27 04:46:43 UTC (rev 2968) @@ -0,0 +1,88 @@ +package org.dllearner.examples; + +import org.dllearner.core.owl.ObjectProperty; + +import java.awt.*; +import java.beans.PropertyChangeListener; +import java.beans.PropertyEditor; + +/** + * Created by IntelliJ IDEA. + * User: Chris + * Date: 7/26/11 + * Time: 9:42 PM + * <p/> + * Basic Property Editor for the Object Property DL-Learner class. Doesn't have GUI support yet but we could add that later if we wanted. + */ +public class ObjectPropertyEditor implements PropertyEditor { + + + private ObjectProperty value; + + @Override + public void setValue(Object value) { + this.value = (ObjectProperty) value; + } + + @Override + public Object getValue() { + return value; + } + + @Override + public boolean isPaintable() { + /** Not right now, we're doing non gui work */ + return false; + } + + @Override + public void paintValue(Graphics gfx, Rectangle box) { + + } + + @Override + public String getJavaInitializationString() { + /** This returns the value needed to reconstitute the object from a string */ + return value.getName(); + } + + @Override + public String getAsText() { + /** Get the text value of this object - for displaying in GUIS, etc */ + return value.getName(); + } + + @Override + public void setAsText(String text) throws IllegalArgumentException { + value = new ObjectProperty(text); + } + + @Override + public String[] getTags() { + /** If there was a known set of values it had to have, we could add that list here */ + return new String[0]; + } + + @Override + public Component getCustomEditor() { + /** GUI stuff, if you wanted to edit it a custom way */ + return null; + } + + @Override + public boolean supportsCustomEditor() { + /** We don't support this right now, but maybe later */ + return false; + + } + + @Override + public void addPropertyChangeListener(PropertyChangeListener listener) { + /** More gui stuff, we don't need this for our basic example */ + } + + @Override + public void removePropertyChangeListener(PropertyChangeListener listener) { + /** More gui stuff, we don't need this for our basic example */ + } +} Added: trunk/components-core/src/test/java/org/dllearner/examples/OurStringTrimmerEditor.java =================================================================== --- trunk/components-core/src/test/java/org/dllearner/examples/OurStringTrimmerEditor.java (rev 0) +++ trunk/components-core/src/test/java/org/dllearner/examples/OurStringTrimmerEditor.java 2011-07-27 04:46:43 UTC (rev 2968) @@ -0,0 +1,22 @@ +package org.dllearner.examples; + +/** + * Created by IntelliJ IDEA. + * User: Chris + * Date: 7/26/11 + * Time: 10:17 PM + * <p/> + * Making a basic extension so I can get a default constructor so that using reflection is simpler. + * + * I'm extending a Spring provided class for this. + */ +public class OurStringTrimmerEditor extends org.springframework.beans.propertyeditors.StringTrimmerEditor { + + + /** + * Default Constructor + */ + public OurStringTrimmerEditor() { + super(true); + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |