001package ball.util.ant.taskdefs; 002/*- 003 * ########################################################################## 004 * Utilities 005 * $Id: ClasspathDelegateAntTask.java 6074 2020-05-31 17:05:22Z ball $ 006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-util/trunk/src/main/java/ball/util/ant/taskdefs/ClasspathDelegateAntTask.java $ 007 * %% 008 * Copyright (C) 2008 - 2020 Allen D. Ball 009 * %% 010 * Licensed under the Apache License, Version 2.0 (the "License"); 011 * you may not use this file except in compliance with the License. 012 * You may obtain a copy of the License at 013 * 014 * http://www.apache.org/licenses/LICENSE-2.0 015 * 016 * Unless required by applicable law or agreed to in writing, software 017 * distributed under the License is distributed on an "AS IS" BASIS, 018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 019 * See the License for the specific language governing permissions and 020 * limitations under the License. 021 * ########################################################################## 022 */ 023import org.apache.tools.ant.AntClassLoader; 024import org.apache.tools.ant.BuildException; 025import org.apache.tools.ant.ProjectComponent; 026import org.apache.tools.ant.types.Path; 027import org.apache.tools.ant.types.Reference; 028import org.apache.tools.ant.util.ClasspathUtils; 029 030/** 031 * Interface to provide common default methods for 032 * {@link org.apache.tools.ant.Task}s that implement the syntax described in 033 * {@link org.apache.tools.ant.util.ClasspathUtils.Delegate}. 034 * 035 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball} 036 * @version $Revision: 6074 $ 037 */ 038public interface ClasspathDelegateAntTask extends AntTaskMixIn { 039 040 /** 041 * Required state for implementing {@link org.apache.tools.ant.Task}s. 042 * Refer to the discussion in {@link ClasspathUtils}. 043 * 044 * @return The {@link org.apache.tools.ant.util.ClasspathUtils.Delegate} 045 * instance created in the 046 * {@link org.apache.tools.ant.Task#init()} method. 047 */ 048 ClasspathUtils.Delegate delegate(); 049 050 /** 051 * Required state for implementing {@link org.apache.tools.ant.Task}s. 052 * Refer to the discussion in {@link ClasspathUtils}. 053 * 054 * @param delegate The 055 * {@link org.apache.tools.ant.util.ClasspathUtils.Delegate}. 056 * 057 * @return The {@link.this}. 058 */ 059 ClasspathDelegateAntTask delegate(ClasspathUtils.Delegate delegate); 060 061 /** 062 * Default implementation for {@link org.apache.tools.ant.Task} 063 * subclasses. 064 */ 065 default void init() throws BuildException { 066 if (delegate() == null) { 067 delegate(ClasspathUtils.getDelegate((ProjectComponent) this)); 068 } 069 } 070 071 /** 072 * See 073 * {@link org.apache.tools.ant.util.ClasspathUtils.Delegate#setClasspathref(Reference)}. 074 * 075 * @param reference The {@link Reference} to the classpath. 076 */ 077 default void setClasspathref(Reference reference) { 078 delegate().setClasspathref(reference); 079 } 080 081 /** 082 * See 083 * {@link org.apache.tools.ant.util.ClasspathUtils.Delegate#createClasspath()}. 084 * 085 * @return The created {@link Path}. 086 */ 087 default Path createClasspath() { return delegate().createClasspath(); } 088 089 /** 090 * See 091 * {@link org.apache.tools.ant.util.ClasspathUtils.Delegate#setClassname(String)}. 092 * 093 * @param name The class name ({@link String}). 094 */ 095 default void setClassname(String name) { delegate().setClassname(name); } 096 097 /** 098 * Method to get the {@link AntClassLoader} specified by {@link.this} 099 * {@link org.apache.tools.ant.Task}. 100 * 101 * @return The {@link AntClassLoader}. 102 */ 103 default AntClassLoader getClassLoader() { 104 if (delegate().getClasspath() == null) { 105 delegate().createClasspath(); 106 } 107 108 AntClassLoader loader = (AntClassLoader) delegate().getClassLoader(); 109 110 loader.setParent(getClass().getClassLoader()); 111 112 return loader; 113 } 114 115 /** 116 * Method to get the {@link Class} associated with the argument name 117 * using the {@link ClassLoader} provided by {@link #getClassLoader()}. 118 * 119 * @param name The fully qualified name of the desired 120 * class. 121 * 122 * @return The {@link Class} for the specified name. 123 * 124 * @throws ClassNotFoundException 125 * If the {@link Class} is not found. 126 */ 127 default Class<?> getClassForName(String name) throws ClassNotFoundException { 128 return Class.forName(name, false, getClassLoader()); 129 } 130}