1/**
2 * Copyright 2007 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.tonicsystems.jarjar;
18
19import com.tonicsystems.jarjar.util.*;
20import java.io.*;
21import java.util.*;
22import java.util.zip.ZipEntry;
23import org.objectweb.asm.ClassReader;
24import org.objectweb.asm.ClassVisitor;
25
26public class DepFind
27{
28    private File curDir = new File(System.getProperty("user.dir"));
29
30    public void setCurrentDirectory(File curDir) {
31        this.curDir = curDir;
32    }
33
34    public void run(String from, String to, DepHandler handler) throws IOException {
35        try {
36            ClassHeaderReader header = new ClassHeaderReader();
37            Map<String, String> classes = new HashMap<String, String>();
38            ClassPathIterator cp = new ClassPathIterator(curDir, to, null);
39            try {
40              while (cp.hasNext()) {
41                ClassPathEntry entry = cp.next();
42                InputStream in = entry.openStream();
43                try {
44                  header.read(in);
45                  classes.put(header.getClassName(), entry.getSource());
46                } catch (Exception e) {
47                  System.err.println("Error reading " + entry.getName() + ": " + e.getMessage());
48                } finally {
49                  in.close();
50                }
51              }
52            } finally {
53              cp.close();
54            }
55
56            handler.handleStart();
57            cp = new ClassPathIterator(curDir, from, null);
58            try {
59              while (cp.hasNext()) {
60                ClassPathEntry entry = cp.next();
61                InputStream in = entry.openStream();
62                try {
63                  new ClassReader(in).accept(
64                      new DepFindVisitor(classes, entry.getSource(), handler),
65                      ClassReader.SKIP_DEBUG);
66                } catch (Exception e) {
67                  System.err.println("Error reading " + entry.getName() + ": " + e.getMessage());
68                } finally {
69                  in.close();
70                }
71              }
72            } finally {
73              cp.close();
74            }
75            handler.handleEnd();
76        } catch (RuntimeIOException e) {
77            throw (IOException)e.getCause();
78        }
79    }
80}
81