PageRenderTime 53ms CodeModel.GetById 42ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/cunningt/core-1
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 */
 22package org.jboss.forge.dev.java;
 23
 24import java.io.FileNotFoundException;
 25import java.io.InputStream;
 26import java.util.Arrays;
 27import java.util.List;
 28import java.util.Map.Entry;
 29
 30import javax.enterprise.event.Event;
 31import javax.inject.Inject;
 32
 33import org.jboss.forge.parser.JavaParser;
 34import org.jboss.forge.parser.java.FieldHolder;
 35import org.jboss.forge.parser.java.Import;
 36import org.jboss.forge.parser.java.JavaClass;
 37import org.jboss.forge.parser.java.JavaSource;
 38import org.jboss.forge.parser.java.Method;
 39import org.jboss.forge.parser.java.MethodHolder;
 40import org.jboss.forge.parser.java.SyntaxError;
 41import org.jboss.forge.parser.java.util.Strings;
 42import org.jboss.forge.project.Project;
 43import org.jboss.forge.project.facets.JavaSourceFacet;
 44import org.jboss.forge.resources.java.JavaResource;
 45import org.jboss.forge.shell.PromptType;
 46import org.jboss.forge.shell.ShellColor;
 47import org.jboss.forge.shell.ShellPrintWriter;
 48import org.jboss.forge.shell.ShellPrompt;
 49import org.jboss.forge.shell.events.PickupResource;
 50import org.jboss.forge.shell.plugins.Alias;
 51import org.jboss.forge.shell.plugins.Command;
 52import org.jboss.forge.shell.plugins.Current;
 53import org.jboss.forge.shell.plugins.DefaultCommand;
 54import org.jboss.forge.shell.plugins.Option;
 55import org.jboss.forge.shell.plugins.PipeIn;
 56import org.jboss.forge.shell.plugins.PipeOut;
 57import org.jboss.forge.shell.plugins.Plugin;
 58import org.jboss.forge.shell.plugins.RequiresFacet;
 59import org.jboss.forge.shell.plugins.RequiresResource;
 60import org.jboss.forge.shell.util.JavaColorizer;
 61
 62/**
 63 * @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
 64 * 
 65 */
 66@Alias("java")
 67@RequiresFacet(JavaSourceFacet.class)
 68public class JavaPlugin implements Plugin
 69{
 70   @Inject
 71   @Current
 72   private JavaResource resource;
 73
 74   @Inject
 75   private Project project;
 76
 77   @Inject
 78   private ShellPrompt prompt;
 79
 80   @Inject
 81   private ShellPrintWriter writer;
 82
 83   @Inject
 84   private Event<PickupResource> pickUp;
 85
 86   @DefaultCommand(help = "Prints all Java system property information.")
 87   public void info(final PipeOut out)
 88   {
 89      for (Entry<Object, Object> entry : System.getProperties().entrySet())
 90      {
 91         if (entry.getKey().toString().startsWith("java"))
 92         {
 93            out.print(ShellColor.BOLD, entry.getKey().toString() + ": ");
 94            out.println(entry.getValue().toString());
 95         }
 96      }
 97   }
 98
 99   @Command("new-class")
100   public void newClass(
101            @PipeIn final InputStream in,
102            @Option(required = false,
103                     help = "the package in which to build this Class",
104                     description = "source package",
105                     type = PromptType.JAVA_PACKAGE,
106                     name = "package") final String pckg,
107            @Option(required = false,
108                     help = "the class definition: surround with quotes",
109                     description = "class definition") final String... def) throws FileNotFoundException
110   {
111
112      JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);
113
114      JavaClass jc = null;
115      if (def != null)
116      {
117         String classDef = Strings.join(Arrays.asList(def), " ");
118         jc = JavaParser.parse(JavaClass.class, classDef);
119      }
120      else if (in != null)
121      {
122         jc = JavaParser.parse(JavaClass.class, in);
123      }
124      else
125      {
126         throw new RuntimeException("arguments required");
127      }
128
129      if (pckg != null)
130      {
131         jc.setPackage(pckg);
132      }
133
134      if (!jc.hasSyntaxErrors())
135      {
136         java.saveJavaSource(jc);
137      }
138      else
139      {
140         writer.println(ShellColor.RED, "Syntax Errors:");
141         for (SyntaxError error : jc.getSyntaxErrors())
142         {
143            writer.println(error.toString());
144         }
145         writer.println();
146
147         if (prompt.promptBoolean("Your class has syntax errors, create anyway?", true))
148         {
149            java.saveJavaSource(jc);
150         }
151      }
152
153      pickUp.fire(new PickupResource(java.getJavaResource(jc)));
154   }
155
156   @Command("list-imports")
157   @RequiresResource(JavaResource.class)
158   public void listImports(
159            final PipeOut out) throws FileNotFoundException
160   {
161      List<Import> imports = resource.getJavaSource().getImports();
162      for (Import i : imports)
163      {
164         String str = "import " + (i.isStatic() ? "static " : "") + i.getQualifiedName() + ";";
165         str = JavaColorizer.format(out, str);
166         out.println(str);
167      }
168   }
169
170   @Command("new-field")
171   @RequiresResource(JavaResource.class)
172   public void newField(
173            @PipeIn final String in,
174            final PipeOut out,
175            @Option(required = false,
176                     help = "the field definition: surround with single quotes",
177                     description = "field definition") final String... def) throws FileNotFoundException
178   {
179      JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);
180
181      String fieldDef = null;
182      if (def != null)
183      {
184         fieldDef = Strings.join(Arrays.asList(def), " ");
185      }
186      else if (in != null)
187      {
188         fieldDef = in;
189      }
190      else
191      {
192         throw new RuntimeException("arguments required");
193      }
194
195      JavaSource<?> source = resource.getJavaSource();
196      if (source instanceof FieldHolder)
197      {
198         FieldHolder<?> clazz = ((FieldHolder<?>) source);
199
200         String name = JavaParser.parse(JavaClass.class, "public class Temp{}").addField(fieldDef).getName();
201         if (clazz.hasField(name))
202         {
203            throw new IllegalStateException("Field named [" + name + "] already exists.");
204         }
205
206         clazz.addField(fieldDef);
207         java.saveJavaSource(source);
208      }
209   }
210
211   @Command("new-method")
212   @RequiresResource(JavaResource.class)
213   public void newMethod(
214            @PipeIn final String in,
215            final PipeOut out,
216            @Option(required = false,
217                     help = "the method definition: surround with single quotes",
218                     description = "method definition") final String... def) throws FileNotFoundException
219   {
220      JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);
221
222      String methodDef = null;
223      if (def != null)
224      {
225         methodDef = Strings.join(Arrays.asList(def), " ");
226      }
227      else if (in != null)
228      {
229         methodDef = in;
230      }
231      else
232      {
233         throw new RuntimeException("arguments required");
234      }
235
236      JavaSource<?> source = resource.getJavaSource();
237      if (source instanceof MethodHolder)
238      {
239         MethodHolder<?> clazz = ((MethodHolder<?>) source);
240
241         Method<JavaClass> method = JavaParser.parse(JavaClass.class, "public class Temp{}").addMethod(methodDef);
242         if (clazz.hasMethodSignature(method))
243         {
244            throw new IllegalStateException("Method with signature [" + method.toSignature()
245                     + "] already exists.");
246         }
247
248         clazz.addMethod(methodDef);
249         java.saveJavaSource(source);
250      }
251   }
252}