This commit is contained in:
Tim Bell 2008-06-25 23:30:55 -07:00
commit ab32cfbffe
23 changed files with 1338 additions and 838 deletions

View File

@ -100,9 +100,12 @@ public class Check {
boolean verboseDeprecated = lint.isEnabled(LintCategory.DEPRECATION); boolean verboseDeprecated = lint.isEnabled(LintCategory.DEPRECATION);
boolean verboseUnchecked = lint.isEnabled(LintCategory.UNCHECKED); boolean verboseUnchecked = lint.isEnabled(LintCategory.UNCHECKED);
boolean enforceMandatoryWarnings = source.enforceMandatoryWarnings();
deprecationHandler = new MandatoryWarningHandler(log,verboseDeprecated, "deprecated"); deprecationHandler = new MandatoryWarningHandler(log, verboseDeprecated,
uncheckedHandler = new MandatoryWarningHandler(log, verboseUnchecked, "unchecked"); enforceMandatoryWarnings, "deprecated");
uncheckedHandler = new MandatoryWarningHandler(log, verboseUnchecked,
enforceMandatoryWarnings, "unchecked");
} }
/** Switch: generics enabled? /** Switch: generics enabled?
@ -1367,13 +1370,47 @@ public class Check {
types.isSameType(rt1, rt2) || types.isSameType(rt1, rt2) ||
rt1.tag >= CLASS && rt2.tag >= CLASS && rt1.tag >= CLASS && rt2.tag >= CLASS &&
(types.covariantReturnType(rt1, rt2, Warner.noWarnings) || (types.covariantReturnType(rt1, rt2, Warner.noWarnings) ||
types.covariantReturnType(rt2, rt1, Warner.noWarnings)); types.covariantReturnType(rt2, rt1, Warner.noWarnings)) ||
checkCommonOverriderIn(s1,s2,site);
if (!compat) return s2; if (!compat) return s2;
} }
} }
} }
return null; return null;
} }
//WHERE
boolean checkCommonOverriderIn(Symbol s1, Symbol s2, Type site) {
Map<TypeSymbol,Type> supertypes = new HashMap<TypeSymbol,Type>();
Type st1 = types.memberType(site, s1);
Type st2 = types.memberType(site, s2);
closure(site, supertypes);
for (Type t : supertypes.values()) {
for (Scope.Entry e = t.tsym.members().lookup(s1.name); e.scope != null; e = e.next()) {
Symbol s3 = e.sym;
if (s3 == s1 || s3 == s2 || s3.kind != MTH || (s3.flags() & (BRIDGE|SYNTHETIC)) != 0) continue;
Type st3 = types.memberType(site,s3);
if (types.overrideEquivalent(st3, st1) && types.overrideEquivalent(st3, st2)) {
if (s3.owner == site.tsym) {
return true;
}
List<Type> tvars1 = st1.getTypeArguments();
List<Type> tvars2 = st2.getTypeArguments();
List<Type> tvars3 = st3.getTypeArguments();
Type rt1 = st1.getReturnType();
Type rt2 = st2.getReturnType();
Type rt13 = types.subst(st3.getReturnType(), tvars3, tvars1);
Type rt23 = types.subst(st3.getReturnType(), tvars3, tvars2);
boolean compat =
rt13.tag >= CLASS && rt23.tag >= CLASS &&
(types.covariantReturnType(rt13, rt1, Warner.noWarnings) &&
types.covariantReturnType(rt23, rt2, Warner.noWarnings));
if (compat)
return true;
}
}
}
return false;
}
/** Check that a given method conforms with any method it overrides. /** Check that a given method conforms with any method it overrides.
* @param tree The tree from which positions are extracted * @param tree The tree from which positions are extracted

View File

@ -25,6 +25,7 @@
package com.sun.tools.javac.file; package com.sun.tools.javac.file;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.Reader; import java.io.Reader;
@ -36,6 +37,9 @@ import javax.tools.JavaFileObject;
import static javax.tools.JavaFileObject.Kind.*; import static javax.tools.JavaFileObject.Kind.*;
public abstract class BaseFileObject implements JavaFileObject { public abstract class BaseFileObject implements JavaFileObject {
protected BaseFileObject(JavacFileManager fileManager) {
this.fileManager = fileManager;
}
public JavaFileObject.Kind getKind() { public JavaFileObject.Kind getKind() {
String n = getName(); String n = getName();
@ -76,4 +80,14 @@ public abstract class BaseFileObject implements JavaFileObject {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
protected abstract String inferBinaryName(Iterable<? extends File> path);
protected static String removeExtension(String fileName) {
int lastDot = fileName.lastIndexOf(".");
return (lastDot == -1 ? fileName : fileName.substring(0, lastDot));
}
/** The file manager that created this JavaFileObject. */
protected final JavacFileManager fileManager;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. * Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -25,21 +25,16 @@
package com.sun.tools.javac.file; package com.sun.tools.javac.file;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.Writer;
import java.lang.ref.SoftReference; import java.lang.ref.SoftReference;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
@ -56,13 +51,11 @@ import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
import javax.lang.model.SourceVersion; import javax.lang.model.SourceVersion;
@ -96,15 +89,6 @@ public class JavacFileManager implements StandardJavaFileManager {
boolean useZipFileIndex; boolean useZipFileIndex;
private static int symbolFilePrefixLength = 0;
static {
try {
symbolFilePrefixLength = symbolFilePrefix.getBytes("UTF-8").length;
} catch (java.io.UnsupportedEncodingException uee) {
// Can't happen...UTF-8 is always supported.
}
}
private static boolean CHECK_ZIP_TIMESTAMP = false; private static boolean CHECK_ZIP_TIMESTAMP = false;
private static Map<File, Boolean> isDirectory = new ConcurrentHashMap<File, Boolean>(); private static Map<File, Boolean> isDirectory = new ConcurrentHashMap<File, Boolean>();
@ -202,7 +186,7 @@ public class JavacFileManager implements StandardJavaFileManager {
} }
public JavaFileObject getRegularFile(File file) { public JavaFileObject getRegularFile(File file) {
return new RegularFileObject(file); return new RegularFileObject(this, file);
} }
public JavaFileObject getFileForOutput(String classname, public JavaFileObject getFileForOutput(String classname,
@ -405,7 +389,7 @@ public class JavacFileManager implements StandardJavaFileManager {
} else { } else {
if (isValidFile(fname, fileKinds)) { if (isValidFile(fname, fileKinds)) {
JavaFileObject fe = JavaFileObject fe =
new RegularFileObject(fname, new File(d, fname)); new RegularFileObject(this, fname, new File(d, fname));
l.append(fe); l.append(fe);
} }
} }
@ -469,99 +453,6 @@ public class JavacFileManager implements StandardJavaFileManager {
Set<String> getSubdirectories(); Set<String> getSubdirectories();
} }
public class ZipArchive implements Archive {
protected final Map<String,List<String>> map;
protected final ZipFile zdir;
public ZipArchive(ZipFile zdir) throws IOException {
this.zdir = zdir;
this.map = new HashMap<String,List<String>>();
for (Enumeration<? extends ZipEntry> e = zdir.entries(); e.hasMoreElements(); ) {
ZipEntry entry;
try {
entry = e.nextElement();
} catch (InternalError ex) {
IOException io = new IOException();
io.initCause(ex); // convenience constructors added in Mustang :-(
throw io;
}
addZipEntry(entry);
}
}
void addZipEntry(ZipEntry entry) {
String name = entry.getName();
int i = name.lastIndexOf('/');
String dirname = name.substring(0, i+1);
String basename = name.substring(i+1);
if (basename.length() == 0)
return;
List<String> list = map.get(dirname);
if (list == null)
list = List.nil();
list = list.prepend(basename);
map.put(dirname, list);
}
public boolean contains(String name) {
int i = name.lastIndexOf('/');
String dirname = name.substring(0, i+1);
String basename = name.substring(i+1);
if (basename.length() == 0)
return false;
List<String> list = map.get(dirname);
return (list != null && list.contains(basename));
}
public List<String> getFiles(String subdirectory) {
return map.get(subdirectory);
}
public JavaFileObject getFileObject(String subdirectory, String file) {
ZipEntry ze = zdir.getEntry(subdirectory + file);
return new ZipFileObject(file, zdir, ze);
}
public Set<String> getSubdirectories() {
return map.keySet();
}
public void close() throws IOException {
zdir.close();
}
}
public class SymbolArchive extends ZipArchive {
final File origFile;
public SymbolArchive(File orig, ZipFile zdir) throws IOException {
super(zdir);
this.origFile = orig;
}
@Override
void addZipEntry(ZipEntry entry) {
// called from super constructor, may not refer to origFile.
String name = entry.getName();
if (!name.startsWith(symbolFilePrefix))
return;
name = name.substring(symbolFilePrefix.length());
int i = name.lastIndexOf('/');
String dirname = name.substring(0, i+1);
String basename = name.substring(i+1);
if (basename.length() == 0)
return;
List<String> list = map.get(dirname);
if (list == null)
list = List.nil();
list = list.prepend(basename);
map.put(dirname, list);
}
@Override
public JavaFileObject getFileObject(String subdirectory, String file) {
return super.getFileObject(symbolFilePrefix + subdirectory, file);
}
}
public class MissingArchive implements Archive { public class MissingArchive implements Archive {
final File zipFileName; final File zipFileName;
public MissingArchive(File name) { public MissingArchive(File name) {
@ -647,24 +538,29 @@ public class JavacFileManager implements StandardJavaFileManager {
if (origZipFileName == zipFileName) { if (origZipFileName == zipFileName) {
if (!useZipFileIndex) { if (!useZipFileIndex) {
archive = new ZipArchive(zdir); archive = new ZipArchive(this, zdir);
} else { } else {
archive = new ZipFileIndexArchive(this, ZipFileIndex.getZipFileIndex(zipFileName, 0, archive = new ZipFileIndexArchive(this, ZipFileIndex.getZipFileIndex(zipFileName, null,
usePreindexedCache, preindexCacheLocation, options.get("writezipindexfiles") != null)); usePreindexedCache, preindexCacheLocation, options.get("writezipindexfiles") != null));
} }
} }
else { else {
if (!useZipFileIndex) { if (!useZipFileIndex) {
archive = new SymbolArchive(origZipFileName, zdir); archive = new SymbolArchive(this, origZipFileName, zdir, symbolFilePrefix);
} }
else { else {
archive = new ZipFileIndexArchive(this, ZipFileIndex.getZipFileIndex(zipFileName, symbolFilePrefixLength, archive = new ZipFileIndexArchive(this,
usePreindexedCache, preindexCacheLocation, options.get("writezipindexfiles") != null)); ZipFileIndex.getZipFileIndex(zipFileName,
symbolFilePrefix,
usePreindexedCache,
preindexCacheLocation,
options.get("writezipindexfiles") != null));
} }
} }
} catch (FileNotFoundException ex) { } catch (FileNotFoundException ex) {
archive = new MissingArchive(zipFileName); archive = new MissingArchive(zipFileName);
} catch (IOException ex) { } catch (IOException ex) {
if (zipFileName.exists())
log.error("error.reading.file", zipFileName, ex.getLocalizedMessage()); log.error("error.reading.file", zipFileName, ex.getLocalizedMessage());
archive = new MissingArchive(zipFileName); archive = new MissingArchive(zipFileName);
} }
@ -694,7 +590,17 @@ public class JavacFileManager implements StandardJavaFileManager {
} }
} }
private Map<JavaFileObject, SoftReference<CharBuffer>> contentCache = new HashMap<JavaFileObject, SoftReference<CharBuffer>>(); CharBuffer getCachedContent(JavaFileObject file) {
SoftReference<CharBuffer> r = contentCache.get(file);
return (r == null ? null : r.get());
}
void cache(JavaFileObject file, CharBuffer cb) {
contentCache.put(file, new SoftReference<CharBuffer>(cb));
}
private final Map<JavaFileObject, SoftReference<CharBuffer>> contentCache
= new HashMap<JavaFileObject, SoftReference<CharBuffer>>();
private String defaultEncodingName; private String defaultEncodingName;
private String getDefaultEncodingName() { private String getDefaultEncodingName() {
@ -724,7 +630,7 @@ public class JavacFileManager implements StandardJavaFileManager {
/** /**
* Make a byte buffer from an input stream. * Make a byte buffer from an input stream.
*/ */
private ByteBuffer makeByteBuffer(InputStream in) ByteBuffer makeByteBuffer(InputStream in)
throws IOException { throws IOException {
int limit = in.available(); int limit = in.available();
if (mmappedIO && in instanceof FileInputStream) { if (mmappedIO && in instanceof FileInputStream) {
@ -750,6 +656,10 @@ public class JavacFileManager implements StandardJavaFileManager {
return (ByteBuffer)result.flip(); return (ByteBuffer)result.flip();
} }
void recycleByteBuffer(ByteBuffer bb) {
byteBufferCache.put(bb);
}
/** /**
* A single-element cache of direct byte buffers. * A single-element cache of direct byte buffers.
*/ */
@ -768,9 +678,10 @@ public class JavacFileManager implements StandardJavaFileManager {
cached = x; cached = x;
} }
} }
private final ByteBufferCache byteBufferCache; private final ByteBufferCache byteBufferCache;
private CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) { CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) {
Charset charset = (this.charset == null) Charset charset = (this.charset == null)
? Charset.forName(encodingName) ? Charset.forName(encodingName)
: this.charset; : this.charset;
@ -790,7 +701,7 @@ public class JavacFileManager implements StandardJavaFileManager {
/** /**
* Decode a ByteBuffer into a CharBuffer. * Decode a ByteBuffer into a CharBuffer.
*/ */
private CharBuffer decode(ByteBuffer inbuf, boolean ignoreEncodingErrors) { CharBuffer decode(ByteBuffer inbuf, boolean ignoreEncodingErrors) {
String encodingName = getEncodingName(); String encodingName = getEncodingName();
CharsetDecoder decoder; CharsetDecoder decoder;
try { try {
@ -900,47 +811,13 @@ public class JavacFileManager implements StandardJavaFileManager {
// Need to match the path semantics of list(location, ...) // Need to match the path semantics of list(location, ...)
Iterable<? extends File> path = getLocation(location); Iterable<? extends File> path = getLocation(location);
if (path == null) { if (path == null) {
//System.err.println("Path for " + location + " is null");
return null; return null;
} }
//System.err.println("Path for " + location + " is " + path);
if (file instanceof RegularFileObject) { if (file instanceof BaseFileObject) {
RegularFileObject r = (RegularFileObject) file; return ((BaseFileObject) file).inferBinaryName(path);
String rPath = r.getPath();
//System.err.println("RegularFileObject " + file + " " +r.getPath());
for (File dir: path) {
//System.err.println("dir: " + dir);
String dPath = dir.getPath();
if (!dPath.endsWith(File.separator))
dPath += File.separator;
if (rPath.regionMatches(true, 0, dPath, 0, dPath.length())
&& new File(rPath.substring(0, dPath.length())).equals(new File(dPath))) {
String relativeName = rPath.substring(dPath.length());
return removeExtension(relativeName).replace(File.separatorChar, '.');
}
}
} else if (file instanceof ZipFileObject) {
ZipFileObject z = (ZipFileObject) file;
String entryName = z.getZipEntryName();
if (entryName.startsWith(symbolFilePrefix))
entryName = entryName.substring(symbolFilePrefix.length());
return removeExtension(entryName).replace('/', '.');
} else if (file instanceof ZipFileIndexFileObject) {
ZipFileIndexFileObject z = (ZipFileIndexFileObject) file;
String entryName = z.getZipEntryName();
if (entryName.startsWith(symbolFilePrefix))
entryName = entryName.substring(symbolFilePrefix.length());
return removeExtension(entryName).replace(File.separatorChar, '.');
} else } else
throw new IllegalArgumentException(file.getClass().getName()); throw new IllegalArgumentException(file.getClass().getName());
// System.err.println("inferBinaryName failed for " + file);
return null;
}
// where
private static String removeExtension(String fileName) {
int lastDot = fileName.lastIndexOf(".");
return (lastDot == -1 ? fileName : fileName.substring(0, lastDot));
} }
public boolean isSameFile(FileObject a, FileObject b) { public boolean isSameFile(FileObject a, FileObject b) {
@ -1028,7 +905,7 @@ public class JavacFileManager implements StandardJavaFileManager {
if (dir.isDirectory()) { if (dir.isDirectory()) {
File f = new File(dir, name.replace('/', File.separatorChar)); File f = new File(dir, name.replace('/', File.separatorChar));
if (f.exists()) if (f.exists())
return new RegularFileObject(f); return new RegularFileObject(this, f);
} else { } else {
Archive a = openArchive(dir); Archive a = openArchive(dir);
if (a.contains(name)) { if (a.contains(name)) {
@ -1090,7 +967,7 @@ public class JavacFileManager implements StandardJavaFileManager {
if (sibling != null && sibling instanceof RegularFileObject) { if (sibling != null && sibling instanceof RegularFileObject) {
siblingDir = ((RegularFileObject)sibling).f.getParentFile(); siblingDir = ((RegularFileObject)sibling).f.getParentFile();
} }
return new RegularFileObject(new File(siblingDir, baseName(fileName))); return new RegularFileObject(this, new File(siblingDir, baseName(fileName)));
} }
} else if (location == SOURCE_OUTPUT) { } else if (location == SOURCE_OUTPUT) {
dir = (getSourceOutDir() != null ? getSourceOutDir() : getClassOutDir()); dir = (getSourceOutDir() != null ? getSourceOutDir() : getClassOutDir());
@ -1104,7 +981,7 @@ public class JavacFileManager implements StandardJavaFileManager {
} }
File file = (dir == null ? new File(fileName) : new File(dir, fileName)); File file = (dir == null ? new File(fileName) : new File(dir, fileName));
return new RegularFileObject(file); return new RegularFileObject(this, file);
} }
@ -1117,7 +994,7 @@ public class JavacFileManager implements StandardJavaFileManager {
else else
result = new ArrayList<RegularFileObject>(); result = new ArrayList<RegularFileObject>();
for (File f: files) for (File f: files)
result.add(new RegularFileObject(nullCheck(f))); result.add(new RegularFileObject(this, nullCheck(f)));
return result; return result;
} }
@ -1267,452 +1144,4 @@ public class JavacFileManager implements StandardJavaFileManager {
t.getClass(); // null check t.getClass(); // null check
return it; return it;
} }
/**
* A subclass of JavaFileObject representing regular files.
*/
private class RegularFileObject extends BaseFileObject {
/** Have the parent directories been created?
*/
private boolean hasParents=false;
/** The file's name.
*/
private String name;
/** The underlying file.
*/
final File f;
public RegularFileObject(File f) {
this(f.getName(), f);
}
public RegularFileObject(String name, File f) {
if (f.isDirectory())
throw new IllegalArgumentException("directories not supported");
this.name = name;
this.f = f;
}
public InputStream openInputStream() throws IOException {
return new FileInputStream(f);
}
protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
return JavacFileManager.this.getDecoder(getEncodingName(), ignoreEncodingErrors);
}
public OutputStream openOutputStream() throws IOException {
ensureParentDirectoriesExist();
return new FileOutputStream(f);
}
public Writer openWriter() throws IOException {
ensureParentDirectoriesExist();
return new OutputStreamWriter(new FileOutputStream(f), getEncodingName());
}
private void ensureParentDirectoriesExist() throws IOException {
if (!hasParents) {
File parent = f.getParentFile();
if (parent != null && !parent.exists()) {
if (!parent.mkdirs()) {
// if the mkdirs failed, it may be because another process concurrently
// created the directory, so check if the directory got created
// anyway before throwing an exception
if (!parent.exists() || !parent.isDirectory())
throw new IOException("could not create parent directories");
}
}
hasParents = true;
}
}
/** @deprecated see bug 6410637 */
@Deprecated
public String getName() {
return name;
}
public boolean isNameCompatible(String cn, JavaFileObject.Kind kind) {
cn.getClass(); // null check
if (kind == Kind.OTHER && getKind() != kind)
return false;
String n = cn + kind.extension;
if (name.equals(n))
return true;
if (name.equalsIgnoreCase(n)) {
try {
// allow for Windows
return (f.getCanonicalFile().getName().equals(n));
} catch (IOException e) {
}
}
return false;
}
/** @deprecated see bug 6410637 */
@Deprecated
public String getPath() {
return f.getPath();
}
public long getLastModified() {
return f.lastModified();
}
public boolean delete() {
return f.delete();
}
public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
SoftReference<CharBuffer> r = contentCache.get(this);
CharBuffer cb = (r == null ? null : r.get());
if (cb == null) {
InputStream in = new FileInputStream(f);
try {
ByteBuffer bb = makeByteBuffer(in);
JavaFileObject prev = log.useSource(this);
try {
cb = decode(bb, ignoreEncodingErrors);
} finally {
log.useSource(prev);
}
byteBufferCache.put(bb); // save for next time
if (!ignoreEncodingErrors)
contentCache.put(this, new SoftReference<CharBuffer>(cb));
} finally {
in.close();
}
}
return cb;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof RegularFileObject))
return false;
RegularFileObject o = (RegularFileObject) other;
try {
return f.equals(o.f)
|| f.getCanonicalFile().equals(o.f.getCanonicalFile());
} catch (IOException e) {
return false;
}
}
@Override
public int hashCode() {
return f.hashCode();
}
public URI toUri() {
try {
// Do no use File.toURI to avoid file system access
String path = f.getAbsolutePath().replace(File.separatorChar, '/');
return new URI("file://" + path).normalize();
} catch (URISyntaxException ex) {
return f.toURI();
}
}
}
/**
* A subclass of JavaFileObject representing zip entries.
*/
public class ZipFileObject extends BaseFileObject {
/** The entry's name.
*/
private String name;
/** The zipfile containing the entry.
*/
ZipFile zdir;
/** The underlying zip entry object.
*/
ZipEntry entry;
public ZipFileObject(String name, ZipFile zdir, ZipEntry entry) {
this.name = name;
this.zdir = zdir;
this.entry = entry;
}
public InputStream openInputStream() throws IOException {
return zdir.getInputStream(entry);
}
public OutputStream openOutputStream() throws IOException {
throw new UnsupportedOperationException();
}
protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
return JavacFileManager.this.getDecoder(getEncodingName(), ignoreEncodingErrors);
}
public Writer openWriter() throws IOException {
throw new UnsupportedOperationException();
}
/** @deprecated see bug 6410637 */
@Deprecated
public String getName() {
return name;
}
public boolean isNameCompatible(String cn, JavaFileObject.Kind k) {
cn.getClass(); // null check
if (k == Kind.OTHER && getKind() != k)
return false;
return name.equals(cn + k.extension);
}
/** @deprecated see bug 6410637 */
@Deprecated
public String getPath() {
return zdir.getName() + "(" + entry + ")";
}
public long getLastModified() {
return entry.getTime();
}
public boolean delete() {
throw new UnsupportedOperationException();
}
public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
SoftReference<CharBuffer> r = contentCache.get(this);
CharBuffer cb = (r == null ? null : r.get());
if (cb == null) {
InputStream in = zdir.getInputStream(entry);
try {
ByteBuffer bb = makeByteBuffer(in);
JavaFileObject prev = log.useSource(this);
try {
cb = decode(bb, ignoreEncodingErrors);
} finally {
log.useSource(prev);
}
byteBufferCache.put(bb); // save for next time
if (!ignoreEncodingErrors)
contentCache.put(this, new SoftReference<CharBuffer>(cb));
} finally {
in.close();
}
}
return cb;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof ZipFileObject))
return false;
ZipFileObject o = (ZipFileObject) other;
return zdir.equals(o.zdir) || name.equals(o.name);
}
@Override
public int hashCode() {
return zdir.hashCode() + name.hashCode();
}
public String getZipName() {
return zdir.getName();
}
public String getZipEntryName() {
return entry.getName();
}
public URI toUri() {
String zipName = new File(getZipName()).toURI().normalize().getPath();
String entryName = getZipEntryName();
return URI.create("jar:" + zipName + "!" + entryName);
}
}
/**
* A subclass of JavaFileObject representing zip entries using the com.sun.tools.javac.zip.ZipFileIndex implementation.
*/
public class ZipFileIndexFileObject extends BaseFileObject {
/** The entry's name.
*/
private String name;
/** The zipfile containing the entry.
*/
ZipFileIndex zfIndex;
/** The underlying zip entry object.
*/
ZipFileIndexEntry entry;
/** The InputStream for this zip entry (file.)
*/
InputStream inputStream = null;
/** The name of the zip file where this entry resides.
*/
String zipName;
JavacFileManager defFileManager = null;
public ZipFileIndexFileObject(JavacFileManager fileManager, ZipFileIndex zfIndex, ZipFileIndexEntry entry, String zipFileName) {
super();
this.name = entry.getFileName();
this.zfIndex = zfIndex;
this.entry = entry;
this.zipName = zipFileName;
defFileManager = fileManager;
}
public InputStream openInputStream() throws IOException {
if (inputStream == null) {
inputStream = new ByteArrayInputStream(read());
}
return inputStream;
}
protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
return JavacFileManager.this.getDecoder(getEncodingName(), ignoreEncodingErrors);
}
public OutputStream openOutputStream() throws IOException {
throw new UnsupportedOperationException();
}
public Writer openWriter() throws IOException {
throw new UnsupportedOperationException();
}
/** @deprecated see bug 6410637 */
@Deprecated
public String getName() {
return name;
}
public boolean isNameCompatible(String cn, JavaFileObject.Kind k) {
cn.getClass(); // null check
if (k == Kind.OTHER && getKind() != k)
return false;
return name.equals(cn + k.extension);
}
/** @deprecated see bug 6410637 */
@Deprecated
public String getPath() {
return zipName + "(" + entry.getName() + ")";
}
public long getLastModified() {
return entry.getLastModified();
}
public boolean delete() {
throw new UnsupportedOperationException();
}
@Override
public boolean equals(Object other) {
if (!(other instanceof ZipFileIndexFileObject))
return false;
ZipFileIndexFileObject o = (ZipFileIndexFileObject) other;
return entry.equals(o.entry);
}
@Override
public int hashCode() {
return zipName.hashCode() + (name.hashCode() << 10);
}
public String getZipName() {
return zipName;
}
public String getZipEntryName() {
return entry.getName();
}
public URI toUri() {
String zipName = new File(getZipName()).toURI().normalize().getPath();
String entryName = getZipEntryName();
if (File.separatorChar != '/') {
entryName = entryName.replace(File.separatorChar, '/');
}
return URI.create("jar:" + zipName + "!" + entryName);
}
private byte[] read() throws IOException {
if (entry == null) {
entry = zfIndex.getZipIndexEntry(name);
if (entry == null)
throw new FileNotFoundException();
}
return zfIndex.read(entry);
}
public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
SoftReference<CharBuffer> r = defFileManager.contentCache.get(this);
CharBuffer cb = (r == null ? null : r.get());
if (cb == null) {
InputStream in = new ByteArrayInputStream(zfIndex.read(entry));
try {
ByteBuffer bb = makeByteBuffer(in);
JavaFileObject prev = log.useSource(this);
try {
cb = decode(bb, ignoreEncodingErrors);
} finally {
log.useSource(prev);
}
byteBufferCache.put(bb); // save for next time
if (!ignoreEncodingErrors)
defFileManager.contentCache.put(this, new SoftReference<CharBuffer>(cb));
} finally {
in.close();
}
}
return cb;
}
}
public class ZipFileIndexArchive implements Archive {
private final ZipFileIndex zfIndex;
private JavacFileManager fileManager;
public ZipFileIndexArchive(JavacFileManager fileManager, ZipFileIndex zdir) throws IOException {
this.fileManager = fileManager;
this.zfIndex = zdir;
}
public boolean contains(String name) {
return zfIndex.contains(name);
}
public com.sun.tools.javac.util.List<String> getFiles(String subdirectory) {
return zfIndex.getFiles(((subdirectory.endsWith("/") || subdirectory.endsWith("\\"))? subdirectory.substring(0, subdirectory.length() - 1) : subdirectory));
}
public JavaFileObject getFileObject(String subdirectory, String file) {
String fullZipFileName = subdirectory + file;
ZipFileIndexEntry entry = zfIndex.getZipIndexEntry(fullZipFileName);
JavaFileObject ret = new ZipFileIndexFileObject(fileManager, zfIndex, entry, zfIndex.getZipFile().getPath());
return ret;
}
public Set<String> getSubdirectories() {
return zfIndex.getAllDirectories();
}
public void close() throws IOException {
zfIndex.close();
}
}
} }

