|
|||||||||||||||||||
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 | |||||||||||||||
ProxyBuilder.java | 100% | 100% | 100% | 100% |
|
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.impl;
|
|
16 |
|
|
17 |
import java.lang.reflect.Method;
|
|
18 |
import java.lang.reflect.Modifier;
|
|
19 |
|
|
20 |
import org.apache.hivemind.internal.Module;
|
|
21 |
import org.apache.hivemind.internal.ServicePoint;
|
|
22 |
import org.apache.hivemind.service.BodyBuilder;
|
|
23 |
import org.apache.hivemind.service.ClassFab;
|
|
24 |
import org.apache.hivemind.service.ClassFabUtils;
|
|
25 |
import org.apache.hivemind.service.ClassFactory;
|
|
26 |
import org.apache.hivemind.service.MethodSignature;
|
|
27 |
|
|
28 |
/**
|
|
29 |
* Class used to assist service extension points in creating proxies.
|
|
30 |
*
|
|
31 |
* @author Howard Lewis Ship
|
|
32 |
*/
|
|
33 |
public final class ProxyBuilder |
|
34 |
{ |
|
35 |
private ServicePoint _point;
|
|
36 |
private Class _serviceInterface;
|
|
37 |
private ClassFab _classFab;
|
|
38 |
private String _type;
|
|
39 |
|
|
40 |
/**
|
|
41 |
* Constructs a new builder. The type will be incorporated
|
|
42 |
* into the class name and the <code>toString()</code> method.
|
|
43 |
* The service extension point is used to identify the service interface
|
|
44 |
* and service id.
|
|
45 |
*/
|
|
46 | 1341 |
public ProxyBuilder(String type, ServicePoint point)
|
47 |
{ |
|
48 | 1341 |
_point = point; |
49 | 1341 |
_type = type; |
50 | 1341 |
_serviceInterface = point.getServiceInterface(); |
51 |
|
|
52 | 1341 |
Module module = point.getModule(); |
53 | 1341 |
ClassFactory factory = |
54 |
(ClassFactory) module.getService("hivemind.ClassFactory", ClassFactory.class); |
|
55 |
|
|
56 | 1341 |
_classFab = factory.newClass(ClassFabUtils.generateClassName(type), Object.class, module);
|
57 |
|
|
58 | 1341 |
_classFab.addInterface(_serviceInterface); |
59 |
} |
|
60 |
|
|
61 | 1341 |
public ClassFab getClassFab()
|
62 |
{ |
|
63 | 1341 |
return _classFab;
|
64 |
} |
|
65 |
|
|
66 |
/**
|
|
67 |
* Creates the service methods for the class.
|
|
68 |
* @param indirection the name of a variable, or a method invocation snippet,
|
|
69 |
* used to redirect the invocation on the proxy to the actual service implementation.
|
|
70 |
*/
|
|
71 | 1341 |
public void addServiceMethods(String indirection) |
72 |
{ |
|
73 | 1341 |
BodyBuilder builder = new BodyBuilder();
|
74 | 1341 |
boolean toString = false; |
75 |
|
|
76 | 1341 |
Method[] methods = _serviceInterface.getMethods(); |
77 | 1341 |
for (int i = 0; i < methods.length; i++) |
78 |
{ |
|
79 | 1556 |
Method m = methods[i]; |
80 |
|
|
81 | 1556 |
builder.clear(); |
82 | 1556 |
builder.begin(); |
83 | 1556 |
builder.add("return ($r) ");
|
84 | 1556 |
builder.add(indirection); |
85 | 1556 |
builder.add(".");
|
86 | 1556 |
builder.add(m.getName()); |
87 | 1556 |
builder.addln("($$);");
|
88 | 1556 |
builder.end(); |
89 |
|
|
90 | 1556 |
_classFab.addMethod(Modifier.PUBLIC, new MethodSignature(m), builder.toString());
|
91 |
|
|
92 | 1556 |
toString |= ClassFabUtils.isToString(m); |
93 |
} |
|
94 |
|
|
95 | 1341 |
if (!toString)
|
96 | 1339 |
ClassFabUtils.addToStringMethod( |
97 |
_classFab, |
|
98 |
"<"
|
|
99 |
+ _type |
|
100 |
+ " for "
|
|
101 |
+ _point.getExtensionPointId() |
|
102 |
+ "("
|
|
103 |
+ _serviceInterface.getName() |
|
104 |
+ ")>");
|
|
105 |
} |
|
106 |
} |
|
107 |
|
|