如何使用depth-first-traverse从jar获取类

时间:2013-11-01 08:50:52

标签: java algorithm data-structures jar tree

解决方案(Get list of classes in packageHow do I list the files inside a JAR file?Listing the files in a directory of the current JAR file)全部使用jarEntry列出jar文件中的类,但只能使用jarEntry作为水平遍历。当我想使用depth-first-traverse遍历jar文件中的类和文件夹时,我该怎么办?谢谢你的考虑。

固定

ENV

jdk 1.6

受审

添加到树

static void getClasses(File pareFile, TJtree root){
      TJtree parent, temp;
      parent = root;

    ArrayList<String> asArrayList = new ArrayList<String>();

    File f = pareFile;
    if(f.exists()){
        try{
            JarInputStream jarFile = new JarInputStream(new FileInputStream(f));

            JarEntry jarEntry;
            String [] jarEntryString;
            String jarNodeNameString;

            while(true) {
                jarEntry=jarFile.getNextJarEntry();  

                if(jarEntry == null){
                    break;
                }

                asArrayList.add(jarEntry.getName());
                Collections.sort(asArrayList);

                jarEntryString = jarEntry.getName().split("/");
                jarNodeNameString = jarEntryString[jarEntryString.length-1];

                if(jarEntry.isDirectory()) {
                    temp = new TJtree();
                    temp.data = jarNodeNameString;
                    parent.childlist.add(temp);
                    parent = temp;
                } else {
                    if(jarEntry.getName().endsWith (".class")) {
                        temp = new TJtree();
                        temp.data = jarNodeNameString;
                        parent.childlist.add(temp);

                    }
                }
          }
                for (String tmpString : asArrayList) {
                    System.out.println("++"+tmpString);
                }
       } catch( Exception e){
            e.printStackTrace ();
       }
    }
}

显示树

  static void displaytree2(TJtree f , int level) {  

        String preStr = "";  
        for(int i=0; i<level; i++) {  
         preStr += "    ";  
        }  

        for(int i=0; i<f.childlist.size(); i++) {  
            TJtree t = f.childlist.get(i);  
            System.out.println(preStr + "-"+t.data);  

         if(! t.childlist.isEmpty()) {  
             displaytree2(t, level + 1);  
         }  
        }  
   } 

输出

