1 11 package org.eclipse.ui.internal.components.framework; 12 13 import java.lang.reflect.Constructor ; 14 import java.lang.reflect.Modifier ; 15 import java.util.ArrayList ; 16 import java.util.Collections ; 17 import java.util.Comparator ; 18 import java.util.Iterator ; 19 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IConfigurationElement; 22 import org.eclipse.core.runtime.IExecutableExtension; 23 import org.eclipse.ui.internal.components.ComponentUtil; 24 import org.eclipse.ui.internal.components.ComponentMessages; 25 26 76 public class ReflectionFactory extends ComponentFactory implements IExecutableExtension { 77 78 private ClassIdentifier classId = null; 79 private Class targetClass = null; 80 81 87 public ReflectionFactory() { 88 } 89 90 95 public ReflectionFactory(Class toCreateByReflection) { 96 targetClass = toCreateByReflection; 97 } 98 99 104 public ReflectionFactory(ClassIdentifier classId) { 105 this.classId = classId; 106 } 107 108 111 public void setInitializationData(IConfigurationElement config, 112 String propertyName, Object data) throws CoreException { 113 114 classId = ComponentUtil.getClassFromInitializationData(config, data); 115 } 116 117 120 public ComponentHandle createHandle(IServiceProvider availableServices) 121 throws ComponentException { 122 123 Class targetClass = this.targetClass; 124 125 if (targetClass == null) { 127 targetClass = ComponentUtil.loadClass(classId); 128 } 129 130 Constructor targetConstructor = null; 131 132 Constructor [] constructors = targetClass.getConstructors(); 133 134 if (constructors.length == 1) { 136 targetConstructor = constructors[0]; 137 } else { 138 ArrayList toSort = new ArrayList (); 139 140 for (int i = 0; i < constructors.length; i++) { 141 Constructor constructor = constructors[i]; 142 143 if ((constructor.getModifiers() & Modifier.PUBLIC) != 0) { 145 toSort.add(constructor); 146 } 147 } 148 149 Collections.sort(toSort, new Comparator () { 151 154 public int compare(Object arg0, Object arg1) { 155 Constructor c1 = (Constructor )arg0; 156 Constructor c2 = (Constructor )arg1; 157 158 int l1 = c1.getParameterTypes().length; 159 int l2 = c2.getParameterTypes().length; 160 161 return l1 - l2; 162 } 163 }); 164 165 for (Iterator iter = toSort.iterator(); iter.hasNext() && targetConstructor == null;) { 167 Constructor next = (Constructor ) iter.next(); 168 169 boolean satisfiable = true; 170 171 Class [] params = next.getParameterTypes(); 172 for (int i = 0; i < params.length && satisfiable; i++) { 173 Class clazz = params[i]; 174 175 if (!availableServices.hasService(clazz)) { 176 satisfiable = false; 177 } 178 } 179 180 if (satisfiable) { 181 targetConstructor = next; 182 } 183 } 184 } 185 186 if (targetConstructor == null) { 187 throw new ComponentException(targetClass, ComponentMessages.ReflectionFactory_no_constructors, null); 188 } 189 190 Class [] paramKeys = targetConstructor.getParameterTypes(); 191 192 try { 193 Object [] params = Components.queryInterfaces(availableServices, paramKeys); 194 195 return new ComponentHandle(targetConstructor.newInstance(params)); 196 } catch (Exception e) { 197 throw new ComponentException(targetClass, e); 198 } 199 } 200 201 } 202 | Popular Tags |