From 0b893c58ae4e0b4a2f8b20eb1582fc55972e863f Mon Sep 17 00:00:00 2001 From: Daan Schipper Date: Mon, 19 Feb 2018 13:44:30 +0100 Subject: SourceRoot implementation for JSS --- .../symbolsolver/utils/SymbolSourceRoot.java | 147 +++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSourceRoot.java (limited to 'javaparser-symbol-solver-core/src/main') diff --git a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSourceRoot.java b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSourceRoot.java new file mode 100644 index 000000000..e5450e19d --- /dev/null +++ b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSourceRoot.java @@ -0,0 +1,147 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2018 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser 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. + */ + +package com.github.javaparser.symbolsolver.utils; + +import com.github.javaparser.JavaParser; +import com.github.javaparser.ParseProblemException; +import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade; +import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; +import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver; +import com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver; +import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver; +import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver; +import com.github.javaparser.utils.Log; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.file.*; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.HashSet; +import java.util.Optional; +import java.util.Set; +import java.util.logging.Level; + +import static com.github.javaparser.utils.Utils.assertNotNull; +import static java.nio.file.FileVisitResult.CONTINUE; +import static java.nio.file.FileVisitResult.SKIP_SIBLINGS; + +public class SymbolSourceRoot { + + private static java.util.logging.Logger logger = java.util.logging.Logger.getLogger(JavaParserFacade.class.getCanonicalName()); + + private final Path root; + private CombinedTypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(false)); + + public SymbolSourceRoot(Path root) { + assertNotNull(root); + if (!Files.isDirectory(root)) { + throw new IllegalArgumentException("Only directories are allowed as root path!"); + } + this.root = root.normalize(); + Log.info("New symbol source root at \"%s\"", this.root); + } + + public TypeSolver walk() throws IOException { + Files.walkFileTree(root, new JavaSymbolSolverWalker()); + Files.walkFileTree(root, new JarVisitor()); + return typeSolver; + } + + public Optional tryToWalk() { + try { + Files.walkFileTree(root, new JavaSymbolSolverWalker()); + Files.walkFileTree(root, new JarVisitor()); + return Optional.of(typeSolver); + } catch (IOException e) { + return Optional.empty(); + } + } + + public TypeSolver getTypeSolver() { + return typeSolver; + } + + /** + * The path that was passed in the constructor. + */ + public Path getRoot() { + return root; + } + + /** + * Walks the directory and adds the roots of the java files to the TypeSolver + */ + private class JavaSymbolSolverWalker extends SimpleFileVisitor { + + private final Set roots = new HashSet<>(); + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attr) throws FileNotFoundException { + if (attr.isRegularFile()) { + PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**.java"); + if (matcher.matches(file)) { + try { + Optional root = JavaParser.parse(file.toFile()).getStorage() + .map(CompilationUnit.Storage::getSourceRoot); + if (root.isPresent()) { + typeSolver.add(new JavaParserTypeSolver(root.get().toFile())); + roots.add(root.get()); + logger.log(Level.FINE, "Added dir " + root.get() + " to the TypeSolver"); + return SKIP_SIBLINGS; + } + } catch (ParseProblemException e) { + logger.log(Level.WARNING, "Unable to parse file " + file, e); + } + } + } + return CONTINUE; + } + + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { + if (Files.isHidden(dir) || roots.stream().anyMatch(dir::startsWith)) { + return SKIP_SIBLINGS; + } + return CONTINUE; + } + } + + private class JarVisitor extends SimpleFileVisitor { + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attr) { + if (attr.isRegularFile()) { + PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**.jar"); + if (matcher.matches(file)) { + try { + typeSolver.add(new JarTypeSolver(file.toString())); + } catch (IOException e) { + logger.log(Level.WARNING, "IOException for file " + file, e); + } + } + } + return CONTINUE; + } + } +} + -- cgit v1.2.3 From 9b6720e36b5a49705d837eab841911b8849367c4 Mon Sep 17 00:00:00 2001 From: Daan Schipper Date: Mon, 19 Feb 2018 14:10:32 +0100 Subject: minor adjustments based on feedback --- .../github/javaparser/symbolsolver/utils/SymbolSourceRoot.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'javaparser-symbol-solver-core/src/main') diff --git a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSourceRoot.java b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSourceRoot.java index e5450e19d..386c5815d 100644 --- a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSourceRoot.java +++ b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSourceRoot.java @@ -40,6 +40,7 @@ import java.util.HashSet; import java.util.Optional; import java.util.Set; import java.util.logging.Level; +import java.util.logging.Logger; import static com.github.javaparser.utils.Utils.assertNotNull; import static java.nio.file.FileVisitResult.CONTINUE; @@ -47,7 +48,7 @@ import static java.nio.file.FileVisitResult.SKIP_SIBLINGS; public class SymbolSourceRoot { - private static java.util.logging.Logger logger = java.util.logging.Logger.getLogger(JavaParserFacade.class.getCanonicalName()); + private static Logger logger = Logger.getLogger(JavaParserFacade.class.getCanonicalName()); private final Path root; private CombinedTypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(false)); @@ -69,10 +70,9 @@ public class SymbolSourceRoot { public Optional tryToWalk() { try { - Files.walkFileTree(root, new JavaSymbolSolverWalker()); - Files.walkFileTree(root, new JarVisitor()); - return Optional.of(typeSolver); + return Optional.of(walk()); } catch (IOException e) { + logger.log(Level.WARNING, "Unable to walk root " + root, e); return Optional.empty(); } } -- cgit v1.2.3 From 354773c7727dfde5a6393e0fc4e42b35fe5df48e Mon Sep 17 00:00:00 2001 From: Daan Schipper Date: Mon, 19 Feb 2018 14:24:19 +0100 Subject: rename of class, added some comments and added one final check for skipping a directory only when the root is succesfully added to known roots --- .../symbolsolver/utils/SymbolSolverQuickSetup.java | 152 +++++++++++++++++++++ .../symbolsolver/utils/SymbolSourceRoot.java | 147 -------------------- 2 files changed, 152 insertions(+), 147 deletions(-) create mode 100644 javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSolverQuickSetup.java delete mode 100644 javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSourceRoot.java (limited to 'javaparser-symbol-solver-core/src/main') diff --git a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSolverQuickSetup.java b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSolverQuickSetup.java new file mode 100644 index 000000000..a9287d7d7 --- /dev/null +++ b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSolverQuickSetup.java @@ -0,0 +1,152 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2018 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser 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. + */ + +package com.github.javaparser.symbolsolver.utils; + +import com.github.javaparser.JavaParser; +import com.github.javaparser.ParseProblemException; +import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade; +import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; +import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver; +import com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver; +import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver; +import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver; +import com.github.javaparser.utils.Log; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.file.*; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.HashSet; +import java.util.Optional; +import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; + +import static com.github.javaparser.utils.Utils.assertNotNull; +import static java.nio.file.FileVisitResult.CONTINUE; +import static java.nio.file.FileVisitResult.SKIP_SIBLINGS; + +/** + * Utility class to add all jars and roots of java files of the provided path to a TypeSolver instance. + * It traverses the file directory tree and adds all files ending in either .java or .jar. + */ +public class SymbolSolverQuickSetup { + + private static Logger logger = Logger.getLogger(JavaParserFacade.class.getCanonicalName()); + + private final Path root; + private CombinedTypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(false)); + + public SymbolSolverQuickSetup(Path root) { + assertNotNull(root); + if (!Files.isDirectory(root)) { + throw new IllegalArgumentException("Only directories are allowed as root path!"); + } + this.root = root.normalize(); + Log.info("New symbol source root at \"%s\"", this.root); + } + + public TypeSolver walk() throws IOException { + Files.walkFileTree(root, new JavaSymbolSolverWalker()); + Files.walkFileTree(root, new JarVisitor()); + return typeSolver; + } + + public Optional tryToWalk() { + try { + return Optional.of(walk()); + } catch (IOException e) { + logger.log(Level.WARNING, "Unable to walk root " + root, e); + return Optional.empty(); + } + } + + public TypeSolver getTypeSolver() { + return typeSolver; + } + + /** + * The path that was passed in the constructor. + */ + public Path getRoot() { + return root; + } + + /** + * Walks the directory and adds the roots of the java files to the TypeSolver + */ + private class JavaSymbolSolverWalker extends SimpleFileVisitor { + + private final Set roots = new HashSet<>(); + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attr) throws FileNotFoundException { + if (attr.isRegularFile()) { + PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**.java"); + if (matcher.matches(file)) { + try { + Optional root = JavaParser.parse(file.toFile()).getStorage() + .map(CompilationUnit.Storage::getSourceRoot); + if (root.isPresent()) { + typeSolver.add(new JavaParserTypeSolver(root.get().toFile())); + if (roots.add(root.get())) { + logger.log(Level.FINE, "Added dir " + root.get() + " to the TypeSolver"); + return SKIP_SIBLINGS; + } + } + } catch (ParseProblemException e) { + logger.log(Level.WARNING, "Unable to parse file " + file, e); + } + } + } + return CONTINUE; + } + + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { + if (Files.isHidden(dir) || roots.stream().anyMatch(dir::startsWith)) { + return SKIP_SIBLINGS; + } + return CONTINUE; + } + } + + private class JarVisitor extends SimpleFileVisitor { + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attr) { + if (attr.isRegularFile()) { + PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**.jar"); + if (matcher.matches(file)) { + try { + typeSolver.add(new JarTypeSolver(file.toString())); + } catch (IOException e) { + logger.log(Level.WARNING, "IOException for file " + file, e); + } + } + } + return CONTINUE; + } + } +} + diff --git a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSourceRoot.java b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSourceRoot.java deleted file mode 100644 index 386c5815d..000000000 --- a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSourceRoot.java +++ /dev/null @@ -1,147 +0,0 @@ -/* - * Copyright (C) 2007-2010 Júlio Vilmar Gesser. - * Copyright (C) 2011, 2013-2018 The JavaParser Team. - * - * This file is part of JavaParser. - * - * JavaParser can be used either under the terms of - * a) the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * b) the terms of the Apache License - * - * You should have received a copy of both licenses in LICENCE.LGPL and - * LICENCE.APACHE. Please refer to those files for details. - * - * JavaParser 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. - */ - -package com.github.javaparser.symbolsolver.utils; - -import com.github.javaparser.JavaParser; -import com.github.javaparser.ParseProblemException; -import com.github.javaparser.ast.CompilationUnit; -import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade; -import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; -import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver; -import com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver; -import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver; -import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver; -import com.github.javaparser.utils.Log; - -import java.io.FileNotFoundException; -import java.io.IOException; -import java.nio.file.*; -import java.nio.file.attribute.BasicFileAttributes; -import java.util.HashSet; -import java.util.Optional; -import java.util.Set; -import java.util.logging.Level; -import java.util.logging.Logger; - -import static com.github.javaparser.utils.Utils.assertNotNull; -import static java.nio.file.FileVisitResult.CONTINUE; -import static java.nio.file.FileVisitResult.SKIP_SIBLINGS; - -public class SymbolSourceRoot { - - private static Logger logger = Logger.getLogger(JavaParserFacade.class.getCanonicalName()); - - private final Path root; - private CombinedTypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(false)); - - public SymbolSourceRoot(Path root) { - assertNotNull(root); - if (!Files.isDirectory(root)) { - throw new IllegalArgumentException("Only directories are allowed as root path!"); - } - this.root = root.normalize(); - Log.info("New symbol source root at \"%s\"", this.root); - } - - public TypeSolver walk() throws IOException { - Files.walkFileTree(root, new JavaSymbolSolverWalker()); - Files.walkFileTree(root, new JarVisitor()); - return typeSolver; - } - - public Optional tryToWalk() { - try { - return Optional.of(walk()); - } catch (IOException e) { - logger.log(Level.WARNING, "Unable to walk root " + root, e); - return Optional.empty(); - } - } - - public TypeSolver getTypeSolver() { - return typeSolver; - } - - /** - * The path that was passed in the constructor. - */ - public Path getRoot() { - return root; - } - - /** - * Walks the directory and adds the roots of the java files to the TypeSolver - */ - private class JavaSymbolSolverWalker extends SimpleFileVisitor { - - private final Set roots = new HashSet<>(); - - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attr) throws FileNotFoundException { - if (attr.isRegularFile()) { - PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**.java"); - if (matcher.matches(file)) { - try { - Optional root = JavaParser.parse(file.toFile()).getStorage() - .map(CompilationUnit.Storage::getSourceRoot); - if (root.isPresent()) { - typeSolver.add(new JavaParserTypeSolver(root.get().toFile())); - roots.add(root.get()); - logger.log(Level.FINE, "Added dir " + root.get() + " to the TypeSolver"); - return SKIP_SIBLINGS; - } - } catch (ParseProblemException e) { - logger.log(Level.WARNING, "Unable to parse file " + file, e); - } - } - } - return CONTINUE; - } - - @Override - public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { - if (Files.isHidden(dir) || roots.stream().anyMatch(dir::startsWith)) { - return SKIP_SIBLINGS; - } - return CONTINUE; - } - } - - private class JarVisitor extends SimpleFileVisitor { - - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attr) { - if (attr.isRegularFile()) { - PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**.jar"); - if (matcher.matches(file)) { - try { - typeSolver.add(new JarTypeSolver(file.toString())); - } catch (IOException e) { - logger.log(Level.WARNING, "IOException for file " + file, e); - } - } - } - return CONTINUE; - } - } -} - -- cgit v1.2.3 From 5606e3b0255a8f1250734f3a429b381371a3c9f8 Mon Sep 17 00:00:00 2001 From: Daan Schipper Date: Mon, 19 Feb 2018 16:16:18 +0100 Subject: unit test --- .../github/javaparser/symbolsolver/utils/SymbolSolverQuickSetup.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'javaparser-symbol-solver-core/src/main') diff --git a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSolverQuickSetup.java b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSolverQuickSetup.java index a9287d7d7..de6628f2b 100644 --- a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSolverQuickSetup.java +++ b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSolverQuickSetup.java @@ -48,7 +48,7 @@ import static java.nio.file.FileVisitResult.SKIP_SIBLINGS; /** * Utility class to add all jars and roots of java files of the provided path to a TypeSolver instance. - * It traverses the file directory tree and adds all files ending in either .java or .jar. + * It traverses the file directory tree and adds all files ending in either .java or .jar. */ public class SymbolSolverQuickSetup { -- cgit v1.2.3 From d1d92de58ca4fb191ba8cc405505a19adb0284dc Mon Sep 17 00:00:00 2001 From: Daan Schipper Date: Mon, 19 Feb 2018 16:25:14 +0100 Subject: make use of javaparser log framework --- .../symbolsolver/utils/SymbolSolverQuickSetup.java | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) (limited to 'javaparser-symbol-solver-core/src/main') diff --git a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSolverQuickSetup.java b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSolverQuickSetup.java index de6628f2b..29319fb5d 100644 --- a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSolverQuickSetup.java +++ b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSolverQuickSetup.java @@ -24,7 +24,6 @@ package com.github.javaparser.symbolsolver.utils; import com.github.javaparser.JavaParser; import com.github.javaparser.ParseProblemException; import com.github.javaparser.ast.CompilationUnit; -import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade; import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver; import com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver; @@ -39,8 +38,6 @@ import java.nio.file.attribute.BasicFileAttributes; import java.util.HashSet; import java.util.Optional; import java.util.Set; -import java.util.logging.Level; -import java.util.logging.Logger; import static com.github.javaparser.utils.Utils.assertNotNull; import static java.nio.file.FileVisitResult.CONTINUE; @@ -52,8 +49,6 @@ import static java.nio.file.FileVisitResult.SKIP_SIBLINGS; */ public class SymbolSolverQuickSetup { - private static Logger logger = Logger.getLogger(JavaParserFacade.class.getCanonicalName()); - private final Path root; private CombinedTypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(false)); @@ -76,7 +71,7 @@ public class SymbolSolverQuickSetup { try { return Optional.of(walk()); } catch (IOException e) { - logger.log(Level.WARNING, "Unable to walk root " + root, e); + Log.error(e, "Unable to walk root " + root); return Optional.empty(); } } @@ -110,12 +105,12 @@ public class SymbolSolverQuickSetup { if (root.isPresent()) { typeSolver.add(new JavaParserTypeSolver(root.get().toFile())); if (roots.add(root.get())) { - logger.log(Level.FINE, "Added dir " + root.get() + " to the TypeSolver"); + Log.trace("Added dir " + root.get() + " to the TypeSolver"); return SKIP_SIBLINGS; } } } catch (ParseProblemException e) { - logger.log(Level.WARNING, "Unable to parse file " + file, e); + Log.error(e, "Unable to parse file " + file); } } } @@ -134,15 +129,11 @@ public class SymbolSolverQuickSetup { private class JarVisitor extends SimpleFileVisitor { @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attr) { + public FileVisitResult visitFile(Path file, BasicFileAttributes attr) throws IOException { if (attr.isRegularFile()) { PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**.jar"); if (matcher.matches(file)) { - try { - typeSolver.add(new JarTypeSolver(file.toString())); - } catch (IOException e) { - logger.log(Level.WARNING, "IOException for file " + file, e); - } + typeSolver.add(new JarTypeSolver(file.toString())); } } return CONTINUE; -- cgit v1.2.3 From 3402a5ccbb267d30f4ac826b87f90cc6363fecf1 Mon Sep 17 00:00:00 2001 From: Daan Schipper Date: Tue, 27 Feb 2018 13:33:24 +0100 Subject: DirFilter to skip certain directories, and skipping sub tree while walking file tree instead of skipping siblings --- .../symbolsolver/utils/SymbolSolverQuickSetup.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'javaparser-symbol-solver-core/src/main') diff --git a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSolverQuickSetup.java b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSolverQuickSetup.java index 29319fb5d..e6d3d530b 100644 --- a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSolverQuickSetup.java +++ b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/utils/SymbolSolverQuickSetup.java @@ -40,8 +40,7 @@ import java.util.Optional; import java.util.Set; import static com.github.javaparser.utils.Utils.assertNotNull; -import static java.nio.file.FileVisitResult.CONTINUE; -import static java.nio.file.FileVisitResult.SKIP_SIBLINGS; +import static java.nio.file.FileVisitResult.*; /** * Utility class to add all jars and roots of java files of the provided path to a TypeSolver instance. @@ -49,8 +48,13 @@ import static java.nio.file.FileVisitResult.SKIP_SIBLINGS; */ public class SymbolSolverQuickSetup { + public interface DirFilter { + boolean filter(Path path); + } + private final Path root; private CombinedTypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(false)); + private DirFilter dirFilter = path -> false; public SymbolSolverQuickSetup(Path root) { assertNotNull(root); @@ -61,6 +65,11 @@ public class SymbolSolverQuickSetup { Log.info("New symbol source root at \"%s\"", this.root); } + public SymbolSolverQuickSetup(Path root, DirFilter dirFilter) { + this(root); + this.dirFilter = dirFilter; + } + public TypeSolver walk() throws IOException { Files.walkFileTree(root, new JavaSymbolSolverWalker()); Files.walkFileTree(root, new JarVisitor()); @@ -119,8 +128,8 @@ public class SymbolSolverQuickSetup { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { - if (Files.isHidden(dir) || roots.stream().anyMatch(dir::startsWith)) { - return SKIP_SIBLINGS; + if (Files.isHidden(dir) || dirFilter.filter(dir) || roots.stream().anyMatch(dir::startsWith)) { + return SKIP_SUBTREE; } return CONTINUE; } -- cgit v1.2.3