-org.objectweb.asm_3.3.1.v201105211655.jar
    -META-INF
        -about_files
            -org
                -objectweb
                    -asm
                        -commons
                            -signature
                                -tree
                                    -analysis
                                        -util
                                            -xml
                                                -AnnotationVisitor.class
                                                -AnnotationWriter.class
                                                -Attribute.class
                                                -ByteVector.class
                                                -ClassAdapter.class
                                                -ClassReader.class
                                                -ClassVisitor.class
                                                -ClassWriter.class
                                                -Edge.class
                                                -FieldVisitor.class
                                                -FieldWriter.class
                                                -Frame.class
                                                -Handler.class
                                                -Item.class
                                                -Label.class
                                                -MethodAdapter.class
                                                -MethodVisitor.class
                                                -MethodWriter.class
                                                -Opcodes.class
                                                -Type.class
                                                -AdviceAdapter.class
                                                -AnalyzerAdapter.class
                                                -CodeSizeEvaluator.class
                                                -EmptyVisitor.class
                                                -GeneratorAdapter.class
                                                -InstructionAdapter.class
                                                -JSRInlinerAdapter$Instantiation.class
                                                -JSRInlinerAdapter$Subroutine.class
                                                -JSRInlinerAdapter.class
                                                -LocalVariablesSorter.class
                                                -Method.class
                                                -Remapper.class
                                                -RemappingAnnotationAdapter.class
                                                -RemappingClassAdapter.class
                                                -RemappingFieldAdapter.class
                                                -RemappingMethodAdapter.class
                                                -RemappingSignatureAdapter.class
                                                -SerialVersionUIDAdder$Item.class
                                                -SerialVersionUIDAdder.class
                                                -SimpleRemapper.class
                                                -StaticInitMerger.class
                                                -TableSwitchGenerator.class
                                                -TryCatchBlockSorter$1.class
                                                -TryCatchBlockSorter.class
                                                -SignatureReader.class
                                                -SignatureVisitor.class
                                                -SignatureWriter.class
                                                -AbstractInsnNode.class
                                                -AnnotationNode.class
                                                -ClassNode.class
                                                -FieldInsnNode.class
                                                -FieldNode.class
                                                -FrameNode.class
                                                -IincInsnNode.class
                                                -InnerClassNode.class
                                                -InsnList$InsnListIterator.class
                                                -InsnList.class
                                                -InsnNode.class
                                                -IntInsnNode.class
                                                -JumpInsnNode.class
                                                -LabelNode.class
                                                -LdcInsnNode.class
                                                -LineNumberNode.class
                                                -LocalVariableNode.class
                                                -LookupSwitchInsnNode.class
                                                -MemberNode.class
                                                -MethodInsnNode.class
                                                -MethodNode$1.class
                                                -MethodNode.class
                                                -MultiANewArrayInsnNode.class
                                                -TableSwitchInsnNode.class
                                                -TryCatchBlockNode.class
                                                -TypeInsnNode.class
                                                -VarInsnNode.class
                                                -Analyzer.class
                                                -AnalyzerException.class
                                                -BasicInterpreter.class
                                                -BasicValue.class
                                                -BasicVerifier.class
                                                -Frame.class
                                                -Interpreter.class
                                                -SimpleVerifier.class
                                                -SmallSet.class
                                                -SourceInterpreter.class
                                                -SourceValue.class
                                                -Subroutine.class
                                                -Value.class
                                                -ASMifiable.class
                                                -ASMifierAbstractVisitor.class
                                                -ASMifierAnnotationVisitor.class
                                                -ASMifierClassVisitor.class
                                                -ASMifierFieldVisitor.class
                                                -ASMifierMethodVisitor.class
                                                -AbstractVisitor.class
                                                -CheckAnnotationAdapter.class
                                                -CheckClassAdapter.class
                                                -CheckFieldAdapter.class
                                                -CheckMethodAdapter$1.class
                                                -CheckMethodAdapter.class
                                                -CheckSignatureAdapter.class
                                                -TraceAbstractVisitor.class
                                                -TraceAnnotationVisitor.class
                                                -TraceClassVisitor.class
                                                -TraceFieldVisitor.class
                                                -TraceMethodVisitor.class
                                                -TraceSignatureVisitor.class
                                                -Traceable.class
                                                -ASMContentHandler$AnnotationDefaultRule.class
                                                -ASMContentHandler$AnnotationParameterRule.class
                                                -ASMContentHandler$AnnotationRule.class
                                                -ASMContentHandler$AnnotationValueAnnotationRule.class
                                                -ASMContentHandler$AnnotationValueArrayRule.class
                                                -ASMContentHandler$AnnotationValueEnumRule.class
                                                -ASMContentHandler$AnnotationValueRule.class
                                                -ASMContentHandler$ClassRule.class
                                                -ASMContentHandler$ExceptionRule.class
                                                -ASMContentHandler$ExceptionsRule.class
                                                -ASMContentHandler$FieldRule.class
                                                -ASMContentHandler$FrameRule.class
                                                -ASMContentHandler$FrameTypeRule.class
                                                -ASMContentHandler$InnerClassRule.class
                                                -ASMContentHandler$InterfaceRule.class
                                                -ASMContentHandler$InterfacesRule.class
                                                -ASMContentHandler$LabelRule.class
                                                -ASMContentHandler$LineNumberRule.class
                                                -ASMContentHandler$LocalVarRule.class
                                                -ASMContentHandler$LookupSwitchLabelRule.class
                                                -ASMContentHandler$LookupSwitchRule.class
                                                -ASMContentHandler$MaxRule.class
                                                -ASMContentHandler$MethodRule.class
                                                -ASMContentHandler$Opcode.class
                                                -ASMContentHandler$OpcodeGroup.class
                                                -ASMContentHandler$OpcodesRule.class
                                                -ASMContentHandler$OuterClassRule.class
                                                -ASMContentHandler$Rule.class
                                                -ASMContentHandler$RuleSet.class
                                                -ASMContentHandler$SourceRule.class
                                                -ASMContentHandler$TableSwitchLabelRule.class
                                                -ASMContentHandler$TableSwitchRule.class
                                                -ASMContentHandler$TryCatchRule.class
                                                -ASMContentHandler.class
                                                -Processor$ASMContentHandlerFactory.class
                                                -Processor$ContentHandlerFactory.class
                                                -Processor$EntryElement.class
                                                -Processor$InputSlicingHandler.class
                                                -Processor$OutputSlicingHandler.class
                                                -Processor$ProtectedInputStream.class
                                                -Processor$SAXWriter.class
                                                -Processor$SAXWriterFactory.class
                                                -Processor$SingleDocElement.class
                                                -Processor$SubdocumentHandlerFactory.class
                                                -Processor$TransformerHandlerFactory.class
                                                -Processor$ZipEntryElement.class
                                                -Processor.class
                                                -SAXAdapter.class
                                                -SAXAnnotationAdapter.class
                                                -SAXClassAdapter.class
                                                -SAXCodeAdapter.class
                                                -SAXFieldAdapter.class

