View Javadoc

1   package net.sf.josceleton.commons.reflect;
2   
3   import java.util.Arrays;
4   
5   import net.sf.josceleton.commons.exception.JosceletonException;
6   
7   /**
8    * @since 0.1
9    */
10  public class DynamicInstantiationException extends JosceletonException {
11  
12  	private static final long serialVersionUID = -4612520808805464973L;
13  	
14  	
15  	private final ClassAdapter<?> clazz;
16  	
17  	private final Object[] arguments;
18  	
19  	
20  	protected DynamicInstantiationException(
21  			final String message,
22  			final ClassAdapter<?> clazz,
23  			final Object[] arguments,
24  			final Throwable cause) {
25  		super(message, cause);
26  		
27  		this.clazz = clazz;
28  		this.arguments = arguments;
29  	}
30  
31  	/**
32  	 * @param clazz tried, but failed, to instantiate.
33  	 * @since 0.1
34  	 */
35  	public static DynamicInstantiationException newForInstantiation(
36  			final ClassAdapter<?> clazz,
37  			final Object[] arguments,
38  			final Throwable cause) {
39  		return new DynamicInstantiationException(
40  			"Could not create instance for class [" + clazz.getName() + "] " +
41  				"with arguments " +  Arrays.toString(arguments) + "!",
42  			clazz, arguments, cause
43  		);
44  	}
45  
46  	/**
47  	 * @param clazz which did not have a proper constructor for given arguments.
48  	 * @since 0.1
49  	 */
50  	public static DynamicInstantiationException newForNotFoundConstructor(
51  			final ClassAdapter<?> clazz,
52  			final Object[] arguments) {
53  		return new DynamicInstantiationException(
54  			"Could not find proper constructor for class [" + clazz.getName() + "] " +
55  				"with arguments " + Arrays.toString(arguments) + "!",
56  			clazz, arguments, null // no cause
57  		);
58  	}
59  
60  	/**
61  	 * @since 0.1
62  	 */
63  	public final ClassAdapter<?> getClazz() {
64  		return this.clazz;
65  	}
66  
67  	/**
68  	 * @since 0.1
69  	 */
70  	public final Object[] getArguments() {
71  		final Object[] copiedArguments = new Object[this.arguments.length];
72  		System.arraycopy(this.arguments, 0, copiedArguments, 0, this.arguments.length);
73  		return copiedArguments;
74  	}
75  
76  }