01: /*
02: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tc.util;
06:
07: import java.io.File;
08: import java.io.FileNotFoundException;
09: import java.net.URL;
10: import java.util.ArrayList;
11: import java.util.List;
12: import java.util.regex.Pattern;
13:
14: /**
15: * Translates an array of Class objects to an array of File objects. NOTE: This class only supports a single level of
16: * nested inner classes, i.e. no innerclass of an innerclass
17: */
18: public final class ClassListToFileList {
19:
20: /**
21: * @return [0][] is a list of absolute file paths, [1][] is a list of relative paths starting at the package name
22: * (com)
23: */
24: public static File[][] translate(Class[] classes)
25: throws FileNotFoundException {
26: List files = new ArrayList();
27: List relativeFiles = new ArrayList();
28: File file;
29: File relativeFile;
30: File inner;
31: int offset;
32: for (int i = 0; i < classes.length; i++) {
33: relativeFile = new File(classes[i].getName().replaceAll(
34: "\\.", "/")
35: + ".class");
36: relativeFiles.add(relativeFile);
37: String[] parts = classes[i].getName().split("\\.");
38: URL url = classes[i].getResource(parts[parts.length - 1]
39: + ".class");
40: file = new File(url.getPath());
41: files.add(file);
42: offset = file.toString().length()
43: - relativeFile.toString().length();
44: if (!file.exists())
45: throw new FileNotFoundException(
46: "Unable to resolve class file location for: "
47: + classes[i]);
48: String[] packageFiles = file.getParentFile().list();
49: for (int j = 0; j < packageFiles.length; j++) {
50: if (Pattern.matches(file.getName().replaceFirst(
51: "\\.class", "")
52: + "(\\$.*)\\.class", packageFiles[j])) {
53: inner = new File(file.getParent() + File.separator
54: + packageFiles[j]);
55: files.add(inner);
56: relativeFiles.add(new File(inner.toString()
57: .substring(offset)));
58: }
59: }
60: }
61: File[][] retVal = new File[2][];
62: retVal[0] = (File[]) files.toArray(new File[0]);
63: retVal[1] = (File[]) relativeFiles.toArray(new File[0]);
64: return retVal;
65: }
66: }
|