| 1 | package org.jtoolkit.essence.data.impl; |
| 2 | |
| 3 | import org.jetbrains.annotations.NotNull; |
| 4 | import org.jtoolkit.essence.app.pojo.Datable; |
| 5 | import org.jtoolkit.essence.app.pojo.DatableUtils; |
| 6 | import static org.jtoolkit.essence.app.pojo.DatableUtils.readObjects; |
| 7 | import static org.jtoolkit.essence.app.pojo.DatableUtils.writeObjects; |
| 8 | import org.jtoolkit.essence.concurrency.Immutable; |
| 9 | import org.jtoolkit.essence.data.Mapping; |
| 10 | import org.jtoolkit.essence.data.Transaction; |
| 11 | import org.jtoolkit.essence.data.Visitor; |
| 12 | import org.jtoolkit.essence.data.VisitorException; |
| 13 | import org.jtoolkit.essence.utils.Pair; |
| 14 | |
| 15 | import java.io.DataInput; |
| 16 | import java.io.DataOutput; |
| 17 | import java.io.IOException; |
| 18 | import java.lang.reflect.Method; |
| 19 | import java.util.Arrays; |
| 20 | import java.util.Map; |
| 21 | import 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 "AS IS" 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 |
| 45 | public 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 | } |