View File

@ -0,0 +1,204 @@
/*
* Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
package com.sun.tools.javac.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharsetDecoder;
import javax.tools.JavaFileObject;
/**
* A subclass of JavaFileObject representing regular files.
*/
class RegularFileObject extends BaseFileObject {
/** Have the parent directories been created?
*/
private boolean hasParents = false;
private String name;
final File f;
public RegularFileObject(JavacFileManager fileManager, File f) {
this(fileManager, f.getName(), f);
}
public RegularFileObject(JavacFileManager fileManager, String name, File f) {
super(fileManager);
if (f.isDirectory()) {
throw new IllegalArgumentException("directories not supported");
}
this.name = name;
this.f = f;
}
public InputStream openInputStream() throws IOException {
return new FileInputStream(f);
}
protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
}
public OutputStream openOutputStream() throws IOException {
ensureParentDirectoriesExist();
return new FileOutputStream(f);
}
public Writer openWriter() throws IOException {
ensureParentDirectoriesExist();
return new OutputStreamWriter(new FileOutputStream(f), fileManager.getEncodingName());
}
@Override
protected String inferBinaryName(Iterable<? extends File> path) {
String fPath = f.getPath();
//System.err.println("RegularFileObject " + file + " " +r.getPath());
for (File dir: path) {
//System.err.println("dir: " + dir);
String dPath = dir.getPath();
if (!dPath.endsWith(File.separator))
dPath += File.separator;
if (fPath.regionMatches(true, 0, dPath, 0, dPath.length())
&& new File(fPath.substring(0, dPath.length())).equals(new File(dPath))) {
String relativeName = fPath.substring(dPath.length());
return removeExtension(relativeName).replace(File.separatorChar, '.');
}
}
return null;
}
private void ensureParentDirectoriesExist() throws IOException {
if (!hasParents) {
File parent = f.getParentFile();
if (parent != null && !parent.exists()) {
if (!parent.mkdirs()) {
if (!parent.exists() || !parent.isDirectory()) {
throw new IOException("could not create parent directories");
}
}
}
hasParents = true;
}
}
@Deprecated
public String getName() {
return name;
}
public boolean isNameCompatible(String cn, JavaFileObject.Kind kind) {
cn.getClass();
// null check
if (kind == Kind.OTHER && getKind() != kind) {
return false;
}
String n = cn + kind.extension;
if (name.equals(n)) {
return true;
}
if (name.equalsIgnoreCase(n)) {
try {
// allow for Windows
return f.getCanonicalFile().getName().equals(n);
} catch (IOException e) {
}
}
return false;
}
@Deprecated
public String getPath() {
return f.getPath();
}
public long getLastModified() {
return f.lastModified();
}
public boolean delete() {
return f.delete();
}
public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
CharBuffer cb = fileManager.getCachedContent(this);
if (cb == null) {
InputStream in = new FileInputStream(f);
try {
ByteBuffer bb = fileManager.makeByteBuffer(in);
JavaFileObject prev = fileManager.log.useSource(this);
try {
cb = fileManager.decode(bb, ignoreEncodingErrors);
} finally {
fileManager.log.useSource(prev);
}
fileManager.recycleByteBuffer(bb);
if (!ignoreEncodingErrors) {
fileManager.cache(this, cb);
}
} finally {
in.close();
}
}
return cb;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof RegularFileObject)) {
return false;
}
RegularFileObject o = (RegularFileObject) other;
try {
return f.equals(o.f) || f.getCanonicalFile().equals(o.f.getCanonicalFile());
} catch (IOException e) {
return false;
}
}
@Override
public int hashCode() {
return f.hashCode();
}
public URI toUri() {
try {
String path = f.getAbsolutePath().replace(File.separatorChar, '/');
return new URI("file://" + path).normalize();
} catch (URISyntaxException ex) {
return f.toURI();
}
}
}