预期输入

如果输入jar文件的路径(例如org.objectweb.asm_3.3.1.v201105211655.jar)

预期输出

输出应该是文件结构(最好只包括包含类文件的类文件和文件夹。)。

D:\WORK\WORKSPACE\MYJAR\CC\ORG.OBJECTWEB.ASM_3.3.1.V201105211655
│  about.html
│  plugin.properties
│
├─about_files
│      LICENSE.txt
│      README.txt
│
├─META-INF
│      eclipse.inf
│      ECLIPSEF.RSA
│      ECLIPSEF.SF
│      MANIFEST.MF
│
└─org
    └─objectweb
        └─asm
            │  AnnotationVisitor.class
            │  AnnotationWriter.class
            │  Attribute.class
            │  ByteVector.class
            │  ClassAdapter.class
            │  ClassReader.class
            │  ClassVisitor.class
            │  ClassWriter.class
            │  Edge.class
            │  FieldVisitor.class
            │  FieldWriter.class
            │  Frame.class
            │  Handler.class
            │  Item.class
            │  Label.class
            │  MethodAdapter.class
            │  MethodVisitor.class
            │  MethodWriter.class
            │  Opcodes.class
            │  Type.class
            │
            ├─commons
            │      AdviceAdapter.class
            │      AnalyzerAdapter.class
            │      CodeSizeEvaluator.class
            │      EmptyVisitor.class
            │      GeneratorAdapter.class
            │      InstructionAdapter.class
            │      JSRInlinerAdapter$Instantiation.class
            │      JSRInlinerAdapter$Subroutine.class
            │      JSRInlinerAdapter.class
            │      LocalVariablesSorter.class
            │      Method.class
            │      Remapper.class
            │      RemappingAnnotationAdapter.class
            │      RemappingClassAdapter.class
            │      RemappingFieldAdapter.class
            │      RemappingMethodAdapter.class
            │      RemappingSignatureAdapter.class
            │      SerialVersionUIDAdder$Item.class
            │      SerialVersionUIDAdder.class
            │      SimpleRemapper.class
            │      StaticInitMerger.class
            │      TableSwitchGenerator.class
            │      TryCatchBlockSorter$1.class
            │      TryCatchBlockSorter.class
            │
            ├─signature
            │      SignatureReader.class
            │      SignatureVisitor.class
            │      SignatureWriter.class
            │
            ├─tree
            │  │  AbstractInsnNode.class
            │  │  AnnotationNode.class
            │  │  ClassNode.class
            │  │  FieldInsnNode.class
            │  │  FieldNode.class
            │  │  FrameNode.class
            │  │  IincInsnNode.class
            │  │  InnerClassNode.class
            │  │  InsnList$InsnListIterator.class
            │  │  InsnList.class
            │  │  InsnNode.class
            │  │  IntInsnNode.class
            │  │  JumpInsnNode.class
            │  │  LabelNode.class
            │  │  LdcInsnNode.class
            │  │  LineNumberNode.class
            │  │  LocalVariableNode.class
            │  │  LookupSwitchInsnNode.class
            │  │  MemberNode.class
            │  │  MethodInsnNode.class
            │  │  MethodNode$1.class
            │  │  MethodNode.class
            │  │  MultiANewArrayInsnNode.class
            │  │  TableSwitchInsnNode.class
            │  │  TryCatchBlockNode.class
            │  │  TypeInsnNode.class
            │  │  VarInsnNode.class
            │  │
            │  └─analysis
            │          Analyzer.class
            │          AnalyzerException.class
            │          BasicInterpreter.class
            │          BasicValue.class
            │          BasicVerifier.class
            │          Frame.class
            │          Interpreter.class
            │          SimpleVerifier.class
            │          SmallSet.class
            │          SourceInterpreter.class
            │          SourceValue.class
            │          Subroutine.class
            │          Value.class
            │
            ├─util
            │      AbstractVisitor.class
            │      ASMifiable.class
            │      ASMifierAbstractVisitor.class
            │      ASMifierAnnotationVisitor.class
            │      ASMifierClassVisitor.class
            │      ASMifierFieldVisitor.class
            │      ASMifierMethodVisitor.class
            │      CheckAnnotationAdapter.class
            │      CheckClassAdapter.class
            │      CheckFieldAdapter.class
            │      CheckMethodAdapter$1.class
            │      CheckMethodAdapter.class
            │      CheckSignatureAdapter.class
            │      Traceable.class
            │      TraceAbstractVisitor.class
            │      TraceAnnotationVisitor.class
            │      TraceClassVisitor.class
            │      TraceFieldVisitor.class
            │      TraceMethodVisitor.class
            │      TraceSignatureVisitor.class
            │
            └─xml
                    ASMContentHandler$AnnotationDefaultRule.class
                    ASMContentHandler$AnnotationParameterRule.class
                    ASMContentHandler$AnnotationRule.class
                    ASMContentHandler$AnnotationValueAnnotationRule.class
                    ASMContentHandler$AnnotationValueArrayRule.class
                    ASMContentHandler$AnnotationValueEnumRule.class
                    ASMContentHandler$AnnotationValueRule.class
                    ASMContentHandler$ClassRule.class
                    ASMContentHandler$ExceptionRule.class
                    ASMContentHandler$ExceptionsRule.class
                    ASMContentHandler$FieldRule.class
                    ASMContentHandler$FrameRule.class
                    ASMContentHandler$FrameTypeRule.class
                    ASMContentHandler$InnerClassRule.class
                    ASMContentHandler$InterfaceRule.class
                    ASMContentHandler$InterfacesRule.class
                    ASMContentHandler$LabelRule.class
                    ASMContentHandler$LineNumberRule.class
                    ASMContentHandler$LocalVarRule.class
                    ASMContentHandler$LookupSwitchLabelRule.class
                    ASMContentHandler$LookupSwitchRule.class
                    ASMContentHandler$MaxRule.class
                    ASMContentHandler$MethodRule.class
                    ASMContentHandler$Opcode.class
                    ASMContentHandler$OpcodeGroup.class
                    ASMContentHandler$OpcodesRule.class
                    ASMContentHandler$OuterClassRule.class
                    ASMContentHandler$Rule.class
                    ASMContentHandler$RuleSet.class
                    ASMContentHandler$SourceRule.class
                    ASMContentHandler$TableSwitchLabelRule.class
                    ASMContentHandler$TableSwitchRule.class
                    ASMContentHandler$TryCatchRule.class
                    ASMContentHandler.class
                    Processor$ASMContentHandlerFactory.class
                    Processor$ContentHandlerFactory.class
                    Processor$EntryElement.class
                    Processor$InputSlicingHandler.class
                    Processor$OutputSlicingHandler.class
                    Processor$ProtectedInputStream.class
                    Processor$SAXWriter.class
                    Processor$SAXWriterFactory.class
                    Processor$SingleDocElement.class
                    Processor$SubdocumentHandlerFactory.class
                    Processor$TransformerHandlerFactory.class
                    Processor$ZipEntryElement.class
                    Processor.class
                    SAXAdapter.class
                    SAXAnnotationAdapter.class
                    SAXClassAdapter.class
                    SAXCodeAdapter.class
                    SAXFieldAdapter.class

修改

我使用的结构,我想存储结果:

    class {
    String data;  
    TJtree parent;   //treenode
    LinkedList<TJtree> childlist; 
    }

1 个答案:

答案 0 :(得分:1)

按照出现的顺序阅读条目。使用完整路径名将它们添加到TreeMap。这将对它们进行排序并按照您想要的方式排列它们。要显示为树,请遍历完整路径名,以查看目录何时更改并正确显示。

e.g。

final NavigableMap<String, MyData> entryMap = new TreeMap<>();

// add each entry by full path to the entryMap
entryMap.put(fullpath, new MyData(... ... ...));

// later process the entries in the order required
File lastDirectory = new File("/");
for(Map.Enrty<String, MyData> entry : entryMap.entrySet()) {
   File file = new File(entry.getKey());
   MyData myData = entry.getValue();
   if (!file.getParent().equals(lastDirectory.getParent()) {
        // different directory
   }