org.apache.camel.util.component.ApiMethodImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of camel-core Show documentation
Show all versions of camel-core Show documentation
The Core Camel Java DSL based router
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.util.component;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Delegate class for {@link ApiMethod}.
* This class is instantiated by Enumerations for Api Proxy types.
*
* For example:
*
*
* {@code
* public enum HelloWorldMethod implements ApiMethod {
* SAYHI(String.class, "sayHi", "name", String.class);
*
* private ApiMethodImpl apiMethod;
*
* private HelloWorldMethods(Class resultType, String name, Object... args) throws IllegalArgumentException {
* this.apiMethod = new ApiMethod(HelloWorld.class, resultType, name, args);
* }
*
* // implement ApiMethod interface
* String getName() { return apiMethod.getName(); }
* Class getResultType() {return apiMethod.getResultType(); }
* List getArgNames() { return apiMethod.getArgNames(); }
* List> getArgTypes() {return apiMethod.getArgTypes(); }
* Method getMethod() { return apiMethod.getMethod(); }
* }
* }
*
*/
public final class ApiMethodImpl implements ApiMethod {
// name, result class, ordered argument names and classes, and Method to invoke
private final String name;
private final Class resultType;
private final List argNames;
private final List> argTypes;
private final Method method;
public ApiMethodImpl(Class proxyType, Class resultType, String name, Object... args) throws IllegalArgumentException {
this.name = name;
this.resultType = resultType;
if (args.length % 2 != 0) {
throw new IllegalArgumentException("Invalid parameter list, "
+ "must be of the form 'Class arg1, String arg1Name, Class arg2, String arg2Name...");
}
int nArgs = args.length / 2;
this.argNames = new ArrayList(nArgs);
this.argTypes = new ArrayList>(nArgs);
for (int i = 0; i < nArgs; i++) {
this.argTypes.add((Class) args[i * 2]);
this.argNames.add((String) args[i * 2 + 1]);
}
// find method in Proxy type
try {
this.method = proxyType.getMethod(name, argTypes.toArray(new Class[nArgs]));
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException(
String.format("Missing method %s %s", name, argTypes.toString().replace('[', '(').replace(']', ')')),
e);
}
}
@Override
public String getName() {
return name;
}
@Override
public Class getResultType() {
return resultType;
}
@Override
public List getArgNames() {
return Collections.unmodifiableList(argNames);
}
@Override
public List> getArgTypes() {
return Collections.unmodifiableList(argTypes);
}
@Override
public Method getMethod() {
return method;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("{")
.append("name=").append(name)
.append(", resultType=").append(resultType)
.append(", argNames=").append(argNames)
.append(", argTypes=").append(argTypes)
.append("}");
return builder.toString();
}
}