Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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 lite.beans;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.WeakHashMap;
import lite.beans.util.BeansUtils;
import lite.beans.util.nls.Messages;
public class Statement {
private Object target;
private String methodName;
private Object[] arguments;
// cache used methods of specified target class to accelerate method search
private static WeakHashMap, Method[]> classMethodsCache = new WeakHashMap, Method[]>();
public Statement(Object target, String methodName, Object[] arguments) {
this.target = target;
this.methodName = methodName;
this.arguments = arguments == null ? BeansUtils.EMPTY_OBJECT_ARRAY
: arguments;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
if (target == null) {
sb.append(BeansUtils.NULL);
} else {
Class> clazz = target.getClass();
sb.append(clazz == String.class ? BeansUtils.QUOTE : BeansUtils
.idOfClass(clazz));
}
sb.append('.' + methodName + '(');
if (arguments != null) {
Class> clazz;
for (int index = 0; index < arguments.length; index++) {
if (index > 0) {
sb.append(", "); //$NON-NLS-1$
}
if (arguments[index] == null) {
sb.append(BeansUtils.NULL);
} else {
clazz = arguments[index].getClass();
sb.append(clazz == String.class ? '"' + (String) arguments[index] + '"'
: BeansUtils.idOfClass(clazz));
}
}
}
sb.append(')');
sb.append(';');
return sb.toString();
}
public String getMethodName() {
return methodName;
}
public Object[] getArguments() {
return arguments;
}
public Object getTarget() {
return target;
}
public void execute() throws Exception {
invokeMethod();
}
Object invokeMethod() throws Exception {
Object result = null;
try {
Object target = getTarget();
String methodName = getMethodName();
Object[] arguments = getArguments();
Class> targetClass = target.getClass();
if (targetClass.isArray()) {
Method method = findArrayMethod(methodName, arguments);
Object[] copy = new Object[arguments.length + 1];
copy[0] = target;
System.arraycopy(arguments, 0, copy, 1, arguments.length);
result = method.invoke(null, copy);
} else if (BeansUtils.NEWINSTANCE.equals(methodName)
&& target == Array.class) {
result = Array.newInstance((Class>) arguments[0],
((Integer) arguments[1]).intValue());
} else if (BeansUtils.NEW.equals(methodName)
|| BeansUtils.NEWINSTANCE.equals(methodName)) {
if (target instanceof Class>) {
Constructor> constructor = findConstructor(
(Class>) target, arguments);
result = constructor.newInstance(arguments);
} else {
if (BeansUtils.NEW.equals(methodName)) {
throw new NoSuchMethodException(this.toString());
}
// target class declares a public named "newInstance" method
Method method = findMethod(targetClass, methodName,
arguments, false);
result = method.invoke(target, arguments);
}
} else if (methodName.equals(BeansUtils.NEWARRAY)) {
// create a new array instance without length attribute
Class> clazz = (Class>) target, argClass;
// check the element types of array
for (int index = 0; index < arguments.length; index++) {
argClass = arguments[index] == null ? null
: arguments[index].getClass();
if (argClass != null && !clazz.isAssignableFrom(argClass)
&& !BeansUtils.isPrimitiveWrapper(argClass, clazz)) {
throw new IllegalArgumentException(
Messages.getString("beans.63")); //$NON-NLS-1$
}
}
result = Array.newInstance(clazz, arguments.length);
if (clazz.isPrimitive()) {
// Copy element according to primitive types
arrayCopy(clazz, arguments, result, arguments.length);
} else {
// Copy element of Objects
System.arraycopy(arguments, 0, result, 0, arguments.length);
}
return result;
} else if (target instanceof Class>) {
Method method = null;
try {
/*
* Try to look for a static method of class described by the
* given Class object at first process only if the class
* differs from Class itself
*/
if (target != Class.class) {
method = findMethod((Class>) target, methodName,
arguments, true);
result = method.invoke(null, arguments);
}
} catch (NoSuchMethodException e) {
// expected
}
if (method == null) {
// static method was not found
// try to invoke method of Class object
if (BeansUtils.FORNAME.equals(methodName)
&& arguments.length == 1
&& arguments[0] instanceof String) {
// special handling of Class.forName(String)
try {
result = Class.forName((String) arguments[0]);
} catch (ClassNotFoundException e2) {
result = Class.forName((String) arguments[0], true,
Thread.currentThread()
.getContextClassLoader());
}
} else {
method = findMethod(targetClass, methodName, arguments,
false);
result = method.invoke(target, arguments);
}
}
} else if (target instanceof Iterator>) {
final Iterator> iterator = (Iterator>) target;
final Method method = findMethod(targetClass, methodName,
arguments, false);
if (iterator.hasNext()) {
result = new PrivilegedAction