1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.ezmorph.array;
18
19 import java.lang.reflect.Array;
20 import java.lang.reflect.Method;
21
22 import net.sf.ezmorph.MorphException;
23 import net.sf.ezmorph.Morpher;
24
25 import org.apache.commons.lang.builder.HashCodeBuilder;
26
27
28
29
30
31
32 public final class ObjectArrayMorpher extends AbstractArrayMorpher
33 {
34 private Morpher morpher;
35 private Method morphMethod;
36 private Class target;
37 private Class targetArrayClass;
38
39
40
41
42
43
44
45
46
47 public ObjectArrayMorpher( Morpher morpher )
48 {
49 super( false );
50 setMorpher( morpher );
51 }
52
53 public boolean equals( Object obj )
54 {
55 if( this == obj ){
56 return true;
57 }
58 if( obj == null ){
59 return false;
60 }
61
62 if( !(obj instanceof ObjectArrayMorpher) ){
63 return false;
64 }
65
66 ObjectArrayMorpher other = (ObjectArrayMorpher) obj;
67 return morpher.equals( other.morpher );
68 }
69
70 public int hashCode()
71 {
72 return new HashCodeBuilder().append( morpher )
73 .toHashCode();
74 }
75
76 public Object morph( Object array )
77 {
78 if( array == null ){
79 return null;
80 }
81
82 if( array.getClass()
83 .isArray() ){
84 int length = Array.getLength( array );
85 int dims = getDimensions( array.getClass() );
86 int[] dimensions = createDimensions( dims, length );
87 Object result = Array.newInstance( this.target, dimensions );
88
89 if( dims == 1 ){
90 for( int index = 0; index < length; index++ ){
91 try{
92 Object value = Array.get( array, index );
93 if( value != null && !morpher.supports( value.getClass() ) ){
94 throw new MorphException( value.getClass() + " is not supported" );
95 }
96 Object morphed = morphMethod.invoke( morpher, new Object[] { value } );
97 Array.set( result, index, morphed );
98 }
99 catch( MorphException me ){
100 throw me;
101 }
102 catch( Exception e ){
103 throw new MorphException( e );
104 }
105 }
106 }else{
107 for( int index = 0; index < length; index++ ){
108 Array.set( result, index, morph( Array.get( array, index ) ) );
109 }
110 }
111
112 return result;
113 }else{
114 throw new MorphException( "argument is not an array: " + array.getClass() );
115 }
116 }
117
118 public Class morphsTo()
119 {
120 return targetArrayClass;
121 }
122
123 public boolean supports( Class clazz )
124 {
125 if( clazz != null && !clazz.isArray() ){
126 return false;
127 }
128 while( clazz.isArray() ){
129 clazz = clazz.getComponentType();
130 }
131 return morpher.supports( clazz );
132 }
133
134 private void setMorpher( Morpher morpher )
135 {
136 if( morpher == null ){
137 throw new IllegalArgumentException( "morpher can not be null" );
138 }
139 if( morpher.morphsTo()
140 .isArray() ){
141 throw new IllegalArgumentException( "morpher target class can not be an array" );
142 }
143 this.morpher = morpher;
144 this.targetArrayClass = Array.newInstance( morpher.morphsTo(), 1 )
145 .getClass();
146 this.target = morpher.morphsTo();
147
148
149 try{
150 morphMethod = morpher.getClass()
151 .getDeclaredMethod( "morph", new Class[] { Object.class } );
152 }
153 catch( NoSuchMethodException nsme ){
154 throw new IllegalArgumentException( nsme.getMessage() );
155 }
156 }
157 }