View File

@ -0,0 +1,71 @@
/*
* Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
package com.sun.tools.javac.file;
import com.sun.tools.javac.util.List;
import java.io.File;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javax.tools.JavaFileObject;
public class SymbolArchive extends ZipArchive {
final File origFile;
final String prefix;
public SymbolArchive(JavacFileManager fileManager, File orig, ZipFile zdir, String prefix) throws IOException {
super(fileManager, zdir);
this.origFile = orig;
this.prefix = prefix;
}
@Override
void addZipEntry(ZipEntry entry) {
String name = entry.getName();
if (!name.startsWith(prefix)) {
return;
}
name = name.substring(prefix.length());
int i = name.lastIndexOf('/');
String dirname = name.substring(0, i + 1);
String basename = name.substring(i + 1);
if (basename.length() == 0) {
return;
}
List<String> list = map.get(dirname);
if (list == null) {
list = List.nil();
}
list = list.prepend(basename);
map.put(dirname, list);
}
@Override
public JavaFileObject getFileObject(String subdirectory, String file) {
return super.getFileObject(prefix + subdirectory, file);
}
}

View File

@ -0,0 +1,234 @@
/*
* Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
package com.sun.tools.javac.file;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javax.tools.JavaFileObject;
import com.sun.tools.javac.file.JavacFileManager.Archive;
import com.sun.tools.javac.util.List;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharsetDecoder;
public class ZipArchive implements Archive {
public ZipArchive(JavacFileManager fm, ZipFile zdir) throws IOException {
this.fileManager = fm;
this.zdir = zdir;
this.map = new HashMap<String,List<String>>();
for (Enumeration<? extends ZipEntry> e = zdir.entries(); e.hasMoreElements(); ) {
ZipEntry entry;
try {
entry = e.nextElement();
} catch (InternalError ex) {
IOException io = new IOException();
io.initCause(ex); // convenience constructors added in Mustang :-(
throw io;
}
addZipEntry(entry);
}
}
void addZipEntry(ZipEntry entry) {
String name = entry.getName();
int i = name.lastIndexOf('/');
String dirname = name.substring(0, i+1);
String basename = name.substring(i+1);
if (basename.length() == 0)
return;
List<String> list = map.get(dirname);
if (list == null)
list = List.nil();
list = list.prepend(basename);
map.put(dirname, list);
}
public boolean contains(String name) {
int i = name.lastIndexOf('/');
String dirname = name.substring(0, i+1);
String basename = name.substring(i+1);
if (basename.length() == 0)
return false;
List<String> list = map.get(dirname);
return (list != null && list.contains(basename));
}
public List<String> getFiles(String subdirectory) {
return map.get(subdirectory);
}
public JavaFileObject getFileObject(String subdirectory, String file) {
ZipEntry ze = zdir.getEntry(subdirectory + file);
return new ZipFileObject(this, file, ze);
}
public Set<String> getSubdirectories() {
return map.keySet();
}
public void close() throws IOException {
zdir.close();
}
protected JavacFileManager fileManager;
protected final Map<String,List<String>> map;
protected final ZipFile zdir;
/**
* A subclass of JavaFileObject representing zip entries.
*/
public static class ZipFileObject extends BaseFileObject {
private String name;
ZipArchive zarch;
ZipEntry entry;
public ZipFileObject(ZipArchive zarch, String name, ZipEntry entry) {
super(zarch.fileManager);
this.zarch = zarch;
this.name = name;
this.entry = entry;
}
public InputStream openInputStream() throws IOException {
return zarch.zdir.getInputStream(entry);
}
public OutputStream openOutputStream() throws IOException {
throw new UnsupportedOperationException();
}
protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
}
public Writer openWriter() throws IOException {
throw new UnsupportedOperationException();
}
@Deprecated
public String getName() {
return name;
}
public boolean isNameCompatible(String cn, JavaFileObject.Kind k) {
cn.getClass();
// null check
if (k == Kind.OTHER && getKind() != k) {
return false;
}
return name.equals(cn + k.extension);
}
@Deprecated
public String getPath() {
return zarch.zdir.getName() + "(" + entry + ")";
}
public long getLastModified() {
return entry.getTime();
}
public boolean delete() {
throw new UnsupportedOperationException();
}
public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
CharBuffer cb = fileManager.getCachedContent(this);
if (cb == null) {
InputStream in = zarch.zdir.getInputStream(entry);
try {
ByteBuffer bb = fileManager.makeByteBuffer(in);
JavaFileObject prev = fileManager.log.useSource(this);
try {
cb = fileManager.decode(bb, ignoreEncodingErrors);
} finally {
fileManager.log.useSource(prev);
}
fileManager.recycleByteBuffer(bb);
if (!ignoreEncodingErrors) {
fileManager.cache(this, cb);
}
} finally {
in.close();
}
}
return cb;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof ZipFileObject)) {
return false;
}
ZipFileObject o = (ZipFileObject) other;
return zarch.zdir.equals(o.zarch.zdir) || name.equals(o.name);
}
@Override
public int hashCode() {
return zarch.zdir.hashCode() + name.hashCode();
}
public String getZipName() {
return zarch.zdir.getName();
}
public String getZipEntryName() {
return entry.getName();
}
public URI toUri() {
String zipName = new File(getZipName()).toURI().normalize().getPath();
String entryName = getZipEntryName();
return URI.create("jar:" + zipName + "!" + entryName);
}
@Override
protected String inferBinaryName(Iterable<? extends File> path) {
String entryName = getZipEntryName();
if (zarch instanceof SymbolArchive) {
String prefix = ((SymbolArchive) zarch).prefix;
if (entryName.startsWith(prefix))
entryName = entryName.substring(prefix.length());
}
return removeExtension(entryName).replace('/', '.');
}
}
}

