001 /**
002 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
003 */
004 package net.sourceforge.pmd.lang.rule.properties;
005
006 import net.sourceforge.pmd.util.StringUtil;
007
008 /**
009 * Defines a property that supports multiple class types, even for primitive values!
010 *
011 * TODO - untested for array types
012 *
013 * @author Brian Remedios
014 */
015 public class TypeMultiProperty extends AbstractMultiPackagedProperty<Class[]> {
016
017 /**
018 * Constructor for TypeProperty.
019 * @param theName String
020 * @param theDescription String
021 * @param theDefaults Class[]
022 * @param legalPackageNames String[]
023 * @param theUIOrder float
024 * @throws IllegalArgumentException
025 */
026 public TypeMultiProperty(String theName, String theDescription, Class<?>[] theDefaults, String[] legalPackageNames, float theUIOrder) {
027 super(theName, theDescription, theDefaults, legalPackageNames, theUIOrder);
028
029 }
030
031 /**
032 * Constructor for TypeProperty.
033 * @param theName String
034 * @param theDescription String
035 * @param theTypeDefaults String
036 * @param legalPackageNames String[]
037 * @param theUIOrder float
038 * @throws IllegalArgumentException
039 */
040 public TypeMultiProperty(String theName, String theDescription, String theTypeDefaults, String[] legalPackageNames, float theUIOrder) {
041 this(theName, theDescription, typesFrom(theTypeDefaults), legalPackageNames, theUIOrder);
042
043 }
044
045 /**
046 * @param classesStr String
047 * @return Class[]
048 */
049 public static Class[] typesFrom(String classesStr) {
050 String[] values = StringUtil.substringsOf(classesStr, DELIMITER);
051
052 Class<?>[] classes = new Class<?>[values.length];
053 for (int i = 0; i < values.length; i++) {
054 classes[i] = TypeProperty.classFrom(values[i]);
055 }
056 return classes;
057 }
058
059 /**
060 * @param item Object
061 * @return String
062 */
063 @Override
064 protected String packageNameOf(Object item) {
065 return ((Class<?>) item).getName();
066 }
067
068 /**
069 * @return Class
070 * @see net.sourceforge.pmd.PropertyDescriptor#type()
071 */
072 public Class<Class[]> type() {
073 return Class[].class;
074 }
075
076 /**
077 * @return String
078 */
079 @Override
080 protected String itemTypeName() {
081 return "type";
082 }
083
084 /**
085 * @param value Object
086 * @return String
087 */
088 @Override
089 protected String asString(Object value) {
090 return value == null ? "" : ((Class<?>) value).getName();
091 }
092
093 /**
094 * @param valueString String
095 * @return Object
096 * @see net.sourceforge.pmd.PropertyDescriptor#valueFrom(String)
097 */
098 public Class<?>[] valueFrom(String valueString) {
099 return typesFrom(valueString);
100 }
101 }
|