Commit e53706ab authored by Jesper Öqvist's avatar Jesper Öqvist
Browse files

Refactoring: moved PathPart to separate jadd file

parent 6cd66864
2013-01-23 Jesper Öqvist <jesper.oqvist@cs.lth.se>
* Moved PathPart and subclasses to separate .jadd file in Java1.4Frontend
2013-01-10 Jesper Öqvist <jesper.oqvist@cs.lth.se>
* Fixed error in Java7Frontend/JavaPrettyPrinter.
......
......@@ -294,288 +294,6 @@ aspect ClassPath {
pathPart.program = this;
}
class PathPart {
public InputStream is;
protected String pathName;
protected String relativeName;
protected String fullName;
long age;
Program program;
protected PathPart() {
}
protected boolean isSource;
protected String fileSuffix() {
return isSource ? ".java" : ".class";
}
public static PathPart createSourcePath(String fileName, Program program) {
PathPart p = createPathPart(fileName);
if(p != null) {
p.isSource = true;
p.program = program;
}
return p;
}
public static PathPart createClassPath(String fileName, Program program) {
PathPart p = createPathPart(fileName);
if(p != null) {
p.isSource = false;
p.program = program;
}
return p;
}
private static PathPart createPathPart(String s) {
try {
File f = new File(s);
if(f.isDirectory())
return new FolderPart(f);
else if(f.isFile())
return new ZipFilePart(new ZipFile(f));
} catch (IOException e) {
// error in path
}
return null;
}
// is there a package with the specified name on this path part
public boolean hasPackage(String name) { return false; }
// select a compilation unit from a canonical name
// returns true of the compilation unit exists on this path
public boolean selectCompilationUnit(String canonicalName) throws IOException { return false; }
// load the return currently selected compilation unit
public CompilationUnit getCompilationUnit() {
long startTime = System.currentTimeMillis();
if(!isSource) {
try {
if(program.options().verbose())
System.out.print("Loading .class file: " + fullName + " ");
CompilationUnit u = program.bytecodeReader.read(is, fullName, program);
//CompilationUnit u = new bytecode.Parser(is, fullName).parse(null, null, program);
u.setPathName(pathName);
u.setRelativeName(relativeName);
u.setFromSource(false);
is.close();
is = null;
if(program.options().verbose())
System.out.println("from " + pathName + " in " + (System.currentTimeMillis() - startTime) + " ms");
return u;
} catch (Exception e) {
throw new Error("Error loading " + fullName, e);
}
}
else {
try {
if(program.options().verbose())
System.out.print("Loading .java file: " + fullName + " ");
CompilationUnit u = program.javaParser.parse(is, fullName);
is.close();
is = null;
u.setPathName(pathName);
u.setRelativeName(relativeName);
u.setFromSource(true);
if(program.options().verbose())
System.out.println("in " + (System.currentTimeMillis() - startTime) + " ms");
return u;
} catch (Exception e) {
System.err.println("Unexpected error of kind " + e.getClass().getName());
throw new Error(fullName + ": " + e.getMessage(), e);
}
}
}
}
// load files from a folder
class FolderPart extends PathPart {
private HashMap map = new HashMap();
private File folder;
public FolderPart(File folder) {
this.folder = folder;
}
public boolean hasPackage(String name) {
return filesInPackage(name) != null;
}
public boolean hasCompilationUnit(String canonicalName) {
int index = canonicalName.lastIndexOf('.');
String packageName = index == -1 ? "" : canonicalName.substring(0, index);
String typeName = canonicalName.substring(index + 1, canonicalName.length());
Collection c = filesInPackage(packageName);
boolean result = c != null && c.contains(typeName + fileSuffix());
return result;
}
private Collection filesInPackage(String packageName) {
if(!map.containsKey(packageName)) {
File f = new File(folder, packageName.replace('.', File.separatorChar));
Collection c = Collections.EMPTY_LIST;
if(f.exists() && f.isDirectory()) {
String[] files = f.list();
if(files.length > 0) {
c = new HashSet();
for(int i = 0; i < files.length; i++)
c.add(files[i]);
}
}
else
c = null;
map.put(packageName, c);
}
return (Collection)map.get(packageName);
}
public boolean selectCompilationUnit(String canonicalName) throws IOException {
if(hasCompilationUnit(canonicalName)) {
String fileName = canonicalName.replace('.', File.separatorChar);
File classFile = new File(folder, fileName + fileSuffix());
if(classFile.isFile()) {
is = new FileInputStream(classFile);
age = classFile.lastModified();
pathName = classFile.getPath();
relativeName = fileName + fileSuffix();
fullName = canonicalName;
return true;
}
}
return false;
}
}
/**
* Loads class files from a zip file (Jar file)
*/
class ZipFilePart extends PathPart {
private HashSet set = new HashSet();
private ZipFile file;
private String zipPath;
public boolean hasPackage(String name) {
return set.contains(name);
}
public ZipFilePart(ZipFile file, String path) {
zipPath = path;
this.file = file;
// process all entries in the zip file
for (Enumeration e = file.entries() ; e.hasMoreElements() ;) {
ZipEntry entry = (ZipEntry)e.nextElement();
String pathName = new File(entry.getName()).getParent();
if(pathName != null)
pathName = pathName.replace(File.separatorChar, '.');
if(!set.contains(pathName)) {
int pos = 0;
while(pathName != null && -1 != (pos = pathName.indexOf('.', pos + 1))) {
String n = pathName.substring(0, pos);
if(!set.contains(n)) {
set.add(n);
}
}
set.add(pathName);
}
set.add(entry.getName());
}
}
public ZipFilePart(ZipFile file) {
this(file, file.getName());
}
public boolean selectCompilationUnit(String canonicalName) throws IOException {
String name = canonicalName.replace('.', '/'); // ZipFiles always use '/' as separator
name = name + fileSuffix();
if(set.contains(name)) {
ZipEntry zipEntry = file.getEntry(name);
if(zipEntry != null && !zipEntry.isDirectory()) {
is = file.getInputStream(zipEntry);
age = zipEntry.getTime();
pathName = zipPath;
relativeName = name + fileSuffix();
fullName = canonicalName;
return true;
}
}
return false;
}
}
// load files specified explicitly (on the command line)
class FileNamesPart extends PathPart {
private HashMap sourceFiles = new HashMap();
private HashSet packages = new HashSet();
public FileNamesPart(Program p) {
isSource = true;
program = p;
}
public boolean hasPackage(String name) { return packages.contains(name); }
public boolean isEmpty() { return sourceFiles.isEmpty(); }
public Collection keySet() { return sourceFiles.keySet(); }
public boolean selectCompilationUnit(String canonicalName) throws IOException {
if(sourceFiles.containsKey(canonicalName)) {
String f = (String)sourceFiles.get(canonicalName);
File classFile = new File(f);
if(classFile.isFile()) {
is = new FileInputStream(classFile);
pathName = classFile.getPath();
relativeName = f;
fullName = canonicalName;
sourceFiles.remove(canonicalName);
return true;
}
}
return false;
}
/**
* Add a source file to be parsed.
* @return The CompilationUnit representing the source file,
* or <code>null</code> if no such file exists
*/
public CompilationUnit addSourceFile(String name) {
try {
File classFile = new File(name);
if(classFile.isFile()) {
is = new FileInputStream(classFile);
this.pathName = classFile.getPath();
relativeName = name;
fullName = name; // is this ok
CompilationUnit u = getCompilationUnit();
if(u != null) {
program.addCompilationUnit(u);
String packageName = u.getPackageDecl();
if(packageName != null && !packages.contains(packageName)) {
packages.add(packageName);
int pos = 0;
while(packageName != null && -1 != (pos = packageName.indexOf('.', pos + 1))) {
String n = packageName.substring(0, pos);
if(!packages.contains(n))
packages.add(n);
}
}
}
return u;
}
} catch (IOException e) {
}
return null;
}
}
// remove user defined classes from this program but keep library classes
public void Program.simpleReset() {
lookupType_String_String_values = new HashMap();
......
/**
* PathPart classes - used to represent path parts on the
* bootclasspath and user classpath.
*/
aspect PathPart {
class PathPart {
public InputStream is;
protected String pathName;
protected String relativeName;
protected String fullName;
long age;
Program program;
protected PathPart() {
}
protected boolean isSource;
protected String fileSuffix() {
return isSource ? ".java" : ".class";
}
public static PathPart createSourcePath(String fileName, Program program) {
PathPart p = createPathPart(fileName);
if(p != null) {
p.isSource = true;
p.program = program;
}
return p;
}
public static PathPart createClassPath(String fileName, Program program) {
PathPart p = createPathPart(fileName);
if(p != null) {
p.isSource = false;
p.program = program;
}
return p;
}
private static PathPart createPathPart(String s) {
try {
File f = new File(s);
if(f.isDirectory())
return new FolderPart(f);
else if(f.isFile())
return new ZipFilePart(new ZipFile(f));
} catch (IOException e) {
// error in path
}
return null;
}
// is there a package with the specified name on this path part
public boolean hasPackage(String name) { return false; }
// select a compilation unit from a canonical name
// returns true of the compilation unit exists on this path
public boolean selectCompilationUnit(String canonicalName) throws IOException { return false; }
// load the return currently selected compilation unit
public CompilationUnit getCompilationUnit() {
long startTime = System.currentTimeMillis();
if(!isSource) {
try {
if(program.options().verbose())
System.out.print("Loading .class file: " + fullName + " ");
CompilationUnit u = program.bytecodeReader.read(is, fullName, program);
//CompilationUnit u = new bytecode.Parser(is, fullName).parse(null, null, program);
u.setPathName(pathName);
u.setRelativeName(relativeName);
u.setFromSource(false);
is.close();
is = null;
if(program.options().verbose())
System.out.println("from " + pathName + " in " + (System.currentTimeMillis() - startTime) + " ms");
return u;
} catch (Exception e) {
throw new Error("Error loading " + fullName, e);
}
}
else {
try {
if(program.options().verbose())
System.out.print("Loading .java file: " + fullName + " ");
CompilationUnit u = program.javaParser.parse(is, fullName);
is.close();
is = null;
u.setPathName(pathName);
u.setRelativeName(relativeName);
u.setFromSource(true);
if(program.options().verbose())
System.out.println("in " + (System.currentTimeMillis() - startTime) + " ms");
return u;
} catch (Exception e) {
System.err.println("Unexpected error of kind " + e.getClass().getName());
throw new Error(fullName + ": " + e.getMessage(), e);
}
}
}
}
// load files from a folder
class FolderPart extends PathPart {
private HashMap map = new HashMap();
private File folder;
public FolderPart(File folder) {
this.folder = folder;
}
public boolean hasPackage(String name) {
return filesInPackage(name) != null;
}
public boolean hasCompilationUnit(String canonicalName) {
int index = canonicalName.lastIndexOf('.');
String packageName = index == -1 ? "" : canonicalName.substring(0, index);
String typeName = canonicalName.substring(index + 1, canonicalName.length());
Collection c = filesInPackage(packageName);
boolean result = c != null && c.contains(typeName + fileSuffix());
return result;
}
private Collection filesInPackage(String packageName) {
if(!map.containsKey(packageName)) {
File f = new File(folder, packageName.replace('.', File.separatorChar));
Collection c = Collections.EMPTY_LIST;
if(f.exists() && f.isDirectory()) {
String[] files = f.list();
if(files.length > 0) {
c = new HashSet();
for(int i = 0; i < files.length; i++)
c.add(files[i]);
}
}
else
c = null;
map.put(packageName, c);
}
return (Collection)map.get(packageName);
}
public boolean selectCompilationUnit(String canonicalName) throws IOException {
if(hasCompilationUnit(canonicalName)) {
String fileName = canonicalName.replace('.', File.separatorChar);
File classFile = new File(folder, fileName + fileSuffix());
if(classFile.isFile()) {
is = new FileInputStream(classFile);
age = classFile.lastModified();
pathName = classFile.getPath();
relativeName = fileName + fileSuffix();
fullName = canonicalName;
return true;
}
}
return false;
}
}
/**
* Loads class files from a zip file (Jar file)
*/
class ZipFilePart extends PathPart {
private HashSet set = new HashSet();
private ZipFile file;
private String zipPath;
public boolean hasPackage(String name) {
return set.contains(name);
}
public ZipFilePart(ZipFile file, String path) {
zipPath = path;
this.file = file;
// process all entries in the zip file
for (Enumeration e = file.entries() ; e.hasMoreElements() ;) {
ZipEntry entry = (ZipEntry)e.nextElement();
String pathName = new File(entry.getName()).getParent();
if(pathName != null)
pathName = pathName.replace(File.separatorChar, '.');
if(!set.contains(pathName)) {
int pos = 0;
while(pathName != null && -1 != (pos = pathName.indexOf('.', pos + 1))) {
String n = pathName.substring(0, pos);
if(!set.contains(n)) {
set.add(n);
}
}
set.add(pathName);
}
set.add(entry.getName());
}
}
public ZipFilePart(ZipFile file) {
this(file, file.getName());
}
public boolean selectCompilationUnit(String canonicalName) throws IOException {
String name = canonicalName.replace('.', '/'); // ZipFiles always use '/' as separator
name = name + fileSuffix();
if(set.contains(name)) {
ZipEntry zipEntry = file.getEntry(name);
if(zipEntry != null && !zipEntry.isDirectory()) {
is = file.getInputStream(zipEntry);
age = zipEntry.getTime();
pathName = zipPath;
relativeName = name + fileSuffix();
fullName = canonicalName;
return true;
}
}
return false;
}
}
// load files specified explicitly (on the command line)
class FileNamesPart extends PathPart {
private HashMap sourceFiles = new HashMap();
private HashSet packages = new HashSet();
public FileNamesPart(Program p) {
isSource = true;
program = p;
}
public boolean hasPackage(String name) { return packages.contains(name); }
public boolean isEmpty() { return sourceFiles.isEmpty(); }
public Collection keySet() { return sourceFiles.keySet(); }
public boolean selectCompilationUnit(String canonicalName) throws IOException {
if(sourceFiles.containsKey(canonicalName)) {
String f = (String)sourceFiles.get(canonicalName);
File classFile = new File(f);
if(classFile.isFile()) {
is = new FileInputStream(classFile);
pathName = classFile.getPath();
relativeName = f;
fullName = canonicalName;
sourceFiles.remove(canonicalName);
return true;
}
}
return false;
}
/**
* Add a source file to be parsed.
* @return The CompilationUnit representing the source file,
* or <code>null</code> if no such file exists
*/
public CompilationUnit addSourceFile(String name) {
try {
File classFile = new File(name);
if(classFile.isFile()) {
is = new FileInputStream(classFile);
this.pathName = classFile.getPath();
relativeName = name;
fullName = name; // is this ok
CompilationUnit u = getCompilationUnit();
if(u != null) {
program.addCompilationUnit(u);
String packageName = u.getPackageDecl();
if(packageName != null && !packages.contains(packageName)) {
packages.add(packageName);
int pos = 0;
while(packageName != null && -1 != (pos = packageName.indexOf('.', pos + 1))) {
String n = packageName.substring(0, pos);
if(!packages.contains(n))
packages.add(n);
}
}
}
return u;
}
} catch (IOException e) {
}
return null;
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment