EMMA Coverage Report (generated Tue Apr 17 08:51:20 BST 2007)
[all classes][org.jtoolkit.essence.data.impl]

COVERAGE SUMMARY FOR SOURCE FILE [CallVisitor.java]

nameclass, %method, %block, %line, %
CallVisitor.java100% (2/2)67%  (6/9)69%  (152/221)71%  (29/41)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CallVisitor$1100% (1/1)50%  (1/2)21%  (3/14)25%  (1/4)
convert (DataInput): CallVisitor 0%   (0/1)0%   (0/11)0%   (0/3)
CallVisitor$1 (): void 100% (1/1)100% (3/3)100% (1/1)
     
class CallVisitor100% (1/1)71%  (5/7)72%  (149/207)76%  (28/37)
CallVisitor (DataInput): void 0%   (0/1)0%   (0/13)0%   (0/4)
writeData (DataOutput): void 0%   (0/1)0%   (0/9)0%   (0/3)
CallVisitor (String, Object []): void 100% (1/1)50%  (13/26)83%  (5/6)
searchForMethod (Class): Method 100% (1/1)64%  (41/64)86%  (6/7)
<static initializer> 100% (1/1)100% (13/13)100% (3/3)
toString (): String 100% (1/1)100% (14/14)100% (1/1)
visit (Object): Object 100% (1/1)100% (68/68)100% (13/13)

1package org.jtoolkit.essence.data.impl;
2 
3import org.jetbrains.annotations.NotNull;
4import org.jtoolkit.essence.app.pojo.Datable;
5import org.jtoolkit.essence.app.pojo.DatableUtils;
6import static org.jtoolkit.essence.app.pojo.DatableUtils.readObjects;
7import static org.jtoolkit.essence.app.pojo.DatableUtils.writeObjects;
8import org.jtoolkit.essence.concurrency.Immutable;
9import org.jtoolkit.essence.data.Mapping;
10import org.jtoolkit.essence.data.Transaction;
11import org.jtoolkit.essence.data.Visitor;
12import org.jtoolkit.essence.data.VisitorException;
13import org.jtoolkit.essence.utils.Pair;
14 
15import java.io.DataInput;
16import java.io.DataOutput;
17import java.io.IOException;
18import java.lang.reflect.Method;
19import java.util.Arrays;
20import java.util.Map;
21import java.util.concurrent.ConcurrentHashMap;
22 
23/*
24   Copyright 2006 Peter Lawrey
25 
26   Licensed under the Apache License, Version 2.0 (the "License");
27   you may not use this file except in compliance with the License.
28   You may obtain a copy of the License at
29 
30       http://www.apache.org/licenses/LICENSE-2.0
31 
32   Unless required by applicable law or agreed to in writing, software
33   distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
34   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35   See the License for the specific language governing permissions and
36   limitations under the License.
37*/
38 
39/**
40 * A Visitor which performs method call.
41 *
42 * @author Peter Lawrey
43 */
44@Immutable
45public class CallVisitor<V, R> implements Visitor<V, R>, Datable {
46    private static final Map<Pair<Class, String>, Method> METHODS = new ConcurrentHashMap<Pair<Class, String>, Method>(256, 0.5f, 16);
47    private static final int MAX_ARGS = 256;
48 
49    public final String method;
50    public final Object[] args;
51 
52    public CallVisitor(@NotNull String method, Object... args) {
53        this.method = method;
54        this.args = args;
55        if (args.length > MAX_ARGS)
56            throw new IllegalArgumentException("More than " + MAX_ARGS + " not supported, was " + args.length);
57    }
58 
59    public R visit(@NotNull V visited) throws VisitorException {
60        Transaction t = Transaction.start("visit");
61        try {
62            Class<? extends Object> vClass = visited.getClass();
63            Pair<Class, String> key = new Pair<Class, String>(vClass, method + '-' + args.length);
64            Method m2 = METHODS.get(key);
65            if (m2 == null) {
66                METHODS.put(key, m2 = searchForMethod(vClass));
67                m2.setAccessible(true);
68            }
69            //noinspection unchecked
70            R ret = (R) m2.invoke(visited, args);
71            t.commit();
72            return ret;
73        } catch (Exception e) {
74            throw new VisitorException(e);
75        } finally {
76            t.complete();
77        }
78    }
79 
80    private Method searchForMethod(Class<? extends Object> vClass) throws NoSuchMethodException {
81        while (vClass != null) {
82            for (Method m : vClass.getDeclaredMethods()) {
83                if (m.getName().equals(method) && m.getParameterTypes().length == args.length) {
84                    m.setAccessible(true);
85                    return m;
86                }
87            }
88            vClass = vClass.getSuperclass();
89        }
90        throw new NoSuchMethodException(vClass + ": Unable to find method " + method + " with " + args.length + " arguments.");
91    }
92 
93    public CallVisitor(@NotNull DataInput in) throws IOException {
94        method = in.readUTF();
95        args = readObjects(in, 0, MAX_ARGS);
96    }
97 
98    static {
99        DatableUtils.registerBuilder(CallVisitor.class, new Mapping<DataInput, CallVisitor>() {
100            public CallVisitor convert(DataInput in) {
101                try {
102                    return new CallVisitor(in);
103                } catch (IOException e) {
104                    throw new IllegalArgumentException(e);
105                }
106            }
107        });
108    }
109 
110    public void writeData(@NotNull DataOutput out) throws IOException {
111        out.writeUTF(method);
112        writeObjects(out, args);
113    }
114 
115    public String toString() {
116        return "CallVisitor " + method + Arrays.asList(args);
117    }
118}

[all classes][org.jtoolkit.essence.data.impl]
EMMA 2.0.5312 (C) Vladimir Roubtsov