Skip to content

Commit

Permalink
FORGE-474: Create a plugin which create enum types
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardo authored and gastaldi committed Feb 15, 2012
1 parent c6794af commit 4fc50c3
Show file tree
Hide file tree
Showing 11 changed files with 836 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.jboss.forge.parser.java.FieldHolder;
import org.jboss.forge.parser.java.Import;
import org.jboss.forge.parser.java.JavaClass;
import org.jboss.forge.parser.java.JavaEnum;
import org.jboss.forge.parser.java.JavaSource;
import org.jboss.forge.parser.java.Method;
import org.jboss.forge.parser.java.MethodHolder;
Expand Down Expand Up @@ -149,10 +150,97 @@ else if (in != null)
java.saveJavaSource(jc);
}
}

pickUp.fire(new PickupResource(java.getJavaResource(jc)));
}

@Command("new-enum-type")
public void newEnumType(
@PipeIn final InputStream in,
@Option(required = false,
help = "the package in which to build this Class",
description = "source package",
type = PromptType.JAVA_PACKAGE,
name = "package") final String pckg,
@Option(required = false,
help = "the class definition: surround with quotes",
description = "class definition") final String... def) throws FileNotFoundException
{

JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);

JavaEnum je = null;
if (def != null)
{
String classDef = Strings.join(Arrays.asList(def), " ");
je = JavaParser.parse(JavaEnum.class, classDef);
}
else if (in != null)
{
je = JavaParser.parse(JavaEnum.class, in);
}
else
{
throw new RuntimeException("arguments required");
}

if (pckg != null)
{
je.setPackage(pckg);
}

if (!je.hasSyntaxErrors())
{
java.saveEnumTypeSource(je);
}
else
{
writer.println(ShellColor.RED, "Syntax Errors:");
for (SyntaxError error : je.getSyntaxErrors())
{
writer.println(error.toString());
}
writer.println();

if (prompt.promptBoolean("Your class has syntax errors, create anyway?", true))
{
java.saveEnumTypeSource(je);
}
}

pickUp.fire(new PickupResource(java.getEnumTypeResource(je)));
}

@Command("new-enum-const")
@RequiresResource(JavaResource.class)
public void newEnumConst(
@PipeIn final String in,
final PipeOut out,
@Option(required = false,
help = "the enum field definition",
description = "enum field definition") final String... def) throws FileNotFoundException
{
JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);

String enumConstDef = null;
if (def != null)
{
enumConstDef = Strings.join(Arrays.asList(def), " ");
}
else if (in != null)
{
enumConstDef = in;
}
else
{
throw new RuntimeException("arguments required");
}

JavaEnum source = (JavaEnum) resource.getJavaSource();
source.addEnumConstant(enumConstDef);
java.saveEnumTypeSource(source);

}

