001: package net.sourceforge.pmd.properties;
002:
003: import net.sourceforge.pmd.util.ClassUtil;
004:
005: /**
006: * Defines a property that supports class types, even for primitive values!
007: *
008: * @author Brian Remedios
009: * @version $Revision$
010: */
011: public class TypeProperty extends StringProperty {
012:
013: private static final char delimiter = '|';
014:
015: /**
016: * Constructor for TypeProperty.
017: * @param theName String
018: * @param theDescription String
019: * @param theDefault Class
020: * @param theUIOrder float
021: */
022: public TypeProperty(String theName, String theDescription,
023: Class theDefault, float theUIOrder) {
024: super (theName, theDescription, theDefault, theUIOrder,
025: delimiter);
026:
027: maxValueCount(1);
028: }
029:
030: /**
031: * Constructor for TypeProperty.
032: * @param theName String
033: * @param theDescription String
034: * @param theDefaults Class[]
035: * @param theUIOrder float
036: */
037: public TypeProperty(String theName, String theDescription,
038: Class[] theDefaults, float theUIOrder) {
039: super (theName, theDescription, theDefaults, theUIOrder,
040: delimiter);
041:
042: maxValueCount(Integer.MAX_VALUE);
043: }
044:
045: /**
046: * Method type.
047: * @return Class
048: * @see net.sourceforge.pmd.PropertyDescriptor#type()
049: */
050: public Class<Class> type() {
051: return Class.class;
052: }
053:
054: /**
055: * Method asString.
056: * @param value Object
057: * @return String
058: */
059: protected String asString(Object value) {
060: return value == null ? "" : ((Class) value).getName();
061: }
062:
063: /**
064: * Method classFrom.
065: * @param className String
066: * @return Class
067: */
068: private Class classFrom(String className) {
069:
070: Class cls = ClassUtil.getTypeFor(className);
071: if (cls != null)
072: return cls;
073:
074: try {
075: return Class.forName(className);
076: } catch (Exception ex) {
077: throw new IllegalArgumentException(className);
078: }
079: }
080:
081: /**
082: * Method valueFrom.
083: * @param valueString String
084: * @return Object
085: * @see net.sourceforge.pmd.PropertyDescriptor#valueFrom(String)
086: */
087: public Object valueFrom(String valueString) {
088:
089: if (maxValueCount() == 1)
090: return classFrom(valueString);
091:
092: String[] values = (String[]) super .valueFrom(valueString);
093:
094: Class[] classes = new Class[values.length];
095: for (int i = 0; i < values.length; i++)
096: classes[i] = classFrom(values[i]);
097: return classes;
098: }
099:
100: /**
101: * Neutralize unwanted superclass functionality that will result
102: * in a class cast exception.
103: *
104: * @param value Object
105: * @return String
106: */
107: protected String valueErrorFor(Object value) {
108: return null;
109: }
110: }
|