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