Skip to content

Commit

Permalink
Added a service to allow non-static access to JavaParser API.
Browse files Browse the repository at this point in the history
  • Loading branch information
lincolnthree committed May 4, 2013
1 parent 9002ea7 commit 5056760
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 0 deletions.
@@ -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);
}
@@ -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);
}
}
@@ -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());
}
}

5 comments on commit 5056760

@gastaldi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome ! Good job Lincoln !

@gastaldi
Copy link
Member

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 ?

@lincolnthree
Copy link
Member Author

@lincolnthree lincolnthree commented on 5056760 May 5, 2013 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lincolnthree
Copy link
Member Author

@lincolnthree lincolnthree commented on 5056760 May 5, 2013 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gastaldi
Copy link
Member

@gastaldi gastaldi commented on 5056760 May 5, 2013 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.