⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 componenthelper.java

📁 java ant的源码!非常值得看的源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            ? null : (String) antLibStack.peek();    }    /**     * Load ant's tasks.     */    private void initTasks() {        ClassLoader classLoader = getClassLoader(null);        Properties props = getDefaultDefinitions(false);        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);            def.setAdaptToClass(Task.class);            def.setAdapterClass(TaskAdapter.class);            antTypeTable.put(name, def);        }    }    private ClassLoader getClassLoader(ClassLoader classLoader) {        String buildSysclasspath = project.getProperty(MagicNames.BUILD_SYSCLASSPATH);        if (project.getCoreLoader() != null            && !(BUILD_SYSCLASSPATH_ONLY.equals(buildSysclasspath))) {            classLoader = project.getCoreLoader();        }        return classLoader;    }    /**     * Load default task or type definitions - just the names,     *  no class loading.     * Caches results between calls to reduce overhead.     * @param type true for typedefs, false for taskdefs     * @return a mapping from definition names to class names     * @throws BuildException if there was some problem loading     *                        or parsing the definitions list     */    private static synchronized Properties getDefaultDefinitions(boolean type)        throws BuildException {        int idx = type ? 1 : 0;        if (defaultDefinitions[idx] == null) {            String resource = type                ? 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.     */    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            boolean isAntlib = componentName.indexOf(MagicNames.ANTLIB_PREFIX) == 0;            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 (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);            }            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) {                out.println("Cause: the class " + classname + " was not found.");                jars = true;                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);                    definitions = true;                }            } catch (NoClassDefFoundError ncdfe) {                jars = true;                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);            }            //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();    }    /**     * Map that contains the component definitions.     */    private static class AntTypeTable extends Hashtable {        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);        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -