View Javadoc
1   /*
2    * Copyright (C) 2015 Uwe Plonus
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  package org.sw4j.apisniffer;
18  
19  import java.io.File;
20  import java.io.FileFilter;
21  import java.io.FileInputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import javax.annotation.Nonnull;
25  import javax.annotation.concurrent.NotThreadSafe;
26  import org.objectweb.asm.ClassReader;
27  import org.sw4j.apisniffer.api.Api;
28  import org.sw4j.apisniffer.builder.ApiBuilder;
29  import org.sw4j.apisniffer.visitor.ApiClassVisitor;
30  
31  /**
32   *
33   * @author Uwe Plonus &lt;u.plonus@gmail.com&gt;
34   */
35  @NotThreadSafe
36  public class ApiScanner {
37  
38      private ApiBuilder apiBuilder;
39  
40      public void scanDirectory(@Nonnull final File folder) throws IOException {
41          apiBuilder = new ApiBuilder();
42          if (!folder.getAbsoluteFile().isDirectory()) {
43              throw new IllegalArgumentException(
44                  new StringBuilder("The method scanFolder(File) is for")
45                      .append(" scanning folders. The provided file \"")
46                      .append(folder.getAbsolutePath())
47                      .append("\" is no folder.")
48                      .toString());
49          }
50          File[] files = folder.getAbsoluteFile().listFiles(new DirectoryOrClassFileFilter());
51          if (files != null) {
52              for (File file: files) {
53                  if (file.isDirectory()) {
54                      scanDirectory(file);
55                  } else {
56                      scanClass(new FileInputStream(file));
57                  }
58              }
59          }
60      }
61  
62      private void scanClass(@Nonnull final InputStream classFile) throws IOException {
63          ClassReader cr = new ClassReader(classFile);
64          ApiClassVisitor cv = new ApiClassVisitor(apiBuilder);
65          cr.accept(cv, 0);
66      }
67  
68      public void scanJar(@Nonnull final InputStream is) throws IOException {
69          apiBuilder = new ApiBuilder();
70      }
71  
72      /**
73       * Creates the API from the {@link #scanJar(java.io.InputStream) scanned jar file} or the
74       * {@link #scanDirectory(java.io.File) scanned directory}.
75       *
76       * @return the API from the scanned files.
77       */
78      public Api createApi() {
79          return null;
80      }
81  
82  
83      /**
84       * A file filter to accept only directories or files with the suffix {@code .class}.
85       */
86      private static final class DirectoryOrClassFileFilter
87      implements FileFilter {
88  
89          /**
90           * Accepts only pathnames if it either is a directory or has the suffix {@code .class}.
91           *
92           * @param pathname the pathname to check.
93           * @return {@code true} if the pathname is either a directory or a file with suffix
94           *  {@code .class}.
95           */
96          @Override
97          public boolean accept(final File pathname) {
98              return pathname.isDirectory() ||
99                  pathname.getName().endsWith(".class");
100         }
101 
102     }
103 
104 }