1 37 package net.sourceforge.cruisecontrol; 38 39 import java.lang.reflect.Method ; 40 import java.lang.reflect.Modifier ; 41 import java.util.LinkedList ; 42 import java.util.List ; 43 44 47 public class GenericPluginDetail implements PluginDetail { 48 private final String name; 49 private final PluginType type; 50 private final Attribute[] requiredAttributes; 51 52 public GenericPluginDetail(String name, Class plugin) { 53 this.name = name; 54 this.requiredAttributes = lookupRequiredAttributes(plugin); 55 this.type = PluginType.find(plugin); 56 } 57 58 public String getName() { 59 return name; 60 } 61 62 public PluginType getType() { 63 return type; 64 } 65 66 public Attribute[] getRequiredAttributes() { 67 return requiredAttributes; 68 } 69 70 public int compareTo(Object other) { 71 return this.getName().compareTo(((PluginDetail) other).getName()); 72 } 73 74 private static Attribute[] lookupRequiredAttributes(Class plugin) { 75 List attrs = new LinkedList (); 76 Method [] methods = plugin.getMethods(); 77 for (int i = 0; i < methods.length; i++) { 78 Method method = methods[i]; 79 if (isRequiredAttribute(method)) { 80 String methodName = method.getName(); 81 String attributeName = methodName.substring(3, 4).toLowerCase() + methodName.substring(4); 82 attrs.add(new Attribute(attributeName, method.getParameterTypes()[0])); 83 } 84 } 85 86 return (Attribute[]) attrs.toArray(new Attribute[attrs.size()]); 87 } 88 89 private static boolean isRequiredAttribute(Method method) { 90 return method.getName().startsWith("set") && Modifier.isPublic(method.getModifiers()); 91 } 92 } 93 | Popular Tags |