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 javaa.beans;
import javaa.beans.Statement;
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 org.apache.harmony.beans.internal.nls.Messages;
import jadex.common.SAccess;
public class Statement {
private static final Object[] EMPTY_ARRAY = new Object[0];
private Object target;
private String methodName;
private Object[] arguments;
// cache used methods of specified target class to accelerate method search
private static WeakHashMap, Method[]> cache = new WeakHashMap, Method[]>();
// the special method name donating constructors
static final String CONSTRUCTOR_NAME = "new"; //$NON-NLS-1$
// the special method name donating array "get"
static final String ARRAY_GET = "get"; //$NON-NLS-1$
// the special method name donating array "set"
static final String ARRAY_SET = "set"; //$NON-NLS-1$
public Statement(Object target, String methodName, Object[] arguments) {
this.target = target;
this.methodName = methodName;
if (arguments != null) {
this.arguments = arguments;
} else {
this.arguments = EMPTY_ARRAY;
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Object theTarget = getTarget();
String theMethodName = getMethodName();
Object[] theArguments = getArguments();
String targetVar = theTarget != null ? convertClassName(theTarget.getClass()) : "null"; //$NON-NLS-1$
sb.append(targetVar);
sb.append('.');
sb.append(theMethodName);
sb.append('(');
if (theArguments != null) {
for (int i = 0; i < theArguments.length; ++i) {
if (i > 0) {
sb.append(", "); //$NON-NLS-1$
}
if (theArguments[i] == null) {
sb.append("null"); //$NON-NLS-1$
} else if (theArguments[i] instanceof String) {
sb.append('"');
sb.append(theArguments[i].toString());
sb.append('"');
} else {
sb.append(convertClassName(theArguments[i].getClass()));
}
}
}
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 theTarget = getTarget();
String theMethodName = getMethodName();
Object[] theArguments = getArguments();
if (theTarget.getClass().isArray()) {
Method method = findArrayMethod(theMethodName, theArguments);
Object[] args = new Object[theArguments.length + 1];
args[0] = theTarget;
System.arraycopy(theArguments, 0, args, 1, theArguments.length);
result = method.invoke(null, args);
} else if (theMethodName.equals("newInstance") //$NON-NLS-1$
&& theTarget == Array.class) {
Class> componentType = (Class>) theArguments[0];
int length = ((Integer) theArguments[1]).intValue();
result = Array.newInstance(componentType, length);
} else if (theMethodName.equals("new") //$NON-NLS-1$
|| theMethodName.equals("newInstance")) { //$NON-NLS-1$
if (theTarget instanceof Class>) {
Constructor> constructor = findConstructor((Class>)theTarget, theArguments);
result = constructor.newInstance(theArguments);
} else {
if ("new".equals(theMethodName)) { //$NON-NLS-1$
throw new NoSuchMethodException(this.toString());
}
// target class declares a public named "newInstance" method
Method method = findMethod(theTarget.getClass(),
theMethodName, theArguments, false);
result = method.invoke(theTarget, theArguments);
}
} else if (theMethodName.equals("newArray")) {//$NON-NLS-1$
// create a new array instance without length attribute
int length = theArguments.length;
Class> clazz = (Class>) theTarget;
// check the element types of array
for (int i = 0; i < length; i++) {
boolean isNull = theArguments[i] == null;
boolean isPrimitiveWrapper = isNull ? false
: isPrimitiveWrapper(theArguments[i].getClass(),
clazz);
boolean isAssignable = isNull ? false : clazz
.isAssignableFrom(theArguments[i].getClass());
if (!isNull && !isPrimitiveWrapper && !isAssignable) {
throw new IllegalArgumentException(Messages
.getString("beans.63")); //$NON-NLS-1$
}
}
result = Array.newInstance(clazz, length);
if (clazz.isPrimitive()) {
// Copy element according to primitive types
arrayCopy(clazz, theArguments, result, length);
} else {
// Copy element of Objects
System.arraycopy(theArguments, 0, result, 0, length);
}
return result;
} else if (theTarget instanceof Class>) {
Method method = null;
boolean found = false;
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 (theTarget != Class.class) {
method = findMethod((Class>) theTarget, theMethodName, theArguments, true);
result = method.invoke(null, theArguments);
found = true;
}
} catch (NoSuchMethodException e) {
// expected
}
if (!found) {
// static method was not found
// try to invoke method of Class object
if (theMethodName.equals("forName") //$NON-NLS-1$
&& theArguments.length == 1 && theArguments[0] instanceof String) {
// special handling of Class.forName(String)
try {
result = Class.forName((String) theArguments[0]);
} catch (ClassNotFoundException e2) {
result = Class.forName((String) theArguments[0], true, Thread
.currentThread().getContextClassLoader());
}
} else {
method = findMethod(theTarget.getClass(), theMethodName, theArguments, false);
result = method.invoke(theTarget, theArguments);
}
}
} else if (theTarget instanceof Iterator>){
final Iterator> iterator = (Iterator>) theTarget;
final Method method = findMethod(theTarget.getClass(), theMethodName,
theArguments, false);
if (iterator.hasNext()) {
PrivilegedAction