|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
DefaultImplementationBuilderImpl.java | 100% | 97% | 100% | 98.1% |
|
1 |
// Copyright 2004 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
14 |
|
|
15 |
package org.apache.hivemind.lib.impl;
|
|
16 |
|
|
17 |
import java.lang.reflect.Modifier;
|
|
18 |
import java.util.Collections;
|
|
19 |
import java.util.HashMap;
|
|
20 |
import java.util.Map;
|
|
21 |
|
|
22 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
23 |
import org.apache.hivemind.impl.BaseLocatable;
|
|
24 |
import org.apache.hivemind.internal.Module;
|
|
25 |
import org.apache.hivemind.lib.DefaultImplementationBuilder;
|
|
26 |
import org.apache.hivemind.service.ClassFab;
|
|
27 |
import org.apache.hivemind.service.ClassFabUtils;
|
|
28 |
import org.apache.hivemind.service.ClassFactory;
|
|
29 |
import org.apache.hivemind.service.MethodIterator;
|
|
30 |
import org.apache.hivemind.service.MethodSignature;
|
|
31 |
|
|
32 |
/**
|
|
33 |
* Implemenation of {@link org.apache.hivemind.lib.DefaultImplementationBuilder}.
|
|
34 |
*
|
|
35 |
* @author Howard Lewis Ship
|
|
36 |
*/
|
|
37 |
public class DefaultImplementationBuilderImpl |
|
38 |
extends BaseLocatable
|
|
39 |
implements DefaultImplementationBuilder
|
|
40 |
{ |
|
41 |
private Map _instances = Collections.synchronizedMap(new HashMap()); |
|
42 |
|
|
43 |
private ClassFactory _classFactory;
|
|
44 |
|
|
45 | 10 |
public Object buildDefaultImplementation(Class interfaceType, Module module)
|
46 |
{ |
|
47 | 10 |
Object result = _instances.get(interfaceType); |
48 |
|
|
49 | 10 |
if (result == null) |
50 |
{ |
|
51 | 9 |
result = create(interfaceType, module); |
52 | 8 |
_instances.put(interfaceType, result); |
53 |
} |
|
54 |
|
|
55 | 9 |
return result;
|
56 |
} |
|
57 |
|
|
58 | 9 |
private Object create(Class interfaceType, Module module)
|
59 |
{ |
|
60 | 9 |
Class defaultClass = createClass(interfaceType, module); |
61 |
|
|
62 | 8 |
try
|
63 |
{ |
|
64 | 8 |
return defaultClass.newInstance();
|
65 |
} |
|
66 |
catch (Exception ex)
|
|
67 |
{ |
|
68 | 0 |
throw new ApplicationRuntimeException( |
69 |
ImplMessages.unableToCreateDefaultImplementation(interfaceType, ex), |
|
70 |
ex); |
|
71 |
} |
|
72 |
} |
|
73 |
|
|
74 | 9 |
private Class createClass(Class interfaceType, Module module)
|
75 |
{ |
|
76 | 9 |
if (!interfaceType.isInterface())
|
77 | 1 |
throw new ApplicationRuntimeException(ImplMessages.notAnInterface(interfaceType)); |
78 |
|
|
79 | 8 |
String name = ClassFabUtils.generateClassName("DefaultImpl");
|
80 |
|
|
81 | 8 |
ClassFab cf = |
82 |
_classFactory.newClass(name, Object.class, module.getClassResolver().getClassLoader());
|
|
83 |
|
|
84 | 8 |
cf.addInterface(interfaceType); |
85 |
|
|
86 | 8 |
MethodIterator mi = new MethodIterator(interfaceType);
|
87 |
|
|
88 | 8 |
while (mi.hasNext())
|
89 |
{ |
|
90 | 10 |
addMethod(cf, mi.next()); |
91 |
} |
|
92 |
|
|
93 | 8 |
if (!mi.getToString())
|
94 | 7 |
ClassFabUtils.addToStringMethod( |
95 |
cf, |
|
96 |
ImplMessages.defaultImplementationDescription(interfaceType)); |
|
97 |
|
|
98 | 8 |
return cf.createClass();
|
99 |
} |
|
100 |
|
|
101 | 10 |
private void addMethod(ClassFab cf, MethodSignature m) |
102 |
{ |
|
103 | 10 |
StringBuffer body = new StringBuffer("{ "); |
104 |
|
|
105 | 10 |
Class returnType = m.getReturnType(); |
106 |
|
|
107 | 10 |
if (returnType != void.class) |
108 |
{ |
|
109 | 5 |
body.append("return");
|
110 |
|
|
111 | 5 |
if (returnType.isPrimitive())
|
112 |
{ |
|
113 | 3 |
if (returnType == boolean.class) |
114 | 1 |
body.append(" false");
|
115 |
else
|
|
116 | 2 |
body.append(" 0");
|
117 |
} |
|
118 |
else
|
|
119 |
{ |
|
120 | 2 |
body.append(" null");
|
121 |
} |
|
122 |
|
|
123 | 5 |
body.append(";");
|
124 |
} |
|
125 |
|
|
126 | 10 |
body.append(" }");
|
127 |
|
|
128 | 10 |
cf.addMethod(Modifier.PUBLIC, m, body.toString()); |
129 |
} |
|
130 |
|
|
131 | 14 |
public void setClassFactory(ClassFactory factory) |
132 |
{ |
|
133 | 14 |
_classFactory = factory; |
134 |
} |
|
135 |
|
|
136 |
} |
|
137 |
|
|