Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
InstanceMethodTypeConverter |
|
| 0.0;0 |
1 | /** |
|
2 | * |
|
3 | * Licensed to the Apache Software Foundation (ASF) under one or more |
|
4 | * contributor license agreements. See the NOTICE file distributed with |
|
5 | * this work for additional information regarding copyright ownership. |
|
6 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
7 | * (the "License"); you may not use this file except in compliance with |
|
8 | * the License. You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | package org.apache.camel.impl.converter; |
|
19 | ||
20 | import org.apache.camel.RuntimeCamelException; |
|
21 | import org.apache.camel.TypeConverter; |
|
22 | import org.apache.camel.util.ObjectHelper; |
|
23 | import org.apache.camel.impl.CachingInjector; |
|
24 | ||
25 | import java.lang.reflect.InvocationTargetException; |
|
26 | import java.lang.reflect.Method; |
|
27 | ||
28 | /** |
|
29 | * A {@link TypeConverter} implementation which instantiates an object |
|
30 | * so that an instance method can be used as a type converter |
|
31 | * |
|
32 | * @version $Revision: 525236 $ |
|
33 | */ |
|
34 | public class InstanceMethodTypeConverter implements TypeConverter { |
|
35 | private final CachingInjector injector; |
|
36 | private final Method method; |
|
37 | ||
38 | 884 | public InstanceMethodTypeConverter(CachingInjector injector, Method method) { |
39 | 884 | this.injector = injector; |
40 | 884 | this.method = method; |
41 | 884 | } |
42 | ||
43 | @Override |
|
44 | public String toString() { |
|
45 | 0 | return "InstanceMethodTypeConverter: " + method; |
46 | } |
|
47 | ||
48 | public synchronized <T> T convertTo(Class<T> type, Object value) { |
|
49 | 27 | Object instance = injector.newInstance(); |
50 | 27 | if (instance == null) { |
51 | 0 | throw new RuntimeCamelException("Could not instantiate aninstance of: " + type.getName()); |
52 | } |
|
53 | 27 | return (T) ObjectHelper.invokeMethod(method, instance, value); |
54 | } |
|
55 | } |