001package ball.util.ant.types; 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 ball.util.Factory; 022import ball.util.ant.taskdefs.NotNull; 023import java.beans.ConstructorProperties; 024import org.apache.tools.ant.BuildException; 025 026/** 027 * Class to provide a typed name-value (attribute) for 028 * {@link.uri http://ant.apache.org/ Ant} {@link org.apache.tools.ant.Task} 029 * implementations. 030 * 031 * {@bean.info} 032 * 033 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball} 034 */ 035public class TypedAttributeType extends StringAttributeType { 036 private String type = String.class.getName(); 037 038 /** 039 * @param name The attribute name. 040 */ 041 @ConstructorProperties({ "name" }) 042 public TypedAttributeType(String name) { super(name); } 043 044 /** 045 * No-argument constructor. 046 */ 047 public TypedAttributeType() { this(null); } 048 049 @NotNull 050 public String getType() { return type; } 051 public void setType(String type) { this.type = type; } 052 053 public Object getTypedValue() throws BuildException { 054 return getTypedValue(getClass().getClassLoader()); 055 } 056 057 public Object getTypedValue(ClassLoader loader) throws BuildException { 058 Object object = null; 059 060 try { 061 Class<?> type = Class.forName(getType(), false, loader); 062 Factory<?> factory = new Factory<>(type); 063 064 object = factory.getInstance(getValue()); 065 } catch (BuildException exception) { 066 throw exception; 067 } catch (RuntimeException exception) { 068 throw exception; 069 } catch (Exception exception) { 070 throw new BuildException(exception); 071 } 072 073 return object; 074 } 075}