Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a service to allow non-static access to JavaParser API.
- Loading branch information
1 parent
9002ea7
commit 5056760
Showing
3 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
parser-java/api/src/main/java/org/jboss/forge/parser/java/JavaParserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2012 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.jboss.forge.parser.java; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.InputStream; | ||
|
||
import org.jboss.forge.container.services.Exported; | ||
|
||
/** | ||
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a> | ||
*/ | ||
@Exported | ||
public interface JavaParserService | ||
{ | ||
/** | ||
* Open the given {@link File}, parsing its contents into a new {@link JavaClass} instance. | ||
*/ | ||
public JavaSource<?> parse(final File file) throws FileNotFoundException; | ||
|
||
/** | ||
* Read the given {@link InputStream} and parse the data into a new {@link JavaClass} instance. | ||
*/ | ||
public JavaSource<?> parse(final InputStream data); | ||
|
||
/** | ||
* Parse the given character array into a new {@link JavaClass} instance. | ||
*/ | ||
public JavaSource<?> parse(final char[] data); | ||
|
||
/** | ||
* Parse the given String data into a new {@link JavaClass} instance. | ||
*/ | ||
public JavaSource<?> parse(final String data); | ||
|
||
/** | ||
* Create a new empty {@link JavaClass} instance. | ||
*/ | ||
public <T extends JavaSource<?>> T create(final Class<T> type); | ||
|
||
/** | ||
* Read the given {@link File} and parse its data into a new {@link JavaSource} instance of the given type. | ||
* | ||
* @throws FileNotFoundException | ||
*/ | ||
public <T extends JavaSource<?>> T parse(final Class<T> type, final File file) throws FileNotFoundException; | ||
|
||
/** | ||
* Read the given {@link InputStream} and parse its data into a new {@link JavaSource} instance of the given type. | ||
*/ | ||
public <T extends JavaSource<?>> T parse(final Class<T> type, final InputStream data); | ||
|
||
/** | ||
* Read the given character array and parse its data into a new {@link JavaSource} instance of the given type. | ||
*/ | ||
public <T extends JavaSource<?>> T parse(final Class<T> type, final char[] data); | ||
|
||
/** | ||
* Read the given string and parse its data into a new {@link JavaSource} instance of the given type. | ||
*/ | ||
public <T extends JavaSource<?>> T parse(final Class<T> type, final String data); | ||
} |
101 changes: 101 additions & 0 deletions
101
parser-java/impl/src/main/java/org/jboss/forge/parser/java/JavaParserServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright 2012 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.jboss.forge.parser.java; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.InputStream; | ||
|
||
import org.jboss.forge.parser.JavaParser; | ||
|
||
/** | ||
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a> | ||
*/ | ||
public class JavaParserServiceImpl implements JavaParserService | ||
{ | ||
/** | ||
* Open the given {@link File}, parsing its contents into a new {@link JavaClass} instance. | ||
*/ | ||
@Override | ||
public JavaSource<?> parse(final File file) throws FileNotFoundException | ||
{ | ||
return JavaParser.parse(file); | ||
} | ||
|
||
/** | ||
* Read the given {@link InputStream} and parse the data into a new {@link JavaClass} instance. | ||
*/ | ||
@Override | ||
public JavaSource<?> parse(final InputStream data) | ||
{ | ||
return JavaParser.parse(data); | ||
} | ||
|
||
/** | ||
* Parse the given character array into a new {@link JavaClass} instance. | ||
*/ | ||
@Override | ||
public JavaSource<?> parse(final char[] data) | ||
{ | ||
return JavaParser.parse(data); | ||
} | ||
|
||
/** | ||
* Parse the given String data into a new {@link JavaClass} instance. | ||
*/ | ||
@Override | ||
public JavaSource<?> parse(final String data) | ||
{ | ||
return JavaParser.parse(data); | ||
} | ||
|
||
/** | ||
* Create a new empty {@link JavaClass} instance. | ||
*/ | ||
@Override | ||
public <T extends JavaSource<?>> T create(final Class<T> type) | ||
{ | ||
return JavaParser.create(type); | ||
} | ||
|
||
/** | ||
* Read the given {@link File} and parse its data into a new {@link JavaSource} instance of the given type. | ||
* | ||
* @throws FileNotFoundException | ||
*/ | ||
@Override | ||
public <T extends JavaSource<?>> T parse(final Class<T> type, final File file) throws FileNotFoundException | ||
{ | ||
return JavaParser.parse(type, file); | ||
} | ||
|
||
/** | ||
* Read the given {@link InputStream} and parse its data into a new {@link JavaSource} instance of the given type. | ||
*/ | ||
public <T extends JavaSource<?>> T parse(final Class<T> type, final InputStream data) | ||
{ | ||
return JavaParser.parse(type, data); | ||
} | ||
|
||
/** | ||
* Read the given character array and parse its data into a new {@link JavaSource} instance of the given type. | ||
*/ | ||
@Override | ||
public <T extends JavaSource<?>> T parse(final Class<T> type, final char[] data) | ||
{ | ||
return JavaParser.parse(type, data); | ||
} | ||
|
||
/** | ||
* Read the given string and parse its data into a new {@link JavaSource} instance of the given type. | ||
*/ | ||
@Override | ||
public <T extends JavaSource<?>> T parse(final Class<T> type, final String data) | ||
{ | ||
return JavaParser.parse(type, data); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
parser-java/tests/src/test/java/test/org/jboss/forge/parser/java/JavaParserServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package test.org.jboss.forge.parser.java; | ||
|
||
/* | ||
* Copyright 2012 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.arquillian.junit.Arquillian; | ||
import org.jboss.forge.arquillian.Addon; | ||
import org.jboss.forge.arquillian.Dependencies; | ||
import org.jboss.forge.arquillian.archive.ForgeArchive; | ||
import org.jboss.forge.container.addons.AddonId; | ||
import org.jboss.forge.container.repositories.AddonDependencyEntry; | ||
import org.jboss.forge.parser.java.JavaClass; | ||
import org.jboss.forge.parser.java.JavaParserService; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
@RunWith(Arquillian.class) | ||
public class JavaParserServiceTest | ||
{ | ||
@Deployment | ||
@Dependencies({ | ||
@Addon(name = "org.jboss.forge:parser-java", version = "2.0.0-SNAPSHOT") | ||
}) | ||
public static ForgeArchive getDeployment() | ||
{ | ||
ForgeArchive archive = ShrinkWrap.create(ForgeArchive.class) | ||
.addBeansXML() | ||
.addAsAddonDependencies( | ||
AddonDependencyEntry.create(AddonId.from("org.jboss.forge:parser-java", "2.0.0-SNAPSHOT")) | ||
); | ||
|
||
return archive; | ||
} | ||
|
||
@Inject | ||
private JavaParserService parser; | ||
|
||
@Test | ||
public void testJavaResourceCreation() throws Exception | ||
{ | ||
JavaClass javaClass = parser.create(JavaClass.class).setPackage("org.jboss.forge.test").setName("Example"); | ||
Assert.assertEquals(0, javaClass.getMembers().size()); | ||
} | ||
} |
5056760
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome ! Good job Lincoln !
5056760
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we have ResourceFactory, ConverterFactory, ProjectFactory, etc, What do you think of renaming it to JavaSourceFactory ?
5056760
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
5056760
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
5056760
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.