PageRenderTime 63ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/dev-plugins/src/main/java/org/jboss/forge/dev/java/JavaPlugin.java

https://github.com/pmuir/forge-core
Java | 252 lines | 200 code | 27 blank | 25 comment | 27 complexity | ffec267d6e7b21bb5a0d54e0c8ba903a MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc., and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software 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 GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.forge.dev.java;
  23. import java.io.FileNotFoundException;
  24. import java.io.InputStream;
  25. import java.util.Arrays;
  26. import java.util.List;
  27. import java.util.Map.Entry;
  28. import javax.enterprise.event.Event;
  29. import javax.inject.Inject;
  30. import org.jboss.forge.parser.JavaParser;
  31. import org.jboss.forge.parser.java.FieldHolder;
  32. import org.jboss.forge.parser.java.Import;
  33. import org.jboss.forge.parser.java.JavaClass;
  34. import org.jboss.forge.parser.java.JavaSource;
  35. import org.jboss.forge.parser.java.Method;
  36. import org.jboss.forge.parser.java.MethodHolder;
  37. import org.jboss.forge.parser.java.SyntaxError;
  38. import org.jboss.forge.parser.java.util.Strings;
  39. import org.jboss.forge.project.Project;
  40. import org.jboss.forge.project.facets.JavaSourceFacet;
  41. import org.jboss.forge.resources.java.JavaResource;
  42. import org.jboss.forge.shell.PromptType;
  43. import org.jboss.forge.shell.ShellColor;
  44. import org.jboss.forge.shell.ShellPrintWriter;
  45. import org.jboss.forge.shell.ShellPrompt;
  46. import org.jboss.forge.shell.events.PickupResource;
  47. import org.jboss.forge.shell.plugins.Alias;
  48. import org.jboss.forge.shell.plugins.Command;
  49. import org.jboss.forge.shell.plugins.Current;
  50. import org.jboss.forge.shell.plugins.DefaultCommand;
  51. import org.jboss.forge.shell.plugins.Option;
  52. import org.jboss.forge.shell.plugins.PipeIn;
  53. import org.jboss.forge.shell.plugins.PipeOut;
  54. import org.jboss.forge.shell.plugins.Plugin;
  55. import org.jboss.forge.shell.plugins.RequiresFacet;
  56. import org.jboss.forge.shell.plugins.RequiresResource;
  57. import org.jboss.forge.shell.util.JavaColorizer;
  58. /**
  59. * @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
  60. *
  61. */
  62. @Alias("java")
  63. @RequiresFacet(JavaSourceFacet.class)
  64. public class JavaPlugin implements Plugin
  65. {
  66. @Inject
  67. @Current
  68. private JavaResource resource;
  69. @Inject
  70. private Project project;
  71. @Inject
  72. private ShellPrompt prompt;
  73. @Inject
  74. private ShellPrintWriter writer;
  75. @Inject
  76. private Event<PickupResource> pickUp;
  77. @DefaultCommand(help = "Prints all Java system property information.")
  78. public void info(final PipeOut out)
  79. {
  80. for (Entry<Object, Object> entry : System.getProperties().entrySet())
  81. {
  82. if (entry.getKey().toString().startsWith("java"))
  83. {
  84. out.print(ShellColor.BOLD, entry.getKey().toString() + ": ");
  85. out.println(entry.getValue().toString());
  86. }
  87. }
  88. }
  89. @Command("new-class")
  90. public void newClass(
  91. @PipeIn final InputStream in,
  92. @Option(required = false,
  93. help = "the package in which to build this Class",
  94. description = "source package",
  95. type = PromptType.JAVA_PACKAGE,
  96. name = "package") final String pckg,
  97. @Option(required = false,
  98. help = "the class definition: surround with quotes",
  99. description = "class definition") final String... def) throws FileNotFoundException
  100. {
  101. JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);
  102. JavaClass jc = null;
  103. if (def != null)
  104. {
  105. String classDef = Strings.join(Arrays.asList(def), " ");
  106. jc = JavaParser.parse(JavaClass.class, classDef);
  107. }
  108. else if (in != null)
  109. {
  110. jc = JavaParser.parse(JavaClass.class, in);
  111. }
  112. else
  113. {
  114. throw new RuntimeException("arguments required");
  115. }
  116. if (pckg != null)
  117. {
  118. jc.setPackage(pckg);
  119. }
  120. if (!jc.hasSyntaxErrors())
  121. {
  122. java.saveJavaSource(jc);
  123. }
  124. else
  125. {
  126. writer.println(ShellColor.RED, "Syntax Errors:");
  127. for (SyntaxError error : jc.getSyntaxErrors())
  128. {
  129. writer.println(error.toString());
  130. }
  131. writer.println();
  132. if (prompt.promptBoolean("Your class has syntax errors, create anyway?", true))
  133. {
  134. java.saveJavaSource(jc);
  135. }
  136. }
  137. pickUp.fire(new PickupResource(java.getJavaResource(jc)));
  138. }
  139. @Command("list-imports")
  140. @RequiresResource(JavaResource.class)
  141. public void listImports(
  142. final PipeOut out) throws FileNotFoundException
  143. {
  144. List<Import> imports = resource.getJavaSource().getImports();
  145. for (Import i : imports)
  146. {
  147. String str = "import " + (i.isStatic() ? "static " : "") + i.getQualifiedName() + ";";
  148. str = JavaColorizer.format(out, str);
  149. out.println(str);
  150. }
  151. }
  152. @Command("new-field")
  153. @RequiresResource(JavaResource.class)
  154. public void newField(
  155. @PipeIn final String in,
  156. final PipeOut out,
  157. @Option(required = false,
  158. help = "the field definition: surround with single quotes",
  159. description = "field definition") final String... def) throws FileNotFoundException
  160. {
  161. JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);
  162. String fieldDef = null;
  163. if (def != null)
  164. {
  165. fieldDef = Strings.join(Arrays.asList(def), " ");
  166. }
  167. else if (in != null)
  168. {
  169. fieldDef = in;
  170. }
  171. else
  172. {
  173. throw new RuntimeException("arguments required");
  174. }
  175. JavaSource<?> source = resource.getJavaSource();
  176. if (source instanceof FieldHolder)
  177. {
  178. FieldHolder<?> clazz = ((FieldHolder<?>) source);
  179. String name = JavaParser.parse(JavaClass.class, "public class Temp{}").addField(fieldDef).getName();
  180. if (clazz.hasField(name))
  181. {
  182. throw new IllegalStateException("Field named [" + name + "] already exists.");
  183. }
  184. clazz.addField(fieldDef);
  185. java.saveJavaSource(source);
  186. }
  187. }
  188. @Command("new-method")
  189. @RequiresResource(JavaResource.class)
  190. public void newMethod(
  191. @PipeIn final String in,
  192. final PipeOut out,
  193. @Option(required = false,
  194. help = "the method definition: surround with single quotes",
  195. description = "method definition") final String... def) throws FileNotFoundException
  196. {
  197. JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);
  198. String methodDef = null;
  199. if (def != null)
  200. {
  201. methodDef = Strings.join(Arrays.asList(def), " ");
  202. }
  203. else if (in != null)
  204. {
  205. methodDef = in;
  206. }
  207. else
  208. {
  209. throw new RuntimeException("arguments required");
  210. }
  211. JavaSource<?> source = resource.getJavaSource();
  212. if (source instanceof MethodHolder)
  213. {
  214. MethodHolder<?> clazz = ((MethodHolder<?>) source);
  215. Method<JavaClass> method = JavaParser.parse(JavaClass.class, "public class Temp{}").addMethod(methodDef);
  216. if (clazz.hasMethodSignature(method))
  217. {
  218. throw new IllegalStateException("Method with signature [" + method.toSignature()
  219. + "] already exists.");
  220. }
  221. clazz.addMethod(methodDef);
  222. java.saveJavaSource(source);
  223. }
  224. }
  225. }