View File

@ -81,11 +81,12 @@ public class ZipFileIndex {
private File zipFile; private File zipFile;
private long zipFileLastModified = NOT_MODIFIED; private long zipFileLastModified = NOT_MODIFIED;
private RandomAccessFile zipRandomFile; private RandomAccessFile zipRandomFile;
private ZipFileIndexEntry[] entries; private Entry[] entries;
private boolean readFromIndex = false; private boolean readFromIndex = false;
private File zipIndexFile = null; private File zipIndexFile = null;
private boolean triedToReadIndex = false; private boolean triedToReadIndex = false;
final String symbolFilePrefix;
private int symbolFilePrefixLength = 0; private int symbolFilePrefixLength = 0;
private boolean hasPopulatedData = false; private boolean hasPopulatedData = false;
private long lastReferenceTimeStamp = NOT_MODIFIED; private long lastReferenceTimeStamp = NOT_MODIFIED;
@ -141,14 +142,14 @@ public class ZipFileIndex {
} }
} }
public static ZipFileIndex getZipFileIndex(File zipFile, int symbolFilePrefixLen, boolean useCache, String cacheLocation, boolean writeIndex) throws IOException { public static ZipFileIndex getZipFileIndex(File zipFile, String symbolFilePrefix, boolean useCache, String cacheLocation, boolean writeIndex) throws IOException {
ZipFileIndex zi = null; ZipFileIndex zi = null;
lock.lock(); lock.lock();
try { try {
zi = getExistingZipIndex(zipFile); zi = getExistingZipIndex(zipFile);
if (zi == null || (zi != null && zipFile.lastModified() != zi.zipFileLastModified)) { if (zi == null || (zi != null && zipFile.lastModified() != zi.zipFileLastModified)) {
zi = new ZipFileIndex(zipFile, symbolFilePrefixLen, writeIndex, zi = new ZipFileIndex(zipFile, symbolFilePrefix, writeIndex,
useCache, cacheLocation); useCache, cacheLocation);
zipFileIndexCache.put(zipFile, zi); zipFileIndexCache.put(zipFile, zi);
} }
@ -229,10 +230,12 @@ public class ZipFileIndex {
} }
} }
private ZipFileIndex(File zipFile, int symbolFilePrefixLen, boolean writeIndex, private ZipFileIndex(File zipFile, String symbolFilePrefix, boolean writeIndex,
boolean useCache, String cacheLocation) throws IOException { boolean useCache, String cacheLocation) throws IOException {
this.zipFile = zipFile; this.zipFile = zipFile;
this.symbolFilePrefixLength = symbolFilePrefixLen; this.symbolFilePrefix = symbolFilePrefix;
this.symbolFilePrefixLength = (symbolFilePrefix == null ? 0 :
symbolFilePrefix.getBytes("UTF-8").length);
this.writeIndex = writeIndex; this.writeIndex = writeIndex;
this.usePreindexedCache = useCache; this.usePreindexedCache = useCache;
this.preindexedCacheLocation = cacheLocation; this.preindexedCacheLocation = cacheLocation;
@ -312,7 +315,7 @@ public class ZipFileIndex {
private void cleanupState() { private void cleanupState() {
// Make sure there is a valid but empty index if the file doesn't exist // Make sure there is a valid but empty index if the file doesn't exist
entries = ZipFileIndexEntry.EMPTY_ARRAY; entries = Entry.EMPTY_ARRAY;
directories = Collections.<String, DirectoryEntry>emptyMap(); directories = Collections.<String, DirectoryEntry>emptyMap();
zipFileLastModified = NOT_MODIFIED; zipFileLastModified = NOT_MODIFIED;
allDirs = Collections.<String>emptySet(); allDirs = Collections.<String>emptySet();
@ -342,7 +345,7 @@ public class ZipFileIndex {
/** /**
* Returns the ZipFileIndexEntry for an absolute path, if there is one. * Returns the ZipFileIndexEntry for an absolute path, if there is one.
*/ */
public ZipFileIndexEntry getZipIndexEntry(String path) { Entry getZipIndexEntry(String path) {
if (File.separatorChar != '/') { if (File.separatorChar != '/') {
path = path.replace('/', File.separatorChar); path = path.replace('/', File.separatorChar);
} }
@ -493,7 +496,7 @@ public class ZipFileIndex {
public long getLastModified(String path) throws IOException { public long getLastModified(String path) throws IOException {
lock.lock(); lock.lock();
try { try {
ZipFileIndexEntry entry = getZipIndexEntry(path); Entry entry = getZipIndexEntry(path);
if (entry == null) if (entry == null)
throw new FileNotFoundException(); throw new FileNotFoundException();
return entry.getLastModified(); return entry.getLastModified();
@ -506,7 +509,7 @@ public class ZipFileIndex {
public int length(String path) throws IOException { public int length(String path) throws IOException {
lock.lock(); lock.lock();
try { try {
ZipFileIndexEntry entry = getZipIndexEntry(path); Entry entry = getZipIndexEntry(path);
if (entry == null) if (entry == null)
throw new FileNotFoundException(); throw new FileNotFoundException();
@ -530,7 +533,7 @@ public class ZipFileIndex {
public byte[] read(String path) throws IOException { public byte[] read(String path) throws IOException {
lock.lock(); lock.lock();
try { try {
ZipFileIndexEntry entry = getZipIndexEntry(path); Entry entry = getZipIndexEntry(path);
if (entry == null) if (entry == null)
throw new FileNotFoundException(MessageFormat.format("Path not found in ZIP: {0}", path)); throw new FileNotFoundException(MessageFormat.format("Path not found in ZIP: {0}", path));
return read(entry); return read(entry);
@ -540,7 +543,7 @@ public class ZipFileIndex {
} }
} }
public byte[] read(ZipFileIndexEntry entry) throws IOException { byte[] read(Entry entry) throws IOException {
lock.lock(); lock.lock();
try { try {
openFile(); openFile();
@ -556,7 +559,7 @@ public class ZipFileIndex {
public int read(String path, byte[] buffer) throws IOException { public int read(String path, byte[] buffer) throws IOException {
lock.lock(); lock.lock();
try { try {
ZipFileIndexEntry entry = getZipIndexEntry(path); Entry entry = getZipIndexEntry(path);
if (entry == null) if (entry == null)
throw new FileNotFoundException(); throw new FileNotFoundException();
return read(entry, buffer); return read(entry, buffer);
@ -566,7 +569,7 @@ public class ZipFileIndex {
} }
} }
public int read(ZipFileIndexEntry entry, byte[] buffer) int read(Entry entry, byte[] buffer)
throws IOException { throws IOException {
lock.lock(); lock.lock();
try { try {
@ -578,7 +581,7 @@ public class ZipFileIndex {
} }
} }
private byte[] readBytes(ZipFileIndexEntry entry) throws IOException { private byte[] readBytes(Entry entry) throws IOException {
byte[] header = getHeader(entry); byte[] header = getHeader(entry);
int csize = entry.compressedSize; int csize = entry.compressedSize;
byte[] cbuf = new byte[csize]; byte[] cbuf = new byte[csize];
@ -600,7 +603,7 @@ public class ZipFileIndex {
/** /**
* *
*/ */
private int readBytes(ZipFileIndexEntry entry, byte[] buffer) throws IOException { private int readBytes(Entry entry, byte[] buffer) throws IOException {
byte[] header = getHeader(entry); byte[] header = getHeader(entry);
// entry is not compressed? // entry is not compressed?
@ -633,7 +636,7 @@ public class ZipFileIndex {
// Zip utilities // Zip utilities
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
private byte[] getHeader(ZipFileIndexEntry entry) throws IOException { private byte[] getHeader(Entry entry) throws IOException {
zipRandomFile.seek(entry.offset); zipRandomFile.seek(entry.offset);
byte[] header = new byte[30]; byte[] header = new byte[30];
zipRandomFile.readFully(header); zipRandomFile.readFully(header);
@ -746,11 +749,11 @@ public class ZipFileIndex {
private void buildIndex() throws IOException { private void buildIndex() throws IOException {
int entryCount = get2ByteLittleEndian(zipDir, 0); int entryCount = get2ByteLittleEndian(zipDir, 0);
entries = new ZipFileIndexEntry[entryCount]; entries = new Entry[entryCount];
// Add each of the files // Add each of the files
if (entryCount > 0) { if (entryCount > 0) {
directories = new HashMap<String, DirectoryEntry>(); directories = new HashMap<String, DirectoryEntry>();
ArrayList<ZipFileIndexEntry> entryList = new ArrayList<ZipFileIndexEntry>(); ArrayList<Entry> entryList = new ArrayList<Entry>();
int pos = 2; int pos = 2;
for (int i = 0; i < entryCount; i++) { for (int i = 0; i < entryCount; i++) {
pos = readEntry(pos, entryList, directories); pos = readEntry(pos, entryList, directories);
@ -759,19 +762,19 @@ public class ZipFileIndex {
// Add the accumulated dirs into the same list // Add the accumulated dirs into the same list
Iterator i = directories.keySet().iterator(); Iterator i = directories.keySet().iterator();
while (i.hasNext()) { while (i.hasNext()) {
ZipFileIndexEntry zipFileIndexEntry = new ZipFileIndexEntry( (String) i.next()); Entry zipFileIndexEntry = new Entry( (String) i.next());
zipFileIndexEntry.isDir = true; zipFileIndexEntry.isDir = true;
entryList.add(zipFileIndexEntry); entryList.add(zipFileIndexEntry);
} }
entries = entryList.toArray(new ZipFileIndexEntry[entryList.size()]); entries = entryList.toArray(new Entry[entryList.size()]);
Arrays.sort(entries); Arrays.sort(entries);
} else { } else {
cleanupState(); cleanupState();
} }
} }
private int readEntry(int pos, List<ZipFileIndexEntry> entryList, private int readEntry(int pos, List<Entry> entryList,
Map<String, DirectoryEntry> directories) throws IOException { Map<String, DirectoryEntry> directories) throws IOException {
if (get4ByteLittleEndian(zipDir, pos) != 0x02014b50) { if (get4ByteLittleEndian(zipDir, pos) != 0x02014b50) {
throw new ZipException("cannot read zip file entry"); throw new ZipException("cannot read zip file entry");
@ -838,7 +841,7 @@ public class ZipFileIndex {
// For each dir create also a file // For each dir create also a file
if (fileStart != fileEnd) { if (fileStart != fileEnd) {
ZipFileIndexEntry entry = new ZipFileIndexEntry(directory, Entry entry = new Entry(directory,
new String(zipDir, fileStart, fileEnd - fileStart, "UTF-8")); new String(zipDir, fileStart, fileEnd - fileStart, "UTF-8"));
entry.setNativeTime(get4ByteLittleEndian(zipDir, pos + 12)); entry.setNativeTime(get4ByteLittleEndian(zipDir, pos + 12));
@ -873,6 +876,7 @@ public class ZipFileIndex {
/** ------------------------------------------------------------------------ /** ------------------------------------------------------------------------
* DirectoryEntry class * DirectoryEntry class
* -------------------------------------------------------------------------*/ * -------------------------------------------------------------------------*/
static class DirectoryEntry { static class DirectoryEntry {
private boolean filesInited; private boolean filesInited;
private boolean directoriesInited; private boolean directoriesInited;
@ -885,9 +889,9 @@ public class ZipFileIndex {
private com.sun.tools.javac.util.List<String> zipFileEntriesFiles = com.sun.tools.javac.util.List.<String>nil(); private com.sun.tools.javac.util.List<String> zipFileEntriesFiles = com.sun.tools.javac.util.List.<String>nil();
private com.sun.tools.javac.util.List<String> zipFileEntriesDirectories = com.sun.tools.javac.util.List.<String>nil(); private com.sun.tools.javac.util.List<String> zipFileEntriesDirectories = com.sun.tools.javac.util.List.<String>nil();
private com.sun.tools.javac.util.List<ZipFileIndexEntry> zipFileEntries = com.sun.tools.javac.util.List.<ZipFileIndexEntry>nil(); private com.sun.tools.javac.util.List<Entry> zipFileEntries = com.sun.tools.javac.util.List.<Entry>nil();
private List<ZipFileIndexEntry> entries = new ArrayList<ZipFileIndexEntry>(); private List<Entry> entries = new ArrayList<Entry>();
private ZipFileIndex zipFileIndex; private ZipFileIndex zipFileIndex;
@ -916,7 +920,7 @@ public class ZipFileIndex {
initEntries(); initEntries();
for (ZipFileIndexEntry e : entries) { for (Entry e : entries) {
if (!e.isDir) { if (!e.isDir) {
zipFileEntriesFiles = zipFileEntriesFiles.append(e.name); zipFileEntriesFiles = zipFileEntriesFiles.append(e.name);
} }
@ -932,7 +936,7 @@ public class ZipFileIndex {
initEntries(); initEntries();
for (ZipFileIndexEntry e : entries) { for (Entry e : entries) {
if (e.isDir) { if (e.isDir) {
zipFileEntriesDirectories = zipFileEntriesDirectories.append(e.name); zipFileEntriesDirectories = zipFileEntriesDirectories.append(e.name);
} }
@ -943,7 +947,7 @@ public class ZipFileIndex {
return zipFileEntriesDirectories; return zipFileEntriesDirectories;
} }
private com.sun.tools.javac.util.List<ZipFileIndexEntry> getEntries() { private com.sun.tools.javac.util.List<Entry> getEntries() {
if (zipFileEntriesInited) { if (zipFileEntriesInited) {
return zipFileEntries; return zipFileEntries;
} }
@ -951,7 +955,7 @@ public class ZipFileIndex {
initEntries(); initEntries();
zipFileEntries = com.sun.tools.javac.util.List.nil(); zipFileEntries = com.sun.tools.javac.util.List.nil();
for (ZipFileIndexEntry zfie : entries) { for (Entry zfie : entries) {
zipFileEntries = zipFileEntries.append(zfie); zipFileEntries = zipFileEntries.append(zfie);
} }
@ -960,9 +964,9 @@ public class ZipFileIndex {
return zipFileEntries; return zipFileEntries;
} }
private ZipFileIndexEntry getEntry(String rootName) { private Entry getEntry(String rootName) {
initEntries(); initEntries();
int index = Collections.binarySearch(entries, new ZipFileIndexEntry(dirName, rootName)); int index = Collections.binarySearch(entries, new Entry(dirName, rootName));
if (index < 0) { if (index < 0) {
return null; return null;
} }
@ -977,9 +981,9 @@ public class ZipFileIndex {
if (!zipFileIndex.readFromIndex) { if (!zipFileIndex.readFromIndex) {
int from = -Arrays.binarySearch(zipFileIndex.entries, int from = -Arrays.binarySearch(zipFileIndex.entries,
new ZipFileIndexEntry(dirName, ZipFileIndex.MIN_CHAR)) - 1; new Entry(dirName, ZipFileIndex.MIN_CHAR)) - 1;
int to = -Arrays.binarySearch(zipFileIndex.entries, int to = -Arrays.binarySearch(zipFileIndex.entries,
new ZipFileIndexEntry(dirName, MAX_CHAR)) - 1; new Entry(dirName, MAX_CHAR)) - 1;
boolean emptyList = false; boolean emptyList = false;
@ -1016,7 +1020,7 @@ public class ZipFileIndex {
// Read java time stamp of the file in the real Jar/Zip file // Read java time stamp of the file in the real Jar/Zip file
long eJavaTimestamp = raf.readLong(); long eJavaTimestamp = raf.readLong();
ZipFileIndexEntry rfie = new ZipFileIndexEntry(dirName, eName); Entry rfie = new Entry(dirName, eName);
rfie.isDir = eIsDir; rfie.isDir = eIsDir;
rfie.offset = eOffset; rfie.offset = eOffset;
rfie.size = eSize; rfie.size = eSize;
@ -1041,7 +1045,7 @@ public class ZipFileIndex {
entriesInited = true; entriesInited = true;
} }
List<ZipFileIndexEntry> getEntriesAsCollection() { List<Entry> getEntriesAsCollection() {
initEntries(); initEntries();
return entries; return entries;
@ -1173,8 +1177,8 @@ public class ZipFileIndex {
raf.seek(currFP); raf.seek(currFP);
// Now write each of the files in the DirectoryEntry // Now write each of the files in the DirectoryEntry
List<ZipFileIndexEntry> entries = de.getEntriesAsCollection(); List<Entry> entries = de.getEntriesAsCollection();
for (ZipFileIndexEntry zfie : entries) { for (Entry zfie : entries) {
// Write the name bytes // Write the name bytes
byte [] zfieNameBytes = zfie.name.getBytes("UTF-8"); byte [] zfieNameBytes = zfie.name.getBytes("UTF-8");
int zfieNameBytesLen = zfieNameBytes.length; int zfieNameBytesLen = zfieNameBytes.length;
@ -1245,4 +1249,94 @@ public class ZipFileIndex {
public File getZipFile() { public File getZipFile() {
return zipFile; return zipFile;
} }
static class Entry implements Comparable<Entry> {
public static final Entry[] EMPTY_ARRAY = {};
// Directory related
String dir;
boolean isDir;
// File related
String name;
int offset;
int size;
int compressedSize;
long javatime;
private int nativetime;
public Entry(String path) {
int separator = path.lastIndexOf(File.separatorChar);
if (separator == -1) {
dir = "".intern();
name = path;
} else {
dir = path.substring(0, separator).intern();
name = path.substring(separator + 1);
}
}
public Entry(String directory, String name) {
this.dir = directory.intern();
this.name = name;
}
public String getName() {
if (dir == null || dir.length() == 0) {
return name;
}
StringBuilder sb = new StringBuilder();
sb.append(dir);
sb.append(File.separatorChar);
sb.append(name);
return sb.toString();
}
public String getFileName() {
return name;
}
public long getLastModified() {
if (javatime == 0) {
javatime = dosToJavaTime(nativetime);
}
return javatime;
}
// From java.util.zip
private static long dosToJavaTime(int nativetime) {
// Bootstrap build problems prevent me from using the code directly
// Convert the raw/native time to a long for now
return (long)nativetime;
}
void setNativeTime(int natTime) {
nativetime = natTime;
}
public boolean isDirectory() {
return isDir;
}
public int compareTo(Entry other) {
String otherD = other.dir;
if (dir != otherD) {
int c = dir.compareTo(otherD);
if (c != 0)
return c;
}
return name.compareTo(other.name);
}
public String toString() {
return isDir ? ("Dir:" + dir + " : " + name) :
(dir + ":" + name);
}
}
} }

View File

@ -0,0 +1,233 @@
/*
* Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
package com.sun.tools.javac.file;
import java.io.IOException;
import java.util.Set;
import javax.tools.JavaFileObject;
import com.sun.tools.javac.file.JavacFileManager.Archive;
import com.sun.tools.javac.util.List;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharsetDecoder;
public class ZipFileIndexArchive implements Archive {
private final ZipFileIndex zfIndex;
private JavacFileManager fileManager;
public ZipFileIndexArchive(JavacFileManager fileManager, ZipFileIndex zdir) throws IOException {
super();
this.fileManager = fileManager;
this.zfIndex = zdir;
}
public boolean contains(String name) {
return zfIndex.contains(name);
}
public List<String> getFiles(String subdirectory) {
return zfIndex.getFiles((subdirectory.endsWith("/") || subdirectory.endsWith("\\")) ? subdirectory.substring(0, subdirectory.length() - 1) : subdirectory);
}
public JavaFileObject getFileObject(String subdirectory, String file) {
String fullZipFileName = subdirectory + file;
ZipFileIndex.Entry entry = zfIndex.getZipIndexEntry(fullZipFileName);
JavaFileObject ret = new ZipFileIndexFileObject(fileManager, zfIndex, entry, zfIndex.getZipFile().getPath());
return ret;
}
public Set<String> getSubdirectories() {
return zfIndex.getAllDirectories();
}
public void close() throws IOException {
zfIndex.close();
}
/**
* A subclass of JavaFileObject representing zip entries using the com.sun.tools.javac.file.ZipFileIndex implementation.
*/
public static class ZipFileIndexFileObject extends BaseFileObject {
/** The entry's name.
*/
private String name;
/** The zipfile containing the entry.
*/
ZipFileIndex zfIndex;
/** The underlying zip entry object.
*/
ZipFileIndex.Entry entry;
/** The InputStream for this zip entry (file.)
*/
InputStream inputStream = null;
/** The name of the zip file where this entry resides.
*/
String zipName;
ZipFileIndexFileObject(JavacFileManager fileManager, ZipFileIndex zfIndex, ZipFileIndex.Entry entry, String zipFileName) {
super(fileManager);
this.name = entry.getFileName();
this.zfIndex = zfIndex;
this.entry = entry;
this.zipName = zipFileName;
}
public InputStream openInputStream() throws IOException {
if (inputStream == null) {
inputStream = new ByteArrayInputStream(read());
}
return inputStream;
}
protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
}
public OutputStream openOutputStream() throws IOException {
throw new UnsupportedOperationException();
}
public Writer openWriter() throws IOException {
throw new UnsupportedOperationException();
}
/** @deprecated see bug 6410637 */
@Deprecated
public String getName() {
return name;
}
public boolean isNameCompatible(String cn, JavaFileObject.Kind k) {
cn.getClass(); // null check
if (k == Kind.OTHER && getKind() != k)
return false;
return name.equals(cn + k.extension);
}
/** @deprecated see bug 6410637 */
@Deprecated
public String getPath() {
return zipName + "(" + entry.getName() + ")";
}
public long getLastModified() {
return entry.getLastModified();
}
public boolean delete() {
throw new UnsupportedOperationException();
}
@Override
public boolean equals(Object other) {
if (!(other instanceof ZipFileIndexFileObject))
return false;
ZipFileIndexFileObject o = (ZipFileIndexFileObject) other;
return entry.equals(o.entry);
}
@Override
public int hashCode() {
return zipName.hashCode() + (name.hashCode() << 10);
}
public String getZipName() {
return zipName;
}
public String getZipEntryName() {
return entry.getName();
}
public URI toUri() {
String zipName = new File(getZipName()).toURI().normalize().getPath();
String entryName = getZipEntryName();
if (File.separatorChar != '/') {
entryName = entryName.replace(File.separatorChar, '/');
}
return URI.create("jar:" + zipName + "!" + entryName);
}
private byte[] read() throws IOException {
if (entry == null) {
entry = zfIndex.getZipIndexEntry(name);
if (entry == null)
throw new FileNotFoundException();
}
return zfIndex.read(entry);
}
public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
CharBuffer cb = fileManager.getCachedContent(this);
if (cb == null) {
InputStream in = new ByteArrayInputStream(zfIndex.read(entry));
try {
ByteBuffer bb = fileManager.makeByteBuffer(in);
JavaFileObject prev = fileManager.log.useSource(this);
try {
cb = fileManager.decode(bb, ignoreEncodingErrors);
} finally {
fileManager.log.useSource(prev);
}
fileManager.recycleByteBuffer(bb); // save for next time
if (!ignoreEncodingErrors)
fileManager.cache(this, cb);
} finally {
in.close();
}
}
return cb;
}
@Override
protected String inferBinaryName(Iterable<? extends File> path) {
String entryName = getZipEntryName();
if (zfIndex.symbolFilePrefix != null) {
String prefix = zfIndex.symbolFilePrefix;
if (entryName.startsWith(prefix))
entryName = entryName.substring(prefix.length());
}
return removeExtension(entryName).replace(File.separatorChar, '.');
}
}
}

View File

@ -1,116 +0,0 @@
/*
* Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
package com.sun.tools.javac.file;
import java.io.File;
public final class ZipFileIndexEntry implements Comparable<ZipFileIndexEntry> {
public static final ZipFileIndexEntry[] EMPTY_ARRAY = {};
// Directory related
String dir;
boolean isDir;
// File related
String name;
int offset;
int size;
int compressedSize;
long javatime;
private int nativetime;
public ZipFileIndexEntry(String path) {
int separator = path.lastIndexOf(File.separatorChar);
if (separator == -1) {
dir = "".intern();
name = path;
} else {
dir = path.substring(0, separator).intern();
name = path.substring(separator + 1);
}
}
public ZipFileIndexEntry(String directory, String name) {
this.dir = directory.intern();
this.name = name;
}
public String getName() {
if (dir == null || dir.length() == 0) {
return name;
}
StringBuilder sb = new StringBuilder();
sb.append(dir);
sb.append(File.separatorChar);
sb.append(name);
return sb.toString();
}
public String getFileName() {
return name;
}
public long getLastModified() {
if (javatime == 0) {
javatime = dosToJavaTime(nativetime);
}
return javatime;
}
// From java.util.zip
private static long dosToJavaTime(int nativetime) {
// Bootstrap build problems prevent me from using the code directly
// Convert the raw/native time to a long for now
return (long)nativetime;
}
void setNativeTime(int natTime) {
nativetime = natTime;
}
public boolean isDirectory() {
return isDir;
}
public int compareTo(ZipFileIndexEntry other) {
String otherD = other.dir;
if (dir != otherD) {
int c = dir.compareTo(otherD);
if (c != 0)
return c;
}
return name.compareTo(other.name);
}
public String toString() {
return isDir ? ("Dir:" + dir + " : " + name) :
(dir + ":" + name);
}
}

View File

@ -1056,7 +1056,7 @@ public class ClassReader extends ClassFile implements Completer {
void readClassAttr(ClassSymbol c, Name attrName, int attrLen) { void readClassAttr(ClassSymbol c, Name attrName, int attrLen) {
if (attrName == names.SourceFile) { if (attrName == names.SourceFile) {
Name n = readName(nextChar()); Name n = readName(nextChar());
c.sourcefile = new SourceFileObject(n); c.sourcefile = new SourceFileObject(n, c.flatname);
} else if (attrName == names.InnerClasses) { } else if (attrName == names.InnerClasses) {
readInnerClasses(c); readInnerClasses(c);
} else if (allowGenerics && attrName == names.Signature) { } else if (allowGenerics && attrName == names.Signature) {
@ -2221,9 +2221,12 @@ public class ClassReader extends ClassFile implements Completer {
/** The file's name. /** The file's name.
*/ */
private Name name; private Name name;
private Name flatname;
public SourceFileObject(Name name) { public SourceFileObject(Name name, Name flatname) {
super(null); // no file manager; never referenced for this file object
this.name = name; this.name = name;
this.flatname = flatname;
} }
public InputStream openInputStream() { public InputStream openInputStream() {
@ -2285,5 +2288,9 @@ public class ClassReader extends ClassFile implements Completer {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
protected String inferBinaryName(Iterable<? extends File> path) {
return flatname.toString();
}
} }
} }

View File

@ -34,7 +34,6 @@ import java.util.Set;
import javax.tools.DiagnosticListener; import javax.tools.DiagnosticListener;
import javax.tools.JavaFileObject; import javax.tools.JavaFileObject;
import com.sun.tools.javac.code.Source;
import com.sun.tools.javac.file.JavacFileManager; import com.sun.tools.javac.file.JavacFileManager;
import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
@ -86,10 +85,6 @@ public class Log {
*/ */
public boolean emitWarnings; public boolean emitWarnings;
/** Enforce mandatory warnings.
*/
private boolean enforceMandatoryWarnings;
/** Print stack trace on errors? /** Print stack trace on errors?
*/ */
public boolean dumpOnError; public boolean dumpOnError;
@ -138,9 +133,6 @@ public class Log {
DiagnosticListener<? super JavaFileObject> diagListener = DiagnosticListener<? super JavaFileObject> diagListener =
context.get(DiagnosticListener.class); context.get(DiagnosticListener.class);
this.diagListener = diagListener; this.diagListener = diagListener;
Source source = Source.instance(context);
this.enforceMandatoryWarnings = source.enforceMandatoryWarnings();
} }
// where // where
private int getIntOption(Options options, String optionName, int defaultValue) { private int getIntOption(Options options, String optionName, int defaultValue) {
@ -473,10 +465,7 @@ public class Log {
* @param args Fields of the warning message. * @param args Fields of the warning message.
*/ */
public void mandatoryWarning(DiagnosticPosition pos, String key, Object ... args) { public void mandatoryWarning(DiagnosticPosition pos, String key, Object ... args) {
if (enforceMandatoryWarnings)
report(diags.mandatoryWarning(source, pos, key, args)); report(diags.mandatoryWarning(source, pos, key, args));
else
report(diags.warning(source, pos, key, args));
} }
/** Report a warning that cannot be suppressed. /** Report a warning that cannot be suppressed.
@ -513,14 +502,28 @@ public class Log {
report(diags.note(source, wrap(pos), key, args)); report(diags.note(source, wrap(pos), key, args));
} }
/** Provide a non-fatal notification, unless suppressed by the -nowarn option.
* @param file The file to which the note applies.
* @param key The key for the localized notification message.
* @param args Fields of the notification message.
*/
public void note(JavaFileObject file, String key, Object ... args) {
report(diags.note(wrap(file), null, key, args));
}
/** Provide a non-fatal notification, unless suppressed by the -nowarn option. /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
* @param key The key for the localized notification message. * @param key The key for the localized notification message.
* @param args Fields of the notification message. * @param args Fields of the notification message.
*/ */
public void mandatoryNote(final JavaFileObject file, String key, Object ... args) { public void mandatoryNote(final JavaFileObject file, String key, Object ... args) {
JCDiagnostic.DiagnosticSource wrapper = null; report(diags.mandatoryNote(wrap(file), key, args));
if (file != null) { }
wrapper = new JCDiagnostic.DiagnosticSource() {
private JCDiagnostic.DiagnosticSource wrap(final JavaFileObject file) {
if (file == null) {
return null;
}
return new JCDiagnostic.DiagnosticSource() {
public JavaFileObject getFile() { public JavaFileObject getFile() {
return file; return file;
} }
@ -538,11 +541,6 @@ public class Log {
} }
}; };
} }
if (enforceMandatoryWarnings)
report(diags.mandatoryNote(wrapper, key, args));
else
report(diags.note(wrapper, null, key, args));
}
private DiagnosticPosition wrap(int pos) { private DiagnosticPosition wrap(int pos) {
return (pos == Position.NOPOS ? null : new SimpleDiagnosticPosition(pos)); return (pos == Position.NOPOS ? null : new SimpleDiagnosticPosition(pos));

View File

@ -101,13 +101,17 @@ public class MandatoryWarningHandler {
* individual instances should be given, or whether an aggregate * individual instances should be given, or whether an aggregate
* message should be generated at the end of the compilation. * message should be generated at the end of the compilation.
* Typically set via -Xlint:option. * Typically set via -Xlint:option.
* @param enforceMandatory
* True if mandatory warnings and notes are being enforced.
* @param prefix A common prefix for the set of message keys for * @param prefix A common prefix for the set of message keys for
* the messages that may be generated. * the messages that may be generated.
*/ */
public MandatoryWarningHandler(Log log, boolean verbose, String prefix) { public MandatoryWarningHandler(Log log, boolean verbose,
boolean enforceMandatory, String prefix) {
this.log = log; this.log = log;
this.verbose = verbose; this.verbose = verbose;
this.prefix = prefix; this.prefix = prefix;
this.enforceMandatory = enforceMandatory;
} }
/** /**
@ -122,7 +126,7 @@ public class MandatoryWarningHandler {
if (log.nwarnings < log.MaxWarnings) { if (log.nwarnings < log.MaxWarnings) {
// generate message and remember the source file // generate message and remember the source file
log.mandatoryWarning(pos, msg, args); logMandatoryWarning(pos, msg, args);
sourcesWithReportedWarnings.add(currentSource); sourcesWithReportedWarnings.add(currentSource);
} else if (deferredDiagnosticKind == null) { } else if (deferredDiagnosticKind == null) {
// set up deferred message // set up deferred message
@ -163,12 +167,12 @@ public class MandatoryWarningHandler {
public void reportDeferredDiagnostic() { public void reportDeferredDiagnostic() {
if (deferredDiagnosticKind != null) { if (deferredDiagnosticKind != null) {
if (deferredDiagnosticArg == null) if (deferredDiagnosticArg == null)
log.mandatoryNote(deferredDiagnosticSource, deferredDiagnosticKind.getKey(prefix)); logMandatoryNote(deferredDiagnosticSource, deferredDiagnosticKind.getKey(prefix));
else else
log.mandatoryNote(deferredDiagnosticSource, deferredDiagnosticKind.getKey(prefix), deferredDiagnosticArg); logMandatoryNote(deferredDiagnosticSource, deferredDiagnosticKind.getKey(prefix), deferredDiagnosticArg);
if (!verbose) if (!verbose)
log.mandatoryNote(deferredDiagnosticSource, prefix + ".recompile"); logMandatoryNote(deferredDiagnosticSource, prefix + ".recompile");
} }
} }
@ -224,4 +228,32 @@ public class MandatoryWarningHandler {
* deferredDiagnosticKind is updated. * deferredDiagnosticKind is updated.
*/ */
private Object deferredDiagnosticArg; private Object deferredDiagnosticArg;
/**
* True if mandatory warnings and notes are being enforced.
*/
private final boolean enforceMandatory;
/**
* Reports a mandatory warning to the log. If mandatory warnings
* are not being enforced, treat this as an ordinary warning.
*/
private void logMandatoryWarning(DiagnosticPosition pos, String msg,
Object... args) {
if (enforceMandatory)
log.mandatoryWarning(pos, msg, args);
else
log.warning(pos, msg, args);
}
/**
* Reports a mandatory note to the log. If mandatory notes are
* not being enforced, treat this as an ordinary note.
*/
private void logMandatoryNote(JavaFileObject file, String msg, Object... args) {
if (enforceMandatory)
log.mandatoryNote(file, msg, args);
else
log.note(file, msg, args);
}
} }

View File

@ -27,7 +27,9 @@ package com.sun.tools.javadoc;
import com.sun.tools.javac.code.Symbol.PackageSymbol; import com.sun.tools.javac.code.Symbol.PackageSymbol;
import com.sun.tools.javac.file.JavacFileManager; import com.sun.tools.javac.file.JavacFileManager;
import com.sun.tools.javac.file.ZipArchive.ZipFileObject;
import com.sun.tools.javac.file.Old199; import com.sun.tools.javac.file.Old199;
import com.sun.tools.javac.file.ZipFileIndexArchive;
import com.sun.tools.javac.jvm.ClassReader; import com.sun.tools.javac.jvm.ClassReader;
import com.sun.tools.javac.util.Context; import com.sun.tools.javac.util.Context;
@ -82,16 +84,16 @@ class JavadocClassReader extends ClassReader {
protected void extraFileActions(PackageSymbol pack, JavaFileObject fo) { protected void extraFileActions(PackageSymbol pack, JavaFileObject fo) {
CharSequence fileName = Old199.getName(fo); CharSequence fileName = Old199.getName(fo);
if (docenv != null && fileName.equals("package.html")) { if (docenv != null && fileName.equals("package.html")) {
if (fo instanceof JavacFileManager.ZipFileObject) { if (fo instanceof ZipFileObject) {
JavacFileManager.ZipFileObject zfo = (JavacFileManager.ZipFileObject) fo; ZipFileObject zfo = (ZipFileObject) fo;
String zipName = zfo.getZipName(); String zipName = zfo.getZipName();
String entryName = zfo.getZipEntryName(); String entryName = zfo.getZipEntryName();
int lastSep = entryName.lastIndexOf("/"); int lastSep = entryName.lastIndexOf("/");
String classPathName = entryName.substring(0, lastSep + 1); String classPathName = entryName.substring(0, lastSep + 1);
docenv.getPackageDoc(pack).setDocPath(zipName, classPathName); docenv.getPackageDoc(pack).setDocPath(zipName, classPathName);
} }
else if (fo instanceof JavacFileManager.ZipFileIndexFileObject) { else if (fo instanceof ZipFileIndexArchive.ZipFileIndexFileObject) {
JavacFileManager.ZipFileIndexFileObject zfo = (JavacFileManager.ZipFileIndexFileObject) fo; ZipFileIndexArchive.ZipFileIndexFileObject zfo = (ZipFileIndexArchive.ZipFileIndexFileObject) fo;
String zipName = zfo.getZipName(); String zipName = zfo.getZipName();
String entryName = zfo.getZipEntryName(); String entryName = zfo.getZipEntryName();
if (File.separatorChar != '/') { if (File.separatorChar != '/') {

View File

@ -25,16 +25,13 @@
package com.sun.tools.javap; package com.sun.tools.javap;
import java.io.File;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener; import javax.tools.DiagnosticListener;
import javax.tools.JavaFileObject; import javax.tools.JavaFileObject;
import com.sun.tools.javac.file.JavacFileManager; import com.sun.tools.javac.file.JavacFileManager;
import com.sun.tools.javac.util.Context; import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.JCDiagnostic;
/** /**
* javap's implementation of JavaFileManager. * javap's implementation of JavaFileManager.
@ -52,29 +49,8 @@ class JavapFileManager extends JavacFileManager {
static JavapFileManager create(final DiagnosticListener<? super JavaFileObject> dl, PrintWriter log, Options options) { static JavapFileManager create(final DiagnosticListener<? super JavaFileObject> dl, PrintWriter log, Options options) {
Context javac_context = new Context(); Context javac_context = new Context();
if (dl != null) { if (dl != null)
// Workaround bug 6625520: javac handles missing entries on classpath badly javac_context.put(DiagnosticListener.class, dl);
// Ignore spurious errors for missing files
DiagnosticListener<JavaFileObject> wrapper = new DiagnosticListener<JavaFileObject>() {
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
if (diagnostic instanceof JCDiagnostic) {
JCDiagnostic jcd = (JCDiagnostic) diagnostic;
if (jcd.getCode().equals("compiler.err.error.reading.file")) {
Object[] args = jcd.getArgs();
if (args.length > 0 && args[0] != null && args[0].toString().length() > 0) {
File f = new File(args[0].toString());
if (!f.exists())
return;
}
}
}
dl.report(diagnostic);
}
};
javac_context.put(DiagnosticListener.class, wrapper);
}
javac_context.put(com.sun.tools.javac.util.Log.outKey, log); javac_context.put(com.sun.tools.javac.util.Log.outKey, log);
return new JavapFileManager(javac_context, null); return new JavapFileManager(javac_context, null);

View File

@ -36,6 +36,7 @@ import javax.tools.*;
import com.sun.source.util.*; import com.sun.source.util.*;
import com.sun.tools.javac.api.*; import com.sun.tools.javac.api.*;
import com.sun.tools.javac.file.*; import com.sun.tools.javac.file.*;
import com.sun.tools.javac.file.JavacFileManager;
import com.sun.tools.javac.main.*; import com.sun.tools.javac.main.*;
import com.sun.tools.javac.util.*; import com.sun.tools.javac.util.*;

View File

@ -33,6 +33,7 @@ import javax.annotation.processing.*;
import javax.lang.model.element.*; import javax.lang.model.element.*;
import javax.tools.*; import javax.tools.*;
import com.sun.tools.javac.file.*; import com.sun.tools.javac.file.*;
import com.sun.tools.javac.file.JavacFileManager; // disambiguate
import com.sun.tools.javac.main.JavaCompiler; import com.sun.tools.javac.main.JavaCompiler;
import com.sun.tools.javac.main.*; import com.sun.tools.javac.main.*;
import com.sun.tools.javac.util.*; import com.sun.tools.javac.util.*;

View File

@ -34,6 +34,7 @@ import javax.annotation.processing.*;
import javax.lang.model.element.*; import javax.lang.model.element.*;
import javax.tools.*; import javax.tools.*;
import com.sun.tools.javac.file.*; import com.sun.tools.javac.file.*;
import com.sun.tools.javac.file.JavacFileManager;
import com.sun.tools.javac.main.JavaCompiler; import com.sun.tools.javac.main.JavaCompiler;
import com.sun.tools.javac.main.*; import com.sun.tools.javac.main.*;
import com.sun.tools.javac.util.*; import com.sun.tools.javac.util.*;

View File

@ -0,0 +1,55 @@
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
import java.io.*;
import java.util.*;
import javax.tools.*;
import com.sun.tools.javac.file.*;
import com.sun.tools.javac.file.JavacFileManager;
import com.sun.tools.javac.util.*;
/*
* @test
* @bug 6625520
* @summary javac handles missing entries on classpath badly
*/
public class T6625520 {
public static void main(String[] args) throws Exception {
new T6625520().run();
}
void run() throws Exception {
Context c = new Context();
DiagnosticCollector<JavaFileObject> dc =
new DiagnosticCollector<JavaFileObject>();
c.put(DiagnosticListener.class, dc);
StandardJavaFileManager fm = new JavacFileManager(c, false, null);
fm.setLocation(StandardLocation.CLASS_PATH,
Arrays.asList(new File("DOES_NOT_EXIST.jar")));
FileObject fo = fm.getFileForInput(StandardLocation.CLASS_PATH,
"p", "C.java");
System.err.println(fo + "\n" + dc.getDiagnostics());
if (dc.getDiagnostics().size() > 0)
throw new Exception("unexpected diagnostics found");
}
}

View File

@ -0,0 +1,49 @@
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/*
* @test
* @bug 6294779
* @summary Problem with interface inheritance and covariant return types
* @author Maurizio Cimadamore
* @compile T6294779a.java
*/
public class T6294779a {
interface A {
A m();
}
interface B extends A {
B m();
}
interface C extends A {
C m();
}
interface D extends B, C {
D m();
}
}

View File

@ -0,0 +1,49 @@
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/*
* @test
* @bug 6294779
* @summary Problem with interface inheritance and covariant return types
* @author Maurizio Cimadamore
* @compile T6294779b.java
*/
import java.util.*;
class T6294779b {
interface I1<E> {
List<E> m();
}
interface I2<E> {
Queue<E> m();
}
interface I3<E> {
LinkedList<E> m();
}
interface I4<E> extends I1<E>, I2<E>, I3<E> {}
}

View File

@ -0,0 +1,53 @@
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/*
* @test
* @bug 6294779
* @summary Problem with interface inheritance and covariant return types
* @author Maurizio Cimadamore
* @compile/fail T6294779c.java
*/
public class T6294779c<X> {
interface A {}
interface B {}
interface C {}
interface I1 {
T6294779c<? extends A> get();
}
interface I2 {
T6294779c<? extends B> get();
}
interface I3 {
T6294779c<? extends C> get();
}
interface I4 extends I1, I2, I3 {}
}

View File

@ -0,0 +1,47 @@
/*
* Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/*
* @test
* @bug 6507179
* @summary Ensure that "-source" option isn't ignored.
* @author Scott Seligman
*/
import com.sun.javadoc.*;
public class SourceOption extends Doclet {
public static void main(String[] args) {
if (com.sun.tools.javadoc.Main.execute(
"javadoc",
"SourceOption",
new String[] {"-source", "1.3", "p"}) != 0)
throw new Error("Javadoc encountered warnings or errors.");
}
public static boolean start(RootDoc root) {
root.classes(); // force parser into action
return true;
}
}

View File

@ -0,0 +1,29 @@
/*
* Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
package p;
public class A {
boolean assert; // illegal since 1.4
boolean enum; // illegal since 5
}