疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

jar包静态依赖解析


 

   

  最近有个点子,做一个jar包的静态依赖解析,将一个文件夹内的所有jar包进行分析,查询某一个jar包静态依赖的其他jar包。

  从网上搜一下资料,发现有一个项目已经完成了jar包静态依赖的解析工作,在这个基础上,做一些代码的处理,得到自己想要的结果。话不多说,直接上代码~~~

  1、代码一:文件夹下的静态依赖关系解析结果获取:

  package cn.letterme.app;

  import java.io.File;

  import java.util.ArrayList;

  import java.util.Iterator;

  import java.util.List;

  import com.kirkk.analyzer.framework.Jar;

  import com.kirkk.analyzer.framework.bcelbundle.JarCollectionImpl;

  import com.kirkk.analyzer.framework.bcelbundle.JarRelationshipDecorator;

  /**

  * 目录下的jar包依赖关系解析

  * @author zzz@letterme.cn

  * @date 2015年1月25日

  * @version V1.0.0 beta

  */

  public class JarAnalysier

  {

  /**

  * 解析

  * @param JarDir jar包目录

  * @return

  * @throws Exception

  */

  public List createSummary(File JarDir) throws Exception

  {

  JarRelationshipDecorator jarRlDec = new JarRelationshipDecorator(new JarCollectionImpl(JarDir,

  UserConfiguration.ignorePackages, UserConfiguration.ignoreJars));

  Jar[] arrayOfJar = jarRlDec.toArray();

  return outputAll(arrayOfJar);

  }

  /**

  * 解析

  * @param JarDir jar包目录

  * @param ignorePackages 忽略的包名

  * @param ignoreJars 忽略的jar包名

  * @return

  * @throws Exception

  */

  public List createSummary(File JarDir, String ignorePackages, String ignoreJars) throws Exception

  {

  UserConfiguration.initialize(ignorePackages, ignoreJars);

  return createSummary(JarDir);

  }

  /**

  * 将结果输出为List

  * @param paramArrayOfJar

  * @return

  */

  private List outputAll(Jar[] paramArrayOfJar)

  {

  List output = new ArrayList();

  for (int i = 0; i < paramArrayOfJar.length; i++)

  {

  String str = paramArrayOfJar[i].getJarFileName().substring(

  paramArrayOfJar[i].getJarFileName().lastIndexOf("\") + 1,

  paramArrayOfJar[i].getJarFileName().length());

  // TODO

  System.out.println("Jar Name = " + str);

  output.addAll(externalJars(paramArrayOfJar[i]));

  }

  return output;

  }

  /**

  * 获取依赖

  * @param paramJar

  * @return

  */

  private List externalJars(Jar paramJar)

  {

  List out = new ArrayList();

  Iterator it = paramJar.getOutgoingDependencies().iterator();

  String outStr = "";

  while (it.hasNext())

  {

  String str1 = paramJar.getJarFileName().replaceAll("[-.]", "_");

  Jar jar = (Jar) it.next();

  String str2 = jar.getJarFileName().replaceAll("[-.]", "_");

  outStr = str1.substring(0, str1.length() - 4) + " -> " + str2.substring(0, str2.length() - 4) + ";";

  out.add(outStr);

  }

  return out;

  }

  /**

  * Test Main

  * @param args

  */

  public static void main(String[] args)

  {

  File JarDir = new File("D:\3rdParty");

  JarAnalysier an = new JarAnalysier();

  try

  {

  an.createSummary(JarDir, "", "java.*;javaw.*");

  }

  catch (Exception e)

  {

  e.printStackTrace();

  }

  }

  }

  2、代码二:配置过滤的jar包和过滤的包名:

  package cn.letterme.app;

  import java.util.ArrayList;

  import java.util.List;

  import java.util.ResourceBundle;

  import java.util.StringTokenizer;

  /**

  * 自定义配置

  * @author zzz@letterme.cn

  * @date 2015年1月25日

  * @version V1.0.0 beta

  */

  public class UserConfiguration

  {

  public static List ignorePackages = null;

  public static List ignoreJars = null;

  /**

  * 初始化配置

  * @param igPackages

  * @param igJars

  */

  public static void initialize(String igPackages, String igJars)

  {

  StringTokenizer strTokenizer;

  String str = "";

  if (igPackages != null)

  {

  ignorePackages = new ArrayList();

  strTokenizer = new StringTokenizer(igPackages, ";");

  while (strTokenizer.hasMoreTokens())

  {

  str = strTokenizer.nextToken();

  ignorePackages.add(str.substring(0, str.length() - 1));

  }

  }

  if (igJars != null)

  {

  ignoreJars = new ArrayList();

  strTokenizer = new StringTokenizer(igJars, ";");

  while (strTokenizer.hasMoreTokens())

  {

  str = strTokenizer.nextToken();

  ignoreJars.add(str.substring(0, str.length()));

  }

  }

  }

  /**

  * 静态块,获取默认的过滤配置,如果获取不到则不处理

  * 默认的过滤对jdk的源码进行过滤

  */

  static

  {

  try

  {

  String str1, str2, str3, str4;

  ResourceBundle resBundle = ResourceBundle.getBundle("cn.letterme.config.Filter");

  str1 = resBundle.getString("filter.packages");

  ignorePackages = new ArrayList();

  StringTokenizer strTokenizer1 = new StringTokenizer(str1, ";");

  while (strTokenizer1.hasMoreTokens())

  {

  str2 = strTokenizer1.nextToken();

  ignorePackages.add(str2.substring(0, str2.length() - 1));

  }

  str3 = resBundle.getString("filter.jars");

  ignoreJars = new ArrayList();

  StringTokenizer strTokenizer2 = new StringTokenizer(str3, ";");

  while (strTokenizer2.hasMoreTokens())

  {

  str4 = strTokenizer2.nextToken();

  ignoreJars.add(str4.substring(0, str4.length()));

  }

  }

  catch (Exception e)

  {

  e.printStackTrace();

  if (null == ignorePackages)

  {

  ignorePackages = new ArrayList();

  }

  if (null == ignoreJars)

  {

  ignoreJars = new ArrayList();

  }

  }

  }

  }

  3、代码三:静态依赖结果分析:

  package cn.letterme.app;

  import java.io.File;

  import java.util.ArrayList;

  import java.util.HashMap;

  import java.util.LinkedHashMap;

  import java.util.LinkedHashSet;

  import java.util.LinkedList;

  import java.util.List;

  import java.util.Map;

  /**

  * 基于JarAnalyzer生成结果

  * @author zzz@letterme.cn

  * @date 2015年1月25日

  * @version V1.0.0 beta

  */

  public class GetMinJar

  {

  private HashMap

  private LinkedHashMap

  /**

  * 根据root获取的依赖结果

  */

  private List dependencies = new ArrayList();

  /**

  * root jar

  */

  private String root = "";

  /**

  * 分析目录下的jar包关系

  * @param jarDir

  * @return

  */

  public List analyseJars(String jarDir)

  {

  if (null == jarDir || "".equals(jarDir.trim()))

  {

  return null;

  }

  File dir = new File(jarDir);

  if (dir.isFile() || !dir.exists())

  {

  return null;

  }

  try

  {

  return new JarAnalysier().createSummary(dir);

  }

  catch (Exception e)

  {

  e.printStackTrace();

  }

  return null;

  }

  /**

  * 根据root jar获取依赖结果

  * @param rootJar

  * @param jars

  */

  public void getDependency(String rootJar, List jars)

  {

  if (null == rootJar || "".equals(rootJar.trim()))

  {

  return;

  }

  root = rootJar.replaceAll("[-.]", "_").substring(0, rootJar.length() - 4);

  if (null == jars)

  {

  return;

  }

  LinkedHashSet val = null;

  for (String line : jars)

  {

  String[] datas = line.split("->|;");

  if (datas.length > 1)

  {

  String key = datas[0].trim();

  val = lines.get(key);

  if (val != null)

  {

  val.add(datas[1].trim());

  }

  else

  {

  LinkedHashSet set = new LinkedHashSet();

  set.add(datas[1].trim());

  lines.put(datas[0].trim(), set);

  }

  }

  }

  // 循环获取结果

  LinkedList queue = new LinkedList();

  queue.add(root);

  LinkedHashSet tmp = null;

  while (!queue.isEmpty())

  {

  String data = queue.poll();

  val = lines.get(data);

  if (val != null)

  {

  for (String str : val)

  {

  results.put(str, "");

  // 检测是否递归死循环

  tmp = lines.get(str);

  if (tmp != null)

  {

  if (tmp.contains(data))

  {

  continue;

  }

  }

  queue.add(str);

  }

  }

  }

  for (Map.Entry

  {

  String key = entry.getKey();

  key = key.replaceAll("_", "-");

  dependencies.add(key + ".jar");

  }

  }

  /**

  * 打印依赖结果

  */

  public void print()

  {

  System.out.println(root + ".jar 依赖于" + dependencies.size() + "个jar包===========");

  for (String str : dependencies)

  {

  System.out.println(str);

  }

  }

  /**

  * 获取结果

  * @return

  */

  public List getResult()

  {

  return dependencies;

  }

  /**

  * Test Main

  * @param args

  */

  public static void main(String[] args)

  {

  GetMinJar min = new GetMinJar();

  List jars = min.analyseJars("D:\3rdParty");

  min.getDependency("autoupdate-client.jar", jars);

  min.print();

  }

  }

  P.S.依赖的jar包有:

  bcel-5.2.jar

  jakarta-regexp-1.3.jar

  jaranalyzer-1.2.jar