📄 componenthelper.java
字号:
? MagicNames.TYPEDEFS_PROPERTIES_RESOURCE : MagicNames.TASKDEF_PROPERTIES_RESOURCE; String errorString = type ? ERROR_NO_TYPE_LIST_LOAD : ERROR_NO_TASK_LIST_LOAD; InputStream in = null; try { in = ComponentHelper.class.getResourceAsStream( resource); if (in == null) { throw new BuildException(errorString); } Properties p = new Properties(); p.load(in); defaultDefinitions[idx] = p; } catch (IOException e) { throw new BuildException(errorString, e); } finally { FileUtils.close(in); } } return defaultDefinitions[idx]; } /** * Load ant's datatypes. */ private void initTypes() { ClassLoader classLoader = getClassLoader(null); Properties props = getDefaultDefinitions(true); Enumeration e = props.propertyNames(); while (e.hasMoreElements()) { String name = (String) e.nextElement(); String className = props.getProperty(name); AntTypeDefinition def = new AntTypeDefinition(); def.setName(name); def.setClassName(className); def.setClassLoader(classLoader); antTypeTable.put(name, def); } } /** * Called for each component name, check if the * associated URI has been examined for antlibs. * @param componentName the name of the component, which should include a URI * prefix if it is in a namespace */ private synchronized void checkNamespace(String componentName) { String uri = ProjectHelper.extractUriFromComponentName(componentName); if ("".equals(uri)) { uri = ProjectHelper.ANT_CORE_URI; } if (!uri.startsWith(ProjectHelper.ANTLIB_URI)) { return; // namespace that does not contain antlib } if (checkedNamespaces.contains(uri)) { return; // Already processed } checkedNamespaces.add(uri); Typedef definer = new Typedef(); definer.setProject(project); definer.init(); definer.setURI(uri); //there to stop error messages being "null" definer.setTaskName(uri); //if this is left out, bad things happen. like all build files break //on the first element encountered. definer.setResource(Definer.makeResourceFromURI(uri)); // a fishing expedition :- ignore errors if antlib not present definer.setOnError(new Typedef.OnError(Typedef.OnError.POLICY_IGNORE)); definer.execute(); } /** * Handler called to do decent diagnosis on instantiation failure. * @param componentName component name. * @param type component type, used in error messages * @return a string containing as much diagnostics info as possible. */ public String diagnoseCreationFailure(String componentName, String type) { StringWriter errorText = new StringWriter(); PrintWriter out = new PrintWriter(errorText); out.println("Problem: failed to create " + type + " " + componentName); //class of problem boolean lowlevel = false; boolean jars = false; boolean definitions = false; boolean antTask; String home = System.getProperty(Launcher.USER_HOMEDIR); File libDir = new File(home, Launcher.USER_LIBDIR); String antHomeLib; boolean probablyIDE = false; String anthome = System.getProperty(MagicNames.ANT_HOME); if (anthome != null) { File antHomeLibDir = new File(anthome, "lib"); antHomeLib = antHomeLibDir.getAbsolutePath(); } else { //running under an IDE that doesn't set ANT_HOME probablyIDE = true; antHomeLib = "ANT_HOME" + File.separatorChar + "lib"; } StringBuffer dirListingText = new StringBuffer(); final String tab = " -"; dirListingText.append(tab); dirListingText.append(antHomeLib); dirListingText.append('\n'); if (probablyIDE) { dirListingText.append(tab); dirListingText.append("the IDE Ant configuration dialogs"); } else { dirListingText.append(tab); dirListingText.append(libDir); dirListingText.append('\n'); dirListingText.append(tab); dirListingText.append( "a directory added on the command line with the -lib argument"); } String dirListing = dirListingText.toString(); //look up the name AntTypeDefinition def = getDefinition(componentName); if (def == null) { //not a known type printUnknownDefinition(out, componentName, dirListing); definitions = true; } else { //we are defined, so it is an instantiation problem final String classname = def.getClassName(); antTask = classname.startsWith("org.apache.tools.ant."); boolean optional = classname.startsWith("org.apache.tools.ant.taskdefs.optional"); optional |= classname.startsWith("org.apache.tools.ant.types.optional"); //start with instantiating the class. Class clazz = null; try { clazz = def.innerGetTypeClass(); } catch (ClassNotFoundException e) { jars = true; if (!optional) { definitions = true; } printClassNotFound(out, classname, optional, dirListing); } catch (NoClassDefFoundError ncdfe) { jars = true; printNotLoadDependentClass(out, optional, ncdfe, dirListing); } //here we successfully loaded the class or failed. if (clazz != null) { //success: proceed with more steps try { def.innerCreateAndSet(clazz, project); //hey, there is nothing wrong with us out.println("The component could be instantiated."); } catch (NoSuchMethodException e) { lowlevel = true; out.println("Cause: The class " + classname + " has no compatible constructor."); } catch (InstantiationException e) { lowlevel = true; out.println("Cause: The class " + classname + " is abstract and cannot be instantiated."); } catch (IllegalAccessException e) { lowlevel = true; out.println("Cause: The constructor for " + classname + " is private and cannot be invoked."); } catch (InvocationTargetException ex) { lowlevel = true; Throwable t = ex.getTargetException(); out.println("Cause: The constructor threw the exception"); out.println(t.toString()); t.printStackTrace(out); } catch (NoClassDefFoundError ncdfe) { jars = true; out.println("Cause: A class needed by class " + classname + " cannot be found: "); out.println(" " + ncdfe.getMessage()); out.println("Action: Determine what extra JAR files are" + " needed, and place them in:"); out.println(dirListing); } } out.println(); out.println("Do not panic, this is a common problem."); if (definitions) { out.println("It may just be a typographical error in the build file " + "or the task/type declaration."); } if (jars) { out.println("The commonest cause is a missing JAR."); } if (lowlevel) { out.println("This is quite a low level problem, which may need " + "consultation with the author of the task."); if (antTask) { out.println("This may be the Ant team. Please file a " + "defect or contact the developer team."); } else { out.println("This does not appear to be a task bundled with Ant."); out.println("Please take it up with the supplier of the third-party " + type + "."); out.println("If you have written it yourself, you probably have a bug to fix."); } } else { out.println(); out.println("This is not a bug; it is a configuration problem"); } } out.flush(); out.close(); return errorText.toString(); } /** * Print unknown definition.forking */ private void printUnknownDefinition( PrintWriter out, String componentName, String dirListing) { boolean isAntlib = componentName.indexOf(MagicNames.ANTLIB_PREFIX) == 0; String uri=ProjectHelper.extractUriFromComponentName(componentName); out.println("Cause: The name is undefined."); out.println("Action: Check the spelling."); out.println("Action: Check that any custom tasks/types have been declared."); out.println("Action: Check that any <presetdef>/<macrodef>" + " declarations have taken place."); if(uri.length()>0) { List matches = antTypeTable.findMatches(uri); if(matches.size()>0) { out.println(); out.println("The definitions in the namespace "+uri+" are:"); for(Iterator it=matches.iterator();it.hasNext();) { AntTypeDefinition def=(AntTypeDefinition) it.next(); String local = ProjectHelper.extractNameFromComponentName(def.getName()); out.println(" "+local); } } else { out.println("No types or tasks have been defined in this namespace yet"); if (isAntlib) { out.println(); out.println("This appears to be an antlib declaration. "); out.println("Action: Check that the implementing library exists in one of:"); out.println(dirListing); } } } } /** * Print class not found. */ private void printClassNotFound( PrintWriter out, String classname, boolean optional, String dirListing) { out.println("Cause: the class " + classname + " was not found."); if (optional) { out.println(" This looks like one of Ant's optional components."); out.println("Action: Check that the appropriate optional JAR exists in"); out.println(dirListing); } else { out.println("Action: Check that the component has been correctly declared"); out.println(" and that the implementing JAR is in one of:"); out.println(dirListing); } } /** * Print could not load dependent class. */ private void printNotLoadDependentClass( PrintWriter out, boolean optional, NoClassDefFoundError ncdfe, String dirListing) { out.println("Cause: Could not load a dependent class " + ncdfe.getMessage()); if (optional) { out.println(" It is not enough to have Ant's optional JARs"); out.println(" you need the JAR files that the" + " optional tasks depend upon."); out.println(" Ant's optional task dependencies are" + " listed in the manual."); } else { out.println(" This class may be in a separate JAR" + " that is not installed."); } out.println("Action: Determine what extra JAR files are" + " needed, and place them in one of:"); out.println(dirListing); } /** * Map that contains the component definitions. */ private static class AntTypeTable extends Hashtable { private static final long serialVersionUID = -3060442320477772028L; private Project project; AntTypeTable(Project project) { this.project = project; } AntTypeDefinition getDefinition(String key) { return (AntTypeDefinition) (super.get(key)); } public Object get(Object key) { return getTypeClass((String) key); } Object create(String name) { AntTypeDefinition def = getDefinition(name); return (def == null) ? null : def.create(project); } Class getTypeClass(String name) { AntTypeDefinition def = getDefinition(name); return (def == null) ? null : def.getTypeClass(project); } Class getExposedClass(String name) { AntTypeDefinition def = getDefinition(name); return (def == null) ? null : def.getExposedClass(project); } public boolean contains(Object clazz) { boolean found = false; if (clazz instanceof Class) { for (Iterator i = values().iterator(); i.hasNext() && !found;) { found = (((AntTypeDefinition) (i.next())).getExposedClass( project) == clazz); } } return found; } public boolean containsValue(Object value) { return contains(value); } /** * Create a list of all definitions that match a prefix, usually the URI * of a library * @param prefix prefix to match off * @return the (possibly empty) list of definitions */ public List/*<AntTypeDefinition>*/ findMatches(String prefix) { ArrayList matches=new ArrayList(); for (Iterator i = values().iterator(); i.hasNext() ;) { AntTypeDefinition def = (AntTypeDefinition) (i.next()); if(def.getName().startsWith(prefix)) { matches.add(def); } } return matches; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -