[seam-commits] Seam SVN: r13341 - in sandbox/encore: core/src/main/java/org/jboss/encore/grammar/java and 4 other directories.
seam-commits at lists.jboss.org
seam-commits at lists.jboss.org
Tue Jul 6 20:46:19 EDT 2010
Author: lincolnthree
Date: 2010-07-06 20:46:18 -0400 (Tue, 06 Jul 2010)
New Revision: 13341
Added:
sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Method.java
sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/ast/
sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/ast/MethodFinderVisitor.java
sandbox/encore/shell/src/main/java/org/jboss/encore/shell/CommandCompleter.java
sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/
sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/Command.java
sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/Option.java
Modified:
sandbox/encore/core/pom.xml
sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/JavaParser.java
sandbox/encore/core/src/test/java/org/jboss/encore/grammar/java/JavaParserTest.java
sandbox/encore/shell/src/main/java/org/jboss/encore/shell/Shell.java
Log:
Parser API & Shell beginnings
Modified: sandbox/encore/core/pom.xml
===================================================================
--- sandbox/encore/core/pom.xml 2010-07-06 17:22:48 UTC (rev 13340)
+++ sandbox/encore/core/pom.xml 2010-07-07 00:46:18 UTC (rev 13341)
@@ -44,10 +44,12 @@
<groupId>org.eclipse.equinox</groupId>
<artifactId>app</artifactId>
</exclusion>
+ <!--
<exclusion>
<groupId>org.eclipse</groupId>
<artifactId>text</artifactId>
</exclusion>
+ -->
<exclusion>
<groupId>org.eclipse.core</groupId>
<artifactId>filesystem</artifactId>
Modified: sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/JavaParser.java
===================================================================
--- sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/JavaParser.java 2010-07-06 17:22:48 UTC (rev 13340)
+++ sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/JavaParser.java 2010-07-07 00:46:18 UTC (rev 13341)
@@ -1,44 +1,118 @@
package org.jboss.encore.grammar.java;
import java.io.InputStream;
+import java.util.Collections;
+import java.util.List;
+import java.util.Stack;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
+import org.eclipse.jdt.core.dom.ImportDeclaration;
+import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
+import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
import org.eclipse.jdt.internal.compiler.util.Util;
+import org.eclipse.jface.text.Document;
+import org.eclipse.text.edits.TextEdit;
+import org.eclipse.text.edits.UndoEdit;
public class JavaParser
{
+ private Document document;
+ private final Stack<UndoEdit> undoStack = new Stack<UndoEdit>();
+
/**
- * Parses and process the java source code as a compilation unit and the
- * result it abstract syntax tree (AST) representation and this action uses
- * the third edition of java Language Specification, that gets the
- * possibility to support J2SE 5 during the parsing.
+ * Parses and process the java source code as a compilation unit and the result it abstract syntax tree (AST)
+ * representation and this action uses the third edition of java Language Specification.
*
- * @param source - the java source to be parsed (i.e. the char[] contains
- * Java source).
- * @return CompilationUnit Abstract syntax tree representation of a java
- * source file.
+ * @param source - the java source to be parsed (i.e. the char[] contains Java source).
+ * @return CompilationUnit Abstract syntax tree representation of a java source file.
*/
- public static CompilationUnit parse(InputStream inputStream)
+ public JavaParser(final InputStream inputStream)
{
try
{
char[] source = Util.getInputStreamAsCharArray(inputStream, inputStream.available(), "ISO8859_1");
+ document = new Document(new String(source));
+ }
+ catch (Exception e)
+ {
+ throw new IllegalArgumentException("InputStream must be a parsable java file: ", e);
+ }
+ }
- ASTParser parser = ASTParser.newParser(AST.JLS3);
- parser.setSource(source);
- parser.setKind(ASTParser.K_COMPILATION_UNIT);
- // to get more informations from the unit.
- parser.setResolveBindings(true);
+ public JavaParser(final char[] source)
+ {
+ document = new Document(new String(source));
+ }
- return (CompilationUnit) parser.createAST(null);
+ public CompilationUnit parse()
+ {
+ ASTParser parser = ASTParser.newParser(AST.JLS3);
+ parser.setSource(document.get().toCharArray());
+ parser.setResolveBindings(true);
+ parser.setKind(ASTParser.K_COMPILATION_UNIT);
+ CompilationUnit cu = (CompilationUnit) parser.createAST(null);
+ return cu;
+ }
+
+ public JavaParser addImport(final String className)
+ {
+ CompilationUnit cu = parse();
+ try
+ {
+ AST ast = cu.getAST();
+ ASTRewrite rewriter = ASTRewrite.create(ast);
+ ListRewrite lrw = rewriter.getListRewrite(cu, CompilationUnit.IMPORTS_PROPERTY);
+
+ ImportDeclaration id = ast.newImportDeclaration();
+ id.setName(ast.newName(tokenizeClassName(className)));
+
+ lrw.insertLast(id, null);
+
+ TextEdit edits = rewriter.rewriteAST(document, null);
+ UndoEdit undo = edits.apply(document);
+ undoStack.add(undo);
+
+ return this;
}
catch (Exception e)
{
- throw new IllegalArgumentException("InputStream must be a parsable java file: ", e);
+ throw new RuntimeException("Could not add import: \"" + className +
+ "\" to compilation unit: \"" + "\"", e);
}
}
+ public void addImport(final Class<?> type)
+ {
+ addImport(type.getName());
+ }
+
+ public void addImports(final Class<?>... types)
+ {
+ for (Class<?> type : types)
+ {
+ addImport(type.getName());
+ }
+ }
+
+ private String[] tokenizeClassName(final String className)
+ {
+ String[] result = className.split("\\.");
+ return result;
+ }
+
+ public Stack<UndoEdit> getUndoStack()
+ {
+ return undoStack;
+ }
+
+ @SuppressWarnings("unchecked")
+ public List<ImportDeclaration> getImports()
+ {
+ CompilationUnit unit = parse();
+ List<ImportDeclaration> imports = unit.imports();
+ return Collections.unmodifiableList(imports);
+ }
}
Added: sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Method.java
===================================================================
--- sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Method.java (rev 0)
+++ sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Method.java 2010-07-07 00:46:18 UTC (rev 13341)
@@ -0,0 +1,31 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.encore.grammar.java;
+
+/**
+ * @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
+ *
+ */
+public class Method
+{
+
+}
Added: sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/ast/MethodFinderVisitor.java
===================================================================
--- sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/ast/MethodFinderVisitor.java (rev 0)
+++ sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/ast/MethodFinderVisitor.java 2010-07-07 00:46:18 UTC (rev 13341)
@@ -0,0 +1,34 @@
+package org.jboss.encore.grammar.java.ast;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.eclipse.jdt.core.dom.ASTNode;
+import org.eclipse.jdt.core.dom.ASTVisitor;
+import org.eclipse.jdt.core.dom.MethodDeclaration;
+import org.eclipse.jdt.core.dom.TypeDeclaration;
+
+public class MethodFinderVisitor extends ASTVisitor
+{
+ private final List<MethodDeclaration> methods = new ArrayList<MethodDeclaration>();
+ private ASTNode parent;
+
+ @Override
+ public boolean visit(final TypeDeclaration node)
+ {
+ parent = node;
+ methods.addAll(Arrays.asList(node.getMethods()));
+ return super.visit(node);
+ }
+
+ public List<MethodDeclaration> getMethods()
+ {
+ return methods;
+ }
+
+ public TypeDeclaration getParent()
+ {
+ return (TypeDeclaration) parent;
+ }
+}
\ No newline at end of file
Modified: sandbox/encore/core/src/test/java/org/jboss/encore/grammar/java/JavaParserTest.java
===================================================================
--- sandbox/encore/core/src/test/java/org/jboss/encore/grammar/java/JavaParserTest.java 2010-07-06 17:22:48 UTC (rev 13340)
+++ sandbox/encore/core/src/test/java/org/jboss/encore/grammar/java/JavaParserTest.java 2010-07-07 00:46:18 UTC (rev 13341)
@@ -1,19 +1,13 @@
package org.jboss.encore.grammar.java;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.Arrays;
+import java.net.URL;
import java.util.List;
+import java.util.Map;
-import org.eclipse.jdt.core.dom.ASTNode;
-import org.eclipse.jdt.core.dom.ASTVisitor;
-import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ImportDeclaration;
-import org.eclipse.jdt.core.dom.MethodDeclaration;
-import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.junit.Before;
import org.junit.Test;
@@ -31,54 +25,38 @@
@Test
public void testParse() throws Exception
{
- CompilationUnit unit = JavaParser.parse(stream);
+ JavaParser parser = new JavaParser(stream);
+ List<ImportDeclaration> imports = parser.getImports();
- @SuppressWarnings("unchecked")
- List<ImportDeclaration> imports = unit.imports();
-
- assertEquals("java.net.URL", imports.get(0).getName().getFullyQualifiedName());
+ assertEquals(URL.class.getName(), imports.get(0).getName().getFullyQualifiedName());
}
@Test
- public void testAddMethod() throws Exception
+ public void testAddImport() throws Exception
{
- CompilationUnit unit = JavaParser.parse(stream);
- MethodFinderVisitor visitor = new MethodFinderVisitor();
- unit.accept(visitor);
- List<MethodDeclaration> methods = visitor.getMethods();
- TypeDeclaration parent = visitor.getParent();
- MethodDeclaration newMethod = parent.getAST().newMethodDeclaration();
- newMethod.setName(parent.getAST().newSimpleName("addedMethod"));
- newMethod.setConstructor(false);
- newMethod.setReturnType2(null);
-
- // http://help.eclipse.org/ganymede/index.jsp?topic=/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/core/dom/rewrite/package-summary.html
- methods = visitor.getMethods();
-
- assertTrue(methods.contains(newMethod));
+ JavaParser parser = new JavaParser(stream);
+ parser.addImport(List.class.getName());
+ List<ImportDeclaration> imports = parser.getImports();
+ assertEquals(2, imports.size());
+ assertEquals(URL.class.getName(), imports.get(0).getName().getFullyQualifiedName());
+ assertEquals(List.class.getName(), imports.get(1).getName().getFullyQualifiedName());
}
- public class MethodFinderVisitor extends ASTVisitor
+ @Test
+ public void testAddImportsClasses() throws Exception
{
- private final List<MethodDeclaration> methods = new ArrayList<MethodDeclaration>();
- private ASTNode parent;
+ JavaParser parser = new JavaParser(stream);
- @Override
- public boolean visit(final TypeDeclaration node)
- {
- parent = node;
- methods.addAll(Arrays.asList(node.getMethods()));
- return super.visit(node);
- }
+ List<ImportDeclaration> imports = parser.getImports();
+ assertEquals(1, imports.size());
- public List<MethodDeclaration> getMethods()
- {
- return methods;
- }
+ parser.addImports(List.class, Map.class);
- public TypeDeclaration getParent()
- {
- return (TypeDeclaration) parent;
- }
+ imports = parser.getImports();
+ assertEquals(3, imports.size());
+ assertEquals(URL.class.getName(), imports.get(0).getName().getFullyQualifiedName());
+ assertEquals(List.class.getName(), imports.get(1).getName().getFullyQualifiedName());
+ assertEquals(Map.class.getName(), imports.get(2).getName().getFullyQualifiedName());
}
+
}
Added: sandbox/encore/shell/src/main/java/org/jboss/encore/shell/CommandCompleter.java
===================================================================
--- sandbox/encore/shell/src/main/java/org/jboss/encore/shell/CommandCompleter.java (rev 0)
+++ sandbox/encore/shell/src/main/java/org/jboss/encore/shell/CommandCompleter.java 2010-07-07 00:46:18 UTC (rev 13341)
@@ -0,0 +1,41 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.encore.shell;
+
+import java.util.List;
+
+import jline.console.completer.Completer;
+
+/**
+ * @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
+ *
+ */
+public class CommandCompleter implements Completer
+{
+
+ @Override
+ public int complete(final String buffer, final int cursor, final List<CharSequence> candidates)
+ {
+ return 0;
+ }
+
+}
Modified: sandbox/encore/shell/src/main/java/org/jboss/encore/shell/Shell.java
===================================================================
--- sandbox/encore/shell/src/main/java/org/jboss/encore/shell/Shell.java 2010-07-06 17:22:48 UTC (rev 13340)
+++ sandbox/encore/shell/src/main/java/org/jboss/encore/shell/Shell.java 2010-07-07 00:46:18 UTC (rev 13341)
@@ -27,6 +27,8 @@
import javax.inject.Inject;
import javax.inject.Singleton;
+import jline.console.ConsoleReader;
+
import org.jboss.encore.shell.events.StartupEvent;
import org.jboss.weld.environment.se.bindings.Parameters;
import org.slf4j.Logger;
@@ -38,6 +40,9 @@
@Singleton
public class Shell
{
+ private static String completionKeys = "TAB";
+ private final String prompt = "encore> ";
+
@Inject
@Parameters
private List<String> parameters;
@@ -45,9 +50,15 @@
@Inject
Logger log;
- public void init(@Observes final StartupEvent event)
+ private ConsoleReader reader;
+
+ public void init(@Observes final StartupEvent event) throws Exception
{
System.out.println("Startup");
log.info("Encore Shell - Starting up.");
+
+ reader = new ConsoleReader();
+ reader.setHistoryEnabled(true);
+ reader.setPrompt(prompt);
}
}
\ No newline at end of file
Added: sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/Command.java
===================================================================
--- sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/Command.java (rev 0)
+++ sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/Command.java 2010-07-07 00:46:18 UTC (rev 13341)
@@ -0,0 +1,57 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.encore.shell.cli;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import javax.inject.Qualifier;
+
+/**
+ * Represents a single command to be run on a Shell.
+ *
+ * @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
+ *
+ */
+ at Qualifier
+ at Target({ METHOD, PARAMETER, TYPE, FIELD })
+ at Retention(RUNTIME)
+ at Documented
+public @interface Command
+{
+ /**
+ * One or more names for this command.
+ */
+ String[] value();
+
+ /**
+ * Help text for this command.
+ */
+ String help();
+}
Added: sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/Option.java
===================================================================
--- sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/Option.java (rev 0)
+++ sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/Option.java 2010-07-07 00:46:18 UTC (rev 13341)
@@ -0,0 +1,46 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.encore.shell.cli;
+
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import javax.inject.Qualifier;
+
+/**
+ * @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
+ *
+ */
+
+ at Qualifier
+ at Target({ METHOD, PARAMETER })
+ at Retention(RUNTIME)
+ at Documented
+public @interface Option
+{
+
+}
More information about the seam-commits
mailing list