PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms 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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. import org.gjt.sp.jedit.View;
  22. import java.lang.reflect.*;
  23. import org.gjt.sp.util.Log;
  24. /**
  25. *
  26. *
  27. * @author Dominic Stolerman
  28. */
  29. public class RunJavaCommand extends Object implements Command {
  30. private final String command;
  31. public RunJavaCommand(String command) {
  32. this.command = command;
  33. }
  34. public void run(ScriptContext sc) {
  35. View parent = sc.getView();
  36. XTreeNode node = sc.getNode();
  37. boolean stringArg = false;
  38. String arg = "";
  39. String cleanedCmd = command.substring(1, command.length() - 1).trim();
  40. int bracket = cleanedCmd.indexOf("(");
  41. if(bracket == -1) {
  42. XScripter.doError(command, "\"(\" expected");
  43. return;
  44. }
  45. // find the LAST "." before bracket - this is the class
  46. int classEnds = cleanedCmd.lastIndexOf(".", bracket);
  47. String clazzName = cleanedCmd.substring(0, classEnds);
  48. String methodName = cleanedCmd.substring(classEnds + 1, bracket);
  49. if(cleanedCmd.charAt(bracket + 1) != ')') {
  50. stringArg = true;
  51. String _arg = cleanedCmd.substring(bracket + 1, cleanedCmd.length());
  52. if(_arg.startsWith("$")) {
  53. arg = XScripter.getSubstituteFor(parent, _arg.substring(1), node);
  54. }
  55. else {
  56. arg = _arg;
  57. }
  58. }
  59. try {
  60. Class clazz = Class.forName(clazzName);
  61. Method method = null;
  62. System.out.println("ClassName=" + clazzName + "/" + clazz.getName() + " methodName=" + methodName);
  63. Object[] args = null; //Arguments for method
  64. if(stringArg) {
  65. method = clazz.getMethod(methodName, new Class[] {String.class});
  66. //method = clazz.getMethod(methodName, new Class[] {Object.class, String.class});
  67. args = new Object[1];
  68. args[0] = arg;
  69. }
  70. else {
  71. //method = clazz.getMethod(methodName, new Class[] {Object.class});
  72. method = clazz.getMethod(methodName, null);
  73. }
  74. Object obj = method.invoke(null, args);
  75. if(obj == null) {
  76. return;
  77. }
  78. else {
  79. InsertTextCommand.insertText(obj.toString(), sc);
  80. }
  81. /*
  82. Object[] args = null;
  83. Method[] methods = clazz.getMethods();
  84. /*
  85. * Debugging code
  86. * for (int i = 0; i < methods.length; i++) {
  87. * String methodString = methods[i].getName();
  88. * String returnString = methods[i].getReturnType().getName();
  89. * Log.log(Log.DEBUG, XScripter.class, "Method: " + methodString + " Return Type: " + returnString);
  90. * Class[] parameterTypes = methods[i].getParameterTypes();
  91. * Log.log(Log.DEBUG, XScripter.class, " Parameter Types");
  92. * for (int k = 0; k < parameterTypes.length; k ++) {
  93. * String parameterString = parameterTypes[k].getName();
  94. * Log.log(Log.DEBUG, XScripter.class," " + parameterString);
  95. * }
  96. * System.out.println();
  97. * }
  98. for(int i = 0; i < methods.length; i++) {
  99. if(methods[i].getName().equals(methodName)) {
  100. Class[] parameterTypes = methods[i].getParameterTypes();
  101. if(stringArg && parameterTypes.length == 2) {
  102. if(parameterTypes[0].getName().equals(ScriptContext.class.getName()) &&
  103. parameterTypes[1].getName().equals(String.class.getName())) {
  104. method = methods[i];
  105. args = new Object[]{sc, arg};
  106. break;
  107. }
  108. }
  109. else if(!stringArg && parameterTypes.length == 1) {
  110. if(parameterTypes[0].getName().equals(ScriptContext.class.getName())) {
  111. method = methods[i];
  112. args = new Object[]{sc};
  113. break;
  114. }
  115. }
  116. }
  117. }
  118. if(method != null) {
  119. for(int i = 0; i < args.length; i++) {
  120. Log.log(Log.DEBUG, this, args[i].getClass().getName());
  121. }
  122. Object obj = method.invoke(null, args);
  123. if(obj == null) {
  124. return;
  125. }
  126. else {
  127. InsertTextCommand.insertText(obj.toString(), sc);
  128. }
  129. }
  130. else {
  131. XScripter.doError(command, "Method not found");
  132. return;
  133. }
  134. */
  135. }
  136. catch(Exception e) {
  137. XScripter.doError(command, e);
  138. }
  139. }
  140. }