@Command("list-imports")
@RequiresResource(JavaResource.class)
public void listImports(
Expand Down
109 changes: 109 additions & 0 deletions dev-plugins/src/test/java/org/jboss/forge/dev/java/JavaPluginTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011, 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.forge.dev.java;

import static junit.framework.Assert.assertNotNull;

import org.jboss.forge.parser.java.EnumConstant;
import org.jboss.forge.parser.java.Field;
import org.jboss.forge.parser.java.JavaClass;
import org.jboss.forge.parser.java.JavaEnum;
import org.jboss.forge.parser.java.Method;
import org.jboss.forge.project.facets.JavaSourceFacet;
import org.jboss.forge.shell.util.Packages;
import org.jboss.forge.test.AbstractShellTest;
import org.junit.Before;
import org.junit.Test;

public class JavaPluginTest extends AbstractShellTest
{

@Before
public void initializeTest() throws Exception
{
initializeJavaProject();
}

@Test
public void testCreateJavaClass() throws Exception
{
getShell().execute(
"java new-class --package org.jboss.forge.test.classes \"public class TestingClassCreation {}\"");
getShell().execute("build");
JavaClass javaClass = (JavaClass) getProject().getFacet(JavaSourceFacet.class)
.getJavaResource(Packages.toFileSyntax("org.jboss.forge.test.classes.TestingClassCreation"))
.getJavaSource();
assertNotNull(javaClass);
}

@Test
public void testCreateJavaField() throws Exception
{
getShell().execute(
"java new-class --package org.jboss.forge.test.classes \"public class TestingFieldCreation {}\"");
getShell().execute("java new-field \"private int testing;\"");
getShell().execute("ls");
getShell().execute("build");
JavaClass javaClass = (JavaClass) getProject().getFacet(JavaSourceFacet.class)
.getJavaResource(Packages.toFileSyntax("org.jboss.forge.test.classes.TestingFieldCreation"))
.getJavaSource();
Field<JavaClass> field = javaClass.getField("testing");
assertNotNull(field);
}

@Test
public void testCreateJavaMethod() throws Exception
{
getShell().execute(
"java new-class --package org.jboss.forge.test.classes \"public class TestingMethodCreation {}\"");
getShell().execute("java new-method \"public void testing(){}\"");
getShell().execute("ls");
getShell().execute("build");
JavaClass javaClass = (JavaClass) getProject().getFacet(JavaSourceFacet.class)
.getJavaResource(Packages.toFileSyntax("org.jboss.forge.test.classes.TestingMethodCreation"))
.getJavaSource();
Method<JavaClass> method = javaClass.getMethod("testing");
assertNotNull(method);
}

@Test
public void testCreateEnumType() throws Exception
{
getShell().execute("java new-enum-type --package org.jboss.forge.test.types \"public enum TestingEnumTypeCreation{}\"");
getShell().execute("ls");
getShell().execute("build");
JavaEnum javaEnum = (JavaEnum) getProject().getFacet(JavaSourceFacet.class).getEnumTypeResource(Packages.toFileSyntax("org.jboss.forge.test.types.TestingEnumTypeCreation")).getJavaSource();
assertNotNull(javaEnum);
}

@Test
public void testCreateEnumConst() throws Exception
{
getShell().execute("java new-enum-type --package org.jboss.forge.test.types \"public enum TestingEnumTypeCreation{}\"");
getShell().execute("java new-enum-const \"A\"");
getShell().execute("ls");
getShell().execute("build");
JavaEnum javaEnum = (JavaEnum) getProject().getFacet(JavaSourceFacet.class).getEnumTypeResource(Packages.toFileSyntax("org.jboss.forge.test.types.TestingEnumTypeCreation")).getJavaSource();
EnumConstant<JavaEnum> enumConst = javaEnum.getEnumConstant("A");
assertNotNull(enumConst);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.jboss.forge.parser.java;

public interface EnumConstant<O extends JavaSource<O>> extends Member<O, EnumConstant<O>>
{

/**
* Get this enum constant name.
*/
String getName();

/**
* Set this enum constant name.
*/
EnumConstant<O> setName(String name);

/**
* @return
*/
String getType();

/**
* @return
*/
String getQualifiedType();

/**
* @param type
* @return
*/
boolean isType(Class<?> type);

/**
* @param type
* @return
*/
boolean isType(String type);

/**
* @param clazz
* @return
*/
EnumConstant<O> setType(Class<?> clazz);

/**
* @param type
* @return
*/
EnumConstant<O> setType(String type);

/**
* @param entity
* @return
*/
EnumConstant<O> setType(JavaSource<?> entity);

/**
* @return
*/
String getStringInitializer();

/**
* @return
*/
String getLiteralInitializer();

/**
* @param value
* @return
*/
EnumConstant<O> setLiteralInitializer(String value);

/**
* @param value
* @return
*/
EnumConstant<O> setStringInitializer(String value);

/* (non-Javadoc)
* @see org.jboss.forge.parser.Internal#getInternal()
*/
Object getInternal();

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,11 @@
*/
public interface JavaEnum extends JavaSource<JavaEnum>
{

EnumConstant<JavaEnum> addEnumConstant();

EnumConstant<JavaEnum> addEnumConstant(String declaration);

EnumConstant<JavaEnum> getEnumConstant(String declaration);

}
Loading

0 comments on commit 4fc50c3

Please sign in to comment.