1 package com.puppycrawl.tools.checkstyle; 20 21 import java.util.ArrayList ; 22 import java.util.List ; 23 24 import com.puppycrawl.tools.checkstyle.api.CheckstyleException; 25 26 32 class PackageObjectFactory implements ModuleFactory 33 { 34 35 private final List mPackages = new ArrayList (); 36 37 40 PackageObjectFactory() 41 { 42 } 43 44 48 String [] getPackages() 49 { 50 return (String []) mPackages.toArray(new String [mPackages.size()]); 51 } 52 53 57 void addPackage(String aPackageName) 58 { 59 mPackages.add(aPackageName); 60 } 61 62 71 private Object doMakeObject(String aName) 72 throws CheckstyleException 73 { 74 try { 76 return createObject(aName); 77 } 78 catch (final CheckstyleException ex) { 79 ; } 81 82 for (int i = 0; i < mPackages.size(); i++) { 84 final String packageName = (String ) mPackages.get(i); 85 final String className = packageName + aName; 86 try { 87 return createObject(className); 88 } 89 catch (final CheckstyleException ex) { 90 ; } 92 } 93 94 throw new CheckstyleException("Unable to instantiate " + aName); 95 } 96 97 103 private Object createObject(String aClassName) 104 throws CheckstyleException 105 { 106 try { 107 final ClassLoader loader = Thread.currentThread() 108 .getContextClassLoader(); 109 final Class clazz = Class.forName(aClassName, true, loader); 110 return clazz.newInstance(); 111 } 112 catch (final ClassNotFoundException e) { 113 throw new CheckstyleException( 114 "Unable to find class for " + aClassName, e); 115 } 116 catch (final InstantiationException e) { 117 throw new CheckstyleException( 119 "Unable to instantiate " + aClassName, e); 120 } 122 catch (final IllegalAccessException e) { 123 throw new CheckstyleException( 125 "Unable to instantiate " + aClassName, e); 126 } 128 } 129 130 140 public Object createModule(String aName) 141 throws CheckstyleException 142 { 143 try { 144 return doMakeObject(aName); 145 } 146 catch (final CheckstyleException ex) { 147 try { 149 return doMakeObject(aName + "Check"); 150 } 151 catch (final CheckstyleException ex2) { 152 throw new CheckstyleException( 153 "Unable to instantiate " + aName, ex2); 154 } 155 } 156 } 157 } 158 | Popular Tags |