001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: */
005: /*
006: * Licensed to the Apache Software Foundation (ASF) under one or more
007: * contributor license agreements. See the NOTICE file distributed with
008: * this work for additional information regarding copyright ownership.
009: * The ASF licenses this file to You under the Apache License, Version 2.0
010: * (the "License"); you may not use this file except in compliance with
011: * the License. You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: */
021: package org.h2.test.trace;
022:
023: import java.lang.reflect.InvocationTargetException;
024: import java.lang.reflect.Method;
025: import java.util.ArrayList;
026:
027: /**
028: * A statement in a Java-style log file.
029: */
030: class Statement {
031: private Player player;
032: private boolean assignment;
033: private boolean staticCall;
034: private String assignClass;
035: private String assignVariable;
036: private String staticCallClass;
037: private String objectName;
038: private Object object;
039: private String methodName;
040: private Arg[] args;
041: private Class returnClass;
042:
043: Statement(Player player) {
044: this .player = player;
045: }
046:
047: Object execute() throws Exception {
048: if (object == player) {
049: // there was an exception previously
050: player.log("> " + assignVariable + " not set");
051: if (assignment) {
052: player.assign(assignVariable, player);
053: }
054: return null;
055: }
056: Class clazz;
057: if (staticCall) {
058: clazz = Player.getClass(staticCallClass);
059: } else {
060: clazz = object.getClass();
061: }
062: Class[] parameterTypes = new Class[args.length];
063: Object[] parameters = new Object[args.length];
064: for (int i = 0; i < args.length; i++) {
065: Arg arg = args[i];
066: arg.execute();
067: parameterTypes[i] = arg.getValueClass();
068: parameters[i] = arg.getValue();
069: }
070: Method method = clazz.getMethod(methodName, parameterTypes);
071: returnClass = method.getReturnType();
072: try {
073: Object obj = method.invoke(object, parameters);
074: if (assignment) {
075: player.assign(assignVariable, obj);
076: }
077: return obj;
078: } catch (IllegalArgumentException e) {
079: e.printStackTrace();
080: } catch (IllegalAccessException e) {
081: e.printStackTrace();
082: } catch (InvocationTargetException e) {
083: Throwable t = e.getTargetException();
084: player.log("> " + t.toString());
085: if (assignment) {
086: player.assign(assignVariable, player);
087: }
088: }
089: return null;
090: }
091:
092: public String toString() {
093: StringBuffer buff = new StringBuffer();
094: if (assignment) {
095: buff.append(assignClass);
096: buff.append(' ');
097: buff.append(assignVariable);
098: buff.append('=');
099: }
100: if (staticCall) {
101: buff.append(staticCallClass);
102: } else {
103: buff.append(objectName);
104: }
105: buff.append('.');
106: buff.append(methodName);
107: buff.append('(');
108: for (int i = 0; args != null && i < args.length; i++) {
109: if (i > 0) {
110: buff.append(", ");
111: }
112: buff.append(args[i].toString());
113: }
114: buff.append(");");
115: return buff.toString();
116: }
117:
118: Class getReturnClass() {
119: return returnClass;
120: }
121:
122: void setAssign(String className, String variableName) {
123: this .assignment = true;
124: this .assignClass = className;
125: this .assignVariable = variableName;
126: }
127:
128: void setStaticCall(String className) {
129: this .staticCall = true;
130: this .staticCallClass = className;
131: }
132:
133: void setMethodCall(String objectName, Object object,
134: String methodName) {
135: this .objectName = objectName;
136: this .object = object;
137: this .methodName = methodName;
138: }
139:
140: public void setArgs(ArrayList list) {
141: args = new Arg[list.size()];
142: list.toArray(args);
143: }
144: }
|