Skip to content

Commit

Permalink
More work on fluent interface methods for modules
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid committed May 13, 2018
1 parent ef82d2b commit 93ed83c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 22 deletions.
Expand Up @@ -32,6 +32,8 @@
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.type.IntersectionType;
import com.github.javaparser.ast.type.Type;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
Expand All @@ -49,6 +51,16 @@

public class JavaParserTest {

@Before
public void setToLatestJava() {
JavaParser.getStaticConfiguration().setLanguageLevel(BLEEDING_EDGE);
}

@After
public void resetJavaLevel() {
JavaParser.getStaticConfiguration().setLanguageLevel(CURRENT);
}

@Test
public void rangeOfAnnotationMemberDeclarationIsCorrect() {
String code = "@interface AD { String foo(); }";
Expand Down Expand Up @@ -249,7 +261,6 @@ public void parsingInitializedAndUnitializedVarsInForStmtComplexCase() {

@Test
public void parseModuleDeclaration() {
JavaParser.getStaticConfiguration().setLanguageLevel(JAVA_9);
JavaParser.parseModuleDeclaration("module X {}");
}

Expand Down
Expand Up @@ -14,8 +14,6 @@
import com.github.javaparser.printer.ConcreteSyntaxModel;
import org.junit.Test;

import javax.annotation.Generated;

import static com.github.javaparser.GeneratedJavaParserConstants.IDENTIFIER;
import static com.github.javaparser.JavaParser.parseClassOrInterfaceType;
import static com.github.javaparser.JavaParser.parseName;
Expand All @@ -24,12 +22,12 @@
import static com.github.javaparser.utils.TestUtils.assertEqualsNoEol;
import static com.github.javaparser.utils.Utils.EOL;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;

public class ModuleDeclarationTest {
public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLanguageLevel(JAVA_9));

private final CompilationUnit parse(String code) {
private CompilationUnit parse(String code) {
return javaParser.parse(ParseStart.COMPILATION_UNIT, provider(code)).getResult().get();
}

Expand Down Expand Up @@ -70,7 +68,7 @@ public void jlsExample1() {

ModuleDeclaration module = cu.getModule().get();
assertEquals("M.N", module.getNameAsString());
assertEquals(false, module.isOpen());
assertFalse(module.isOpen());
assertThat(module.getAnnotations()).containsExactly(
new SingleMemberAnnotationExpr(new Name("Foo"), new IntegerLiteralExpr("1")),
new SingleMemberAnnotationExpr(new Name("Foo"), new IntegerLiteralExpr("2")),
Expand Down Expand Up @@ -105,7 +103,7 @@ public void jlsExample2HasAnOpenModule() {

ModuleDeclaration module = cu.getModule().get();
assertEquals("M.N", module.getNameAsString());
assertEquals(true, module.isOpen());
assertTrue(module.isOpen());
}

@Test
Expand Down Expand Up @@ -185,20 +183,35 @@ public void testCsmPrinting() {
@Test
public void fluentInterface() {
ModuleDeclaration moduleDeclaration = new CompilationUnit()
.addModule("org.javacord.api")
.addSingleMemberAnnotation(Generated.class, "generator")
.addModule("com.laamella.base")
.addSingleMemberAnnotation(SuppressWarnings.class, "\"module\"")
.addDirective("requires transitive java.desktop;")
.addDirective("exports org.javacord.api.entity.channel;")
.addDirective("exports org.javacord.api.entity.channel.internal to org.javacord.core;")
.addDirective("uses org.javacord.api.util.internal.DelegateFactoryDelegate;");

assertEqualsNoEol("@Generated(generator) @SuppressWarnings(\"module\") \n" +
"module org.javacord.api {\n" +
.addDirective("exports com.laamella.base.entity.channel;")
.addDirective("exports com.laamella.base.entity.channel.internal to com.laamella.core;")
.addDirective("uses com.laamella.base.util.internal.FactoryDelegate;");

moduleDeclaration.getModuleStmts()
.addLast(new ModuleExportsStmt()
.setName("foo.bar")
.addModuleName("other.foo")
.addModuleName("other.bar")
);

moduleDeclaration
.addDirective(new ModuleExportsStmt()
.setName("foo.bar.x")
.addModuleName("other.foo")
.addModuleName("other.bar")
);

assertEqualsNoEol("@SuppressWarnings(\"module\") \n" +
"module com.laamella.base {\n" +
" requires transitive java.desktop;\n" +
" exports org.javacord.api.entity.channel;\n" +
" exports org.javacord.api.entity.channel.internal to org.javacord.core;\n" +
" uses org.javacord.api.util.internal.DelegateFactoryDelegate;\n" +
" exports com.laamella.base.entity.channel;\n" +
" exports com.laamella.base.entity.channel.internal to com.laamella.core;\n" +
" uses com.laamella.base.util.internal.FactoryDelegate;\n" +
" exports foo.bar to other.foo, other.bar;\n" +
" exports foo.bar.x to other.foo, other.bar;\n" +
"}\n", moduleDeclaration.toString());
}
}
Expand Up @@ -204,10 +204,14 @@ public boolean replace(Node node, Node replacementNode) {
}

/**
* Add a directive to the module, like "
* Add a directive to the module, like "exports R.S to T1.U1, T2.U2;"
*/
public ModuleDeclaration addDirective(String directive) {
getModuleStmts().add(JavaParser.parseModuleDirective(directive));
return addDirective(JavaParser.parseModuleDirective(directive));
}

public ModuleDeclaration addDirective(ModuleStmt directive) {
getModuleStmts().add(directive);
return this;
}
}
Expand Up @@ -11,8 +11,8 @@
import com.github.javaparser.ast.visitor.VoidVisitor;
import com.github.javaparser.metamodel.JavaParserMetaModel;
import com.github.javaparser.metamodel.ModuleExportsStmtMetaModel;
import java.util.Arrays;
import java.util.List;

import static com.github.javaparser.JavaParser.parseName;
import static com.github.javaparser.utils.Utils.assertNotNull;
import javax.annotation.Generated;
import com.github.javaparser.TokenRange;
Expand Down Expand Up @@ -164,4 +164,9 @@ public void ifModuleExportsStmt(Consumer<ModuleExportsStmt> action) {
public Optional<ModuleExportsStmt> toModuleExportsStmt() {
return Optional.of(this);
}

public ModuleExportsStmt addModuleName(String name) {
moduleNames.add(parseName(name));
return this;
}
}

0 comments on commit 93ed83c

Please sign in to comment.