| 1 | /* |
| 2 | * Copyright (C) Christian Schulte, 2005-206 |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * |
| 9 | * o Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * |
| 12 | * o Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in |
| 14 | * the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
| 19 | * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | * |
| 28 | * $JOMC: KeyValueType.java 4613 2012-09-22 10:07:08Z schulte $ |
| 29 | * |
| 30 | */ |
| 31 | package org.jomc.ant.types; |
| 32 | |
| 33 | import java.lang.reflect.InvocationTargetException; |
| 34 | import java.lang.reflect.Method; |
| 35 | import java.lang.reflect.Modifier; |
| 36 | import org.apache.commons.lang.builder.ToStringBuilder; |
| 37 | import org.apache.tools.ant.BuildException; |
| 38 | import org.apache.tools.ant.Location; |
| 39 | |
| 40 | /** |
| 41 | * Datatype holding a {@code key}, {@code value} and {@code type} property. |
| 42 | * |
| 43 | * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> |
| 44 | * @version $JOMC: KeyValueType.java 4613 2012-09-22 10:07:08Z schulte $ |
| 45 | */ |
| 46 | public class KeyValueType implements Cloneable |
| 47 | { |
| 48 | |
| 49 | /** The key of the type. */ |
| 50 | private String key; |
| 51 | |
| 52 | /** The value of the type. */ |
| 53 | private String value; |
| 54 | |
| 55 | /** The class of the type of {@code value}. */ |
| 56 | private Class<?> type; |
| 57 | |
| 58 | /** Creates a new {@code KeyValueType} instance. */ |
| 59 | public KeyValueType() |
| 60 | { |
| 61 | super(); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Gets the value of the {@code key} property. |
| 66 | * |
| 67 | * @return The value of the {@code key} property. |
| 68 | * |
| 69 | * @see #setKey(java.lang.String) |
| 70 | */ |
| 71 | public final String getKey() |
| 72 | { |
| 73 | return this.key; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Sets the value of the {@code key} property. |
| 78 | * |
| 79 | * @param k The new value of the {@code key} property. |
| 80 | * |
| 81 | * @see #getKey() |
| 82 | */ |
| 83 | public final void setKey( final String k ) |
| 84 | { |
| 85 | this.key = k; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Gets the value of the {@code value} property. |
| 90 | * |
| 91 | * @return The value of the {@code value} property or {@code null}. |
| 92 | * |
| 93 | * @see #setValue(java.lang.String) |
| 94 | */ |
| 95 | public final String getValue() |
| 96 | { |
| 97 | return this.value; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Sets the value of the {@code value} property. |
| 102 | * |
| 103 | * @param v The new value of the {@code value} property or {@code null}. |
| 104 | * |
| 105 | * @see #getValue() |
| 106 | */ |
| 107 | public final void setValue( final String v ) |
| 108 | { |
| 109 | this.value = v; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Gets the value of the {@code type} property. |
| 114 | * |
| 115 | * @return The value of the {@code type} property or {@code null}. |
| 116 | * |
| 117 | * @see #setType(java.lang.Class) |
| 118 | */ |
| 119 | public final Class<?> getType() |
| 120 | { |
| 121 | return this.type; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Sets the value of the {@code type} property. |
| 126 | * |
| 127 | * @param t The new value of the {@code type} property or {@code null}. |
| 128 | * |
| 129 | * @see #getType() |
| 130 | */ |
| 131 | public final void setType( final Class<?> t ) |
| 132 | { |
| 133 | this.type = t; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Gets the object of the instance. |
| 138 | * |
| 139 | * @param location The location the object is requested at. |
| 140 | * |
| 141 | * @return The object of the instance or {@code null}. |
| 142 | * |
| 143 | * @throws NullPointerException if {@code location} is {@code null}. |
| 144 | * @throws BuildException if getting the object fails. |
| 145 | * |
| 146 | * @see #getType() |
| 147 | * @see #getValue() |
| 148 | */ |
| 149 | public Object getObject( final Location location ) throws BuildException |
| 150 | { |
| 151 | if ( location == null ) |
| 152 | { |
| 153 | throw new NullPointerException( "location" ); |
| 154 | } |
| 155 | |
| 156 | try |
| 157 | { |
| 158 | Object o = this.getValue(); |
| 159 | |
| 160 | if ( o != null ) |
| 161 | { |
| 162 | if ( this.getType() != null && !String.class.equals( this.getType() ) ) |
| 163 | { |
| 164 | try |
| 165 | { |
| 166 | o = this.getType().getConstructor( String.class ).newInstance( o ); |
| 167 | } |
| 168 | catch ( final NoSuchMethodException e ) |
| 169 | { |
| 170 | final Method valueOf = this.getType().getMethod( "valueOf", String.class ); |
| 171 | |
| 172 | if ( Modifier.isStatic( valueOf.getModifiers() ) |
| 173 | && valueOf.getReturnType().equals( this.getType() ) ) |
| 174 | { |
| 175 | o = valueOf.invoke( null, o ); |
| 176 | } |
| 177 | else |
| 178 | { |
| 179 | throw new BuildException( |
| 180 | Messages.getMessage( "noSuchMethodCreatingValueObject", this.getType(), |
| 181 | this.getValue(), this.getType().getSimpleName() ), e, location ); |
| 182 | |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | else if ( this.getType() != null ) |
| 188 | { |
| 189 | o = this.getType().newInstance(); |
| 190 | } |
| 191 | |
| 192 | return o; |
| 193 | } |
| 194 | catch ( final NoSuchMethodException e ) |
| 195 | { |
| 196 | throw new BuildException( |
| 197 | Messages.getMessage( "noSuchMethodCreatingValueObject", this.getType(), |
| 198 | this.getValue(), this.getType().getSimpleName() ), e, location ); |
| 199 | |
| 200 | } |
| 201 | catch ( final InstantiationException e ) |
| 202 | { |
| 203 | throw new BuildException( Messages.getMessage( "failureCreatingValueObject", this.getType(), |
| 204 | this.getValue() ), e, location ); |
| 205 | |
| 206 | } |
| 207 | catch ( final IllegalAccessException e ) |
| 208 | { |
| 209 | throw new BuildException( Messages.getMessage( "failureCreatingValueObject", this.getType(), |
| 210 | this.getValue() ), e, location ); |
| 211 | |
| 212 | } |
| 213 | catch ( final InvocationTargetException e ) |
| 214 | { |
| 215 | throw new BuildException( Messages.getMessage( "failureCreatingValueObject", this.getType(), |
| 216 | this.getValue() ), e, location ); |
| 217 | |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Creates and returns a copy of this object. |
| 223 | * |
| 224 | * @return A copy of this object. |
| 225 | */ |
| 226 | @Override |
| 227 | public KeyValueType clone() |
| 228 | { |
| 229 | try |
| 230 | { |
| 231 | return (KeyValueType) super.clone(); |
| 232 | } |
| 233 | catch ( final CloneNotSupportedException e ) |
| 234 | { |
| 235 | throw new AssertionError( e ); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Creates and returns a string representation of the object. |
| 241 | * |
| 242 | * @return A string representation of the object. |
| 243 | */ |
| 244 | @Override |
| 245 | public String toString() |
| 246 | { |
| 247 | return ToStringBuilder.reflectionToString( this ); |
| 248 | } |
| 249 | |
| 250 | } |