Skip to content

Commit

Permalink
Improve SourceRoot's quality a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid committed Mar 3, 2018
1 parent cf66169 commit e76b410
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
Expand Up @@ -33,8 +33,9 @@
import static java.nio.file.FileVisitResult.SKIP_SUBTREE;

/**
* A collection of Java source files located in one directory and its subdirectories on the file system. Files can be
* parsed and written back one by one or all together. <b>Note that</b> the internal cache used is thread-safe.
* A collection of Java source files located in one directory and its subdirectories on the file system. The root directory
* corresponds to the root of the package structure of the source files within. Files can be parsed and written back one
* by one or all together. <b>Note that</b> the internal cache used is thread-safe.
* <ul>
* <li>methods called "tryToParse..." will return their result inside a "ParseResult", which supports parse successes and failures.</li>
* <li>methods called "parse..." will return "CompilationUnit"s. If a file fails to parse, an exception is thrown.</li>
Expand Down Expand Up @@ -62,6 +63,10 @@ enum Result {
private Function<CompilationUnit, String> printer = new PrettyPrinter()::print;
private static final Pattern JAVA_IDENTIFIER = Pattern.compile("\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*");

/**
* @param root the root directory of a set of source files. It corresponds to the root of the package structure of the
* source files within, like "javaparser/javaparser-core/src/main/java"
*/
public SourceRoot(Path root) {
assertNotNull(root);
if (!Files.isDirectory(root)) {
Expand All @@ -71,6 +76,10 @@ public SourceRoot(Path root) {
Log.info("New source root at \"%s\"", this.root);
}

/**
* @param root the root directory of a set of source files. It corresponds to the root of the package structure of the
* source files within, like "javaparser/javaparser-core/src/main/java"
*/
public SourceRoot(Path root, ParserConfiguration parserConfiguration) {
this(root);
setParserConfiguration(parserConfiguration);
Expand Down Expand Up @@ -160,7 +169,9 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) th

private static boolean isSensibleDirectoryToEnter(Path dir) throws IOException {
final String dirToEnter = dir.getFileName().toString();
// Don't enter directories that cannot be packages.
final boolean directoryIsAValidJavaIdentifier = JAVA_IDENTIFIER.matcher(dirToEnter).matches();
// Don't enter directories that are hidden, assuming that people don't store source files in hidden directories.
if (Files.isHidden(dir) || !directoryIsAValidJavaIdentifier) {
Log.trace("Not processing directory \"%s\"", dirToEnter);
return false;
Expand Down
@@ -1,10 +1,12 @@
package com.github.javaparser.utils;

import com.github.javaparser.ParseProblemException;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ast.CompilationUnit;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
Expand All @@ -13,9 +15,14 @@
import static org.junit.Assert.assertTrue;

public class SourceRootTest {
private final Path root = CodeGenerationUtils.classLoaderRoot(SourceRootTest.class).resolve("com/github/javaparser/utils/");
private final Path root = CodeGenerationUtils.mavenModuleRoot(SourceRootTest.class).resolve("src/test/resources/com/github/javaparser/utils/");
private final SourceRoot sourceRoot = new SourceRoot(root);

@Before
public void before() {
sourceRoot.getParserConfiguration().setLanguageLevel(ParserConfiguration.LanguageLevel.BLEEDING_EDGE);
}

@Test
public void parseTestDirectory() throws IOException {

Expand All @@ -24,15 +31,24 @@ public void parseTestDirectory() throws IOException {

assertEquals(2, units.size());
assertTrue(units.stream().allMatch(unit -> !unit.getTypes().isEmpty() || unit.getModule().isPresent()));
assertTrue(parseResults.stream().anyMatch(cu -> cu.getResult().get().getStorage().get().getPath().toString().contains("source" + File.separator + "root")));
assertTrue(parseResults.stream().noneMatch(cu -> cu.getResult().get().getStorage().get().getPath().toString().contains("source.root")));
}

@Test(expected = IllegalArgumentException.class)
public void fileAsRootIsNotAllowed() {
Path path = CodeGenerationUtils.classLoaderRoot(SourceRootTest.class).resolve("/com/github/javaparser/utils/Bla.java");
Path path = CodeGenerationUtils.classLoaderRoot(SourceRootTest.class).resolve("com/github/javaparser/utils/Bla.java");
new SourceRoot(path);
}



@Test
public void dotsInRootDirectoryAreAllowed() throws IOException {
Path path = CodeGenerationUtils.mavenModuleRoot(SourceRootTest.class).resolve("src/test/resources/com/github/javaparser/utils/source.root");
new SourceRoot(path).tryToParse();
}

@Test(expected = ParseProblemException.class)
public void dotsInPackageAreNotAllowed() {
Path path = CodeGenerationUtils.mavenModuleRoot(SourceRootTest.class).resolve("src/test/resources/com/github/javaparser/utils");
new SourceRoot(path).parse("source.root", "Y.java");
}
}

0 comments on commit e76b410

Please sign in to comment.