ããã¯Java Advent Calendarã®2æ¥ç®ã®è¨äºã§ãã
ãã¦ãJavaã§éçºããã¦ããã¨ãã¤ã®éã«ãã©ãããã使ç¨ããã¦ããªãã¯ã©ã¹ã¨ãããã®ãåºã¦ãã¦ãã¾ãã¾ãã
ãªãã¡ã¯ã¿ãªã³ã°ãä»æ§å¤æ´ã®çµæãå¼ã³åºããã¦ãªããªã£ãã¯ã©ã¹ã§ãã
ããããã©ãããã使ç¨ããã¦ãªãã¯ã©ã¹ãä¸è¦§åãã¦ã¿ã¾ãã
ã¾ãã使ç¨ããã¦ããã¯ã©ã¹ãä¸è¦§åãã¾ãã
ä¸è¦§åã«ã¯jdepsã使ç¨ãã¾ãã
jdepsã®æ¦è¦ã«ã¤ãã¦ã¯CLOVERの記事ãåç §ãã¦ãã ããã
CLOVERã®è¨äºã§ã¯classãããã¯jarãæå®ãã¦ãã¾ãããå®ã¯ããã±ã¼ã¸(ãã©ã«ã)ãæå®ãããã¨ã§ããã®ä¸ã®ã¯ã©ã¹ã®ä¾åå ããã¹ã¦åºåãã¦ããã¾ãã
使ç¨ããã¦ããã¯ã©ã¹ã®ä¸è¦§ãåºæ¥ãããå
¨ä½ã®ã¯ã©ã¹ã®ä¸è¦§ããå¼ãç®ãã¦ããã¾ãã
package 配下のクラス一覧を取得する方法はいろいろあるããã§ãããä»åã¯Guavaã®ClassPathã使ç¨ãã¾ããã
ã¨ãããã¨ã§ã以ä¸ã®ãããªæãã§ãã
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.Charset; import java.nio.file.Paths; import java.util.Set; import java.util.stream.Collectors; import com.google.common.reflect.ClassPath; public class SelectNotUsedClass { public static void main(String[] args) throws IOException { // buildã¯ã³ã³ãã¤ã«ãããã¯ã©ã¹ã®åºåå Process process = new ProcessBuilder("jdeps", "-v", Paths.get("build").toAbsolutePath().toString()).start(); //Charsetã¯åç°å¢ã«åããã¦ãã ããã Set<String> dependencies = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.forName("Windows-31J"))).lines().map(s -> s.trim()) .filter(s -> s.contains("->")).map(s -> s.substring(s.indexOf("->"))).map(s -> s.replace("->", "").trim().replaceAll("\\s+.*$", "")) .collect(Collectors.toSet()); ClassLoader loader = Thread.currentThread().getContextClassLoader(); // jp.hoge.xxxxã¯ä¾åé¢ä¿ã調ã¹ããããã±ã¼ã¸ã®åå ClassPath.from(loader).getTopLevelClassesRecursive("jp.hoge.xxxx").stream().map(info -> info.getName()).filter(c -> !dependencies.contains(c)) .forEach(System.out::println); } }
jdepsãåºãã¾ã§ã¯.javaãã¡ã¤ã«ã®importè¡ã解æãã¦åºãã¦ãããã¾ããããããã¶ãã¨æ¥½ã«ãªãã¾ãããã
-
-
- -
-
ãã³ã¡ã§
> ããæ®éã«ä¾¿å©ãããã¡ãã£ã¨æ°ã«ãªã£ããã ãã©ãã¹ãããã¯åç
§ããã¦ããã©ãããã¯ãã³ã¼ãããã¯ä¸ååç
§ããã¦ãªãã®ã£ã¦æ¤åºã§ããã®ããããï¼
ã¨ããã®ãããã¾ãããã
ãã¹ãã¯ã©ã¹ã¨ãããã¯ãã³ã¼ãã¯å¥ç®¡çããã¦ããã¯ããªã®ã§ããããã¯ãã³ã¼ãã®ã¿ãã³ã³ãã¤ã«ããä¸ã§(ãã¹ãã³ã¼ãã¯åãé¤ãã¦)å®è¡ããã°ãã¹ãããã®ã¿åç
§ããã¦ãããã®ãåãé¤ããã¨ãåºæ¥ã¾ãã
ãã®å¾ãããã¤ãã®ãã¹ãã³ã¼ããã³ã³ãã¤ã«ã¨ã©ã¼ã«ãªãã®ã§ããã¡ãã¯åå¥ã§åé¤ãã¦ãã ããã