PageRenderTime 1720ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/shell/src/main/java/org/jboss/forge/shell/plugins/builtin/LsJavaPlugin.java

https://github.com/includeeasy/core
Java | 170 lines | 124 code | 20 blank | 26 comment | 14 complexity | b4ccf9de80eb64d22cf6306d74956454 MD5 | raw file
  1. /*
  2. * JBoss, by Red Hat.
  3. * Copyright 2010, 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.shell.plugins.builtin;
  23. import static org.jboss.forge.shell.util.GeneralUtils.printOutColumns;
  24. import java.io.FileNotFoundException;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. import javax.inject.Inject;
  28. import org.jboss.forge.parser.java.Field;
  29. import org.jboss.forge.parser.java.JavaClass;
  30. import org.jboss.forge.parser.java.JavaSource;
  31. import org.jboss.forge.parser.java.Method;
  32. import org.jboss.forge.parser.java.Parameter;
  33. import org.jboss.forge.resources.Resource;
  34. import org.jboss.forge.resources.java.JavaFieldResource;
  35. import org.jboss.forge.resources.java.JavaMemberResource;
  36. import org.jboss.forge.resources.java.JavaMethodResource;
  37. import org.jboss.forge.resources.java.JavaResource;
  38. import org.jboss.forge.shell.Shell;
  39. import org.jboss.forge.shell.ShellColor;
  40. import org.jboss.forge.shell.plugins.Alias;
  41. import org.jboss.forge.shell.plugins.DefaultCommand;
  42. import org.jboss.forge.shell.plugins.Help;
  43. import org.jboss.forge.shell.plugins.Option;
  44. import org.jboss.forge.shell.plugins.PipeOut;
  45. import org.jboss.forge.shell.plugins.Plugin;
  46. import org.jboss.forge.shell.plugins.RequiresResource;
  47. import org.jboss.forge.shell.plugins.Topic;
  48. import org.jboss.forge.shell.util.GeneralUtils;
  49. import org.jboss.forge.shell.util.JavaColorizer;
  50. /**
  51. * @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
  52. * @author Mike Brock
  53. */
  54. @Alias("ls")
  55. @RequiresResource({ JavaResource.class, JavaMethodResource.class, JavaFieldResource.class })
  56. @Topic("File & Resources")
  57. @Help("Prints the contents current Java file")
  58. public class LsJavaPlugin implements Plugin
  59. {
  60. private static final String DELIM = "::";
  61. @Inject
  62. private Shell shell;
  63. @DefaultCommand
  64. public void run(
  65. @Option(description = "path", defaultValue = ".") final Resource<?>[] paths,
  66. @Option(flagOnly = true, name = "all", shortName = "a", required = false) final boolean showAll,
  67. @Option(flagOnly = true, name = "list", shortName = "l", required = false) final boolean list,
  68. final PipeOut out) throws FileNotFoundException
  69. {
  70. for (Resource<?> resource : paths)
  71. {
  72. if (resource instanceof JavaResource)
  73. {
  74. if (showAll)
  75. {
  76. out.print(JavaColorizer.format(out, ((JavaResource) resource).getJavaSource().toString()));
  77. }
  78. else
  79. {
  80. JavaResource javaResource = (JavaResource) resource;
  81. JavaSource<?> javaSource = javaResource.getJavaSource();
  82. List<String> output = new ArrayList<String>();
  83. if (!out.isPiped())
  84. {
  85. out.println();
  86. out.println(ShellColor.RED, "[fields]");
  87. }
  88. if (javaSource instanceof JavaClass)
  89. {
  90. JavaClass javaClass = (JavaClass) javaSource;
  91. List<Field<JavaClass>> fields = javaClass.getFields();
  92. for (Field<JavaClass> field : fields)
  93. {
  94. String entry = out.renderColor(ShellColor.BLUE, field.getVisibility().scope());
  95. entry += out.renderColor(ShellColor.GREEN, DELIM + field.getType() + "");
  96. entry += DELIM + field.getName() + ";";
  97. output.add(entry);
  98. }
  99. if (out.isPiped())
  100. {
  101. GeneralUtils.OutputAttributes attr = new GeneralUtils.OutputAttributes(120, 1);
  102. printOutColumns(output, ShellColor.NONE, out, attr, null, false);
  103. }
  104. else
  105. {
  106. GeneralUtils.printOutColumns(output, out, shell, true);
  107. out.println();
  108. }
  109. // rinse and repeat for methods
  110. output = new ArrayList<String>();
  111. List<Method<JavaClass>> methods = javaClass.getMethods();
  112. if (!out.isPiped())
  113. {
  114. out.println(ShellColor.RED, "[methods]");
  115. }
  116. for (Method<JavaClass> method : methods)
  117. {
  118. String entry = out.renderColor(ShellColor.BLUE, method.getVisibility().scope());
  119. String parameterString = "(";
  120. for (Parameter param : method.getParameters())
  121. {
  122. parameterString += param.toString();
  123. }
  124. parameterString += ")";
  125. entry += DELIM + method.getName() + parameterString;
  126. String returnType = method.getReturnType() == null ? "void" : method.getReturnType();
  127. entry += out.renderColor(ShellColor.GREEN, DELIM + returnType + "");
  128. output.add(entry);
  129. }
  130. if (out.isPiped())
  131. {
  132. GeneralUtils.OutputAttributes attr = new GeneralUtils.OutputAttributes(120, 1);
  133. printOutColumns(output, ShellColor.NONE, out, attr, null, false);
  134. }
  135. else
  136. {
  137. GeneralUtils.printOutColumns(output, out, shell, true);
  138. out.println();
  139. }
  140. }
  141. }
  142. }
  143. else if (resource instanceof JavaMemberResource<?>)
  144. {
  145. out.println();
  146. out.println(JavaColorizer.format(out, resource.toString()));
  147. }
  148. }
  149. }
  150. }