PageRenderTime 100ms CodeModel.GetById 88ms app.highlight 6ms RepoModel.GetById 5ms app.codeStats 0ms

/bundles/plugins-trunk/XInsert/src/RunJavaCommand.java

#
Java | 152 lines | 58 code | 10 blank | 84 comment | 11 complexity | a9ed7e641e201648fd8543b626b2223e MD5 | raw file
  1/*
  2 *
  3 * RunJavaCommand.java
  4 * Copyright (C) 2001 Dominic Stolerman
  5 * dstolerman@jedit.org
  6 *
  7 * This program is free software; you can redistribute it and/or
  8 * modify it under the terms of the GNU General Public License
  9 * as published by the Free Software Foundation; either version 2
 10 * of the License, or any later version.
 11 *
 12 * This program is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 * GNU General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU General Public License
 18 * along with this program; if not, write to the Free Software
 19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 20 */
 21
 22import org.gjt.sp.jedit.View;
 23import java.lang.reflect.*;
 24import org.gjt.sp.util.Log;
 25/**
 26 *  
 27 *
 28 * @author     Dominic Stolerman
 29 */
 30public class RunJavaCommand extends Object implements Command {
 31
 32	private final String command;
 33
 34
 35	public RunJavaCommand(String command) {
 36		this.command = command;
 37	}
 38
 39	public void run(ScriptContext sc) {
 40		View parent = sc.getView();
 41		XTreeNode node = sc.getNode();
 42		boolean stringArg = false;
 43		String arg = "";
 44		String cleanedCmd = command.substring(1, command.length() - 1).trim();
 45		int bracket = cleanedCmd.indexOf("(");
 46		if(bracket == -1) {
 47			XScripter.doError(command, "\"(\" expected");
 48			return;
 49		}
 50
 51		// find the LAST "." before bracket - this is the class
 52		int classEnds = cleanedCmd.lastIndexOf(".", bracket);
 53		String clazzName = cleanedCmd.substring(0, classEnds);
 54		String methodName = cleanedCmd.substring(classEnds + 1, bracket);
 55		if(cleanedCmd.charAt(bracket + 1) != ')') {
 56			stringArg = true;
 57			String _arg = cleanedCmd.substring(bracket + 1, cleanedCmd.length());
 58			if(_arg.startsWith("$")) {
 59				arg = XScripter.getSubstituteFor(parent, _arg.substring(1), node);
 60			}
 61			else {
 62				arg = _arg;
 63			}
 64		}
 65		try {
 66			Class clazz = Class.forName(clazzName);
 67			Method method = null;
 68			System.out.println("ClassName=" + clazzName + "/" + clazz.getName() + " methodName=" + methodName);
 69			
 70			Object[] args = null; //Arguments for method
 71			if(stringArg) {
 72				method = clazz.getMethod(methodName, new Class[] {String.class});
 73				//method = clazz.getMethod(methodName, new Class[] {Object.class, String.class});
 74				args = new Object[1];
 75				args[0] = arg;
 76			}
 77			else {
 78				//method = clazz.getMethod(methodName, new Class[] {Object.class});
 79				method = clazz.getMethod(methodName, null);
 80			}
 81			
 82			Object obj = method.invoke(null, args);
 83			if(obj == null) {
 84				return;
 85			}
 86			else {
 87				InsertTextCommand.insertText(obj.toString(), sc);
 88			}
 89			/*
 90			Object[] args = null;
 91			Method[] methods = clazz.getMethods();
 92			/*
 93			 * Debugging code
 94			 * for (int i = 0; i < methods.length; i++) {
 95			 * String methodString = methods[i].getName();
 96			 * String returnString = methods[i].getReturnType().getName();
 97			 * Log.log(Log.DEBUG, XScripter.class, "Method: " + methodString + " Return Type: " + returnString);
 98			 * Class[] parameterTypes = methods[i].getParameterTypes();
 99			 * Log.log(Log.DEBUG, XScripter.class, "   Parameter Types");
100			 * for (int k = 0; k < parameterTypes.length; k ++) {
101			 * String parameterString = parameterTypes[k].getName();
102			 * Log.log(Log.DEBUG, XScripter.class," " + parameterString);
103			 * }
104			 * System.out.println();
105			 * }
106			 
107			for(int i = 0; i < methods.length; i++) {
108				if(methods[i].getName().equals(methodName)) {
109					Class[] parameterTypes = methods[i].getParameterTypes();
110					if(stringArg && parameterTypes.length == 2) {
111						if(parameterTypes[0].getName().equals(ScriptContext.class.getName()) && 
112								parameterTypes[1].getName().equals(String.class.getName())) {
113							method = methods[i];
114							args = new Object[]{sc, arg};
115							break;
116						}
117					}
118					else if(!stringArg && parameterTypes.length == 1) {
119						if(parameterTypes[0].getName().equals(ScriptContext.class.getName())) {
120							method = methods[i];
121							args = new Object[]{sc};
122							break;
123						}
124					}
125				}
126			}
127			if(method != null) {
128				for(int i = 0; i < args.length; i++) {
129					Log.log(Log.DEBUG, this, args[i].getClass().getName());
130				}
131				Object obj = method.invoke(null, args);
132				if(obj == null) {
133					return;
134				}
135				else {
136					InsertTextCommand.insertText(obj.toString(), sc);
137				}
138			}
139			else {
140				XScripter.doError(command, "Method not found");
141				return;
142			}
143			
144*/
145		}
146		catch(Exception e) {
147			XScripter.doError(command, e);
148		}
149	}
150}
151
152