1 /** 2 * Copyright 2006-2017 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.objenesis.tck.search; 17 18 import org.objenesis.instantiator.ObjectInstantiator; 19 import org.objenesis.strategy.PlatformDescription; 20 import org.objenesis.tck.candidates.SerializableNoConstructor; 21 22 import java.io.Serializable; 23 import java.lang.reflect.Constructor; 24 import java.lang.reflect.InvocationTargetException; 25 import java.util.Iterator; 26 import java.util.SortedSet; 27 28 /** 29 * This class will try every available instantiator on the platform to see which works. 30 * 31 * @author Henri Tremblay 32 */ 33 public class SearchWorkingInstantiator implements Serializable { // implements Serializable just for the test 34 35 private SearchWorkingInstantiatorListener listener; 36 37 public static void main(String[] args) throws Exception { 38 System.out.println(); 39 System.out.println(PlatformDescription.describePlatform()); 40 System.out.println(); 41 42 SearchWorkingInstantiator searchWorkingInstantiator = new SearchWorkingInstantiator(new SystemOutListener()); 43 searchWorkingInstantiator.searchForInstantiator(SerializableNoConstructor.class); 44 } 45 46 public SearchWorkingInstantiator(SearchWorkingInstantiatorListener listener) { 47 this.listener = listener; 48 } 49 50 public void searchForInstantiator(Class<?> toInstantiate) { 51 SortedSet<String> classes = ClassEnumerator.getClassesForPackage(ObjectInstantiator.class.getPackage()); 52 53 for (Iterator<String> it = classes.iterator(); it.hasNext();) { 54 String className = it.next(); 55 56 // Skip if inner class of isn't named like a instantiator 57 if(className.contains("$") || !className.endsWith("Instantiator")) { 58 continue; 59 } 60 61 Class<?> c = null; 62 try { 63 c = Class.forName(className); 64 } 65 catch(Exception e) { 66 listener.instantiatorUnsupported(c, e); 67 continue; 68 } 69 70 if(c.isInterface() || !ObjectInstantiator.class.isAssignableFrom(c)) { 71 continue; 72 } 73 74 Constructor<?> constructor; 75 try { 76 constructor = c.getConstructor(Class.class); 77 } catch (NoSuchMethodException e) { 78 throw new RuntimeException(e); 79 } 80 81 try { 82 ObjectInstantiator<?> instantiator = 83 (ObjectInstantiator<?>) constructor.newInstance(toInstantiate); 84 instantiator.newInstance(); 85 listener.instantiatorSupported(c); 86 } 87 catch(Exception e) { 88 Throwable t = (e instanceof InvocationTargetException) ? e.getCause() : e; 89 listener.instantiatorUnsupported(c, t); 90 } 91 } 92 } 93 } 94