| 1 | /* |
|---|---|
| 2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
| 3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
| 4 | * |
| 5 | * This file is part of JavaParser. |
| 6 | * |
| 7 | * JavaParser can be used either under the terms of |
| 8 | * a) the GNU Lesser General Public License as published by |
| 9 | * the Free Software Foundation, either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * b) the terms of the Apache License |
| 12 | * |
| 13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
| 14 | * LICENCE.APACHE. Please refer to those files for details. |
| 15 | * |
| 16 | * JavaParser is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU Lesser General Public License for more details. |
| 20 | */ |
| 21 | |
| 22 | package com.github.javaparser.utils; |
| 23 | |
| 24 | import com.github.javaparser.ParserConfiguration; |
| 25 | |
| 26 | import java.io.IOException; |
| 27 | import java.nio.file.FileVisitResult; |
| 28 | import java.nio.file.Files; |
| 29 | import java.nio.file.Path; |
| 30 | import java.nio.file.PathMatcher; |
| 31 | import java.nio.file.SimpleFileVisitor; |
| 32 | import java.nio.file.attribute.BasicFileAttributes; |
| 33 | |
| 34 | import static java.nio.file.FileVisitResult.*; |
| 35 | |
| 36 | /** |
| 37 | * A brute force {@link CollectionStrategy} for discovering a project structure. |
| 38 | * It will search through the given project root path for Java files, |
| 39 | * look at their package declarations, and figure out the root directories for those files. |
| 40 | * No project definition files like pom.xml or build.gradle are used. |
| 41 | * This strategy is crude, but can work for many cases. |
| 42 | * Note that any build artifacts will also be detected: jar files in target directories and so on. |
| 43 | * Note that if your project has a module-info.java file, ensure that you have set the language level to at least 9. |
| 44 | */ |
| 45 | public class ParserCollectionStrategy implements CollectionStrategy { |
| 46 | |
| 47 | private final ParserConfiguration parserConfiguration; |
| 48 | |
| 49 | public ParserCollectionStrategy() { |
| 50 | this(new ParserConfiguration()); |
| 51 | } |
| 52 | |
| 53 | public ParserCollectionStrategy(ParserConfiguration parserConfiguration) { |
| 54 | this.parserConfiguration = parserConfiguration; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public ParserConfiguration getParserConfiguration() { |
| 59 | return parserConfiguration; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public ProjectRoot collect(Path path) { |
| 64 | ProjectRoot projectRoot = new ProjectRoot(path, parserConfiguration); |
| 65 | try { |
| 66 | Files.walkFileTree(path, new SimpleFileVisitor<Path>() { |
| 67 | Path current_root; |
| 68 | PathMatcher javaMatcher = getPathMatcher("glob:**.java"); |
| 69 | |
| 70 | @Override |
| 71 | public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
| 72 | if (javaMatcher.matches(file)) { |
| 73 | current_root = getRoot(file).orElse(null); |
| 74 | if (current_root != null) { |
| 75 | return SKIP_SIBLINGS; |
| 76 | } |
| 77 | } |
| 78 | return CONTINUE; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { |
| 83 | if (Files.isHidden(dir) || (current_root != null && dir.startsWith(current_root))) { |
| 84 | return SKIP_SUBTREE; |
| 85 | } |
| 86 | return CONTINUE; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException { |
| 91 | if (current_root != null && Files.isSameFile(dir, current_root)) { |
| 92 | projectRoot.addSourceRoot(dir); |
| 93 | current_root = null; |
| 94 | } |
| 95 | return CONTINUE; |
| 96 | } |
| 97 | }); |
| 98 | } catch (IOException e) { |
| 99 | Log.error(e, "Unable to walk %s", () -> path); |
| 100 | } |
| 101 | return projectRoot; |
| 102 | } |
| 103 | } |
| 104 |
Members