提交 18c5f1f2 编写于 作者: T tbell

Merge

...@@ -95,6 +95,7 @@ public class Kinds { ...@@ -95,6 +95,7 @@ public class Kinds {
ANNOTATION("kindname.interface"), ANNOTATION("kindname.interface"),
CONSTRUCTOR("kindname.constructor"), CONSTRUCTOR("kindname.constructor"),
INTERFACE("kindname.interface"), INTERFACE("kindname.interface"),
ENUM("kindname.enum"),
STATIC("kindname.static"), STATIC("kindname.static"),
TYPEVAR("kindname.type.variable"), TYPEVAR("kindname.type.variable"),
BOUND("kindname.type.variable.bound"), BOUND("kindname.type.variable.bound"),
...@@ -145,11 +146,15 @@ public class Kinds { ...@@ -145,11 +146,15 @@ public class Kinds {
return KindName.PACKAGE; return KindName.PACKAGE;
case ENUM: case ENUM:
return KindName.ENUM;
case ANNOTATION_TYPE: case ANNOTATION_TYPE:
case INTERFACE:
case CLASS: case CLASS:
return KindName.CLASS; return KindName.CLASS;
case INTERFACE:
return KindName.INTERFACE;
case TYPE_PARAMETER: case TYPE_PARAMETER:
return KindName.TYPEVAR; return KindName.TYPEVAR;
...@@ -160,8 +165,10 @@ public class Kinds { ...@@ -160,8 +165,10 @@ public class Kinds {
case EXCEPTION_PARAMETER: case EXCEPTION_PARAMETER:
return KindName.VAR; return KindName.VAR;
case METHOD:
case CONSTRUCTOR: case CONSTRUCTOR:
return KindName.CONSTRUCTOR;
case METHOD:
case STATIC_INIT: case STATIC_INIT:
case INSTANCE_INIT: case INSTANCE_INIT:
return KindName.METHOD; return KindName.METHOD;
......
...@@ -303,7 +303,7 @@ public class Attr extends JCTree.Visitor { ...@@ -303,7 +303,7 @@ public class Attr extends JCTree.Visitor {
public Env<AttrContext> attribExprToTree(JCTree expr, Env<AttrContext> env, JCTree tree) { public Env<AttrContext> attribExprToTree(JCTree expr, Env<AttrContext> env, JCTree tree) {
breakTree = tree; breakTree = tree;
JavaFileObject prev = log.useSource(null); JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
try { try {
attribExpr(expr, env); attribExpr(expr, env);
} catch (BreakAttr b) { } catch (BreakAttr b) {
...@@ -317,7 +317,7 @@ public class Attr extends JCTree.Visitor { ...@@ -317,7 +317,7 @@ public class Attr extends JCTree.Visitor {
public Env<AttrContext> attribStatToTree(JCTree stmt, Env<AttrContext> env, JCTree tree) { public Env<AttrContext> attribStatToTree(JCTree stmt, Env<AttrContext> env, JCTree tree) {
breakTree = tree; breakTree = tree;
JavaFileObject prev = log.useSource(null); JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
try { try {
attribStat(stmt, env); attribStat(stmt, env);
} catch (BreakAttr b) { } catch (BreakAttr b) {
......
...@@ -109,7 +109,7 @@ public abstract class AbstractDiagnosticFormatter implements DiagnosticFormatter ...@@ -109,7 +109,7 @@ public abstract class AbstractDiagnosticFormatter implements DiagnosticFormatter
return formatDiagnostic(d, locale); return formatDiagnostic(d, locale);
} }
abstract String formatDiagnostic(JCDiagnostic d, Locale locale); protected abstract String formatDiagnostic(JCDiagnostic d, Locale locale);
public String formatPosition(JCDiagnostic d, PositionKind pk,Locale l) { public String formatPosition(JCDiagnostic d, PositionKind pk,Locale l) {
assert (d.getPosition() != Position.NOPOS); assert (d.getPosition() != Position.NOPOS);
......
...@@ -58,7 +58,7 @@ public abstract class AbstractLog { ...@@ -58,7 +58,7 @@ public abstract class AbstractLog {
protected DiagnosticSource getSource(JavaFileObject file) { protected DiagnosticSource getSource(JavaFileObject file) {
if (file == null) if (file == null)
return null; return DiagnosticSource.NO_SOURCE;
DiagnosticSource s = sourceMap.get(file); DiagnosticSource s = sourceMap.get(file);
if (s == null) { if (s == null) {
s = new DiagnosticSource(file, this); s = new DiagnosticSource(file, this);
......
...@@ -46,11 +46,22 @@ import static com.sun.tools.javac.util.LayoutCharacters.*; ...@@ -46,11 +46,22 @@ import static com.sun.tools.javac.util.LayoutCharacters.*;
* deletion without notice.</b> * deletion without notice.</b>
*/ */
public class DiagnosticSource { public class DiagnosticSource {
/* constant DiagnosticSource to be used when sourcefile is missing */
public static final DiagnosticSource NO_SOURCE = new DiagnosticSource() {
@Override
protected boolean findLine(int pos) {
return false;
}
};
public DiagnosticSource(JavaFileObject fo, AbstractLog log) { public DiagnosticSource(JavaFileObject fo, AbstractLog log) {
this.fileObject = fo; this.fileObject = fo;
this.log = log; this.log = log;
} }
private DiagnosticSource() {}
/** Return the underlying file object handled by this /** Return the underlying file object handled by this
* DiagnosticSource object. * DiagnosticSource object.
*/ */
...@@ -134,7 +145,7 @@ public class DiagnosticSource { ...@@ -134,7 +145,7 @@ public class DiagnosticSource {
/** Find the line in the buffer that contains the current position /** Find the line in the buffer that contains the current position
* @param pos Character offset into the buffer * @param pos Character offset into the buffer
*/ */
private boolean findLine(int pos) { protected boolean findLine(int pos) {
if (pos == Position.NOPOS) if (pos == Position.NOPOS)
return false; return false;
......
...@@ -83,7 +83,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> { ...@@ -83,7 +83,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> {
*/ */
public JCDiagnostic error( public JCDiagnostic error(
DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
return new JCDiagnostic(formatter, ERROR, true, source, pos, qualify(ERROR, key), args); return create(ERROR, true, source, pos, key, args);
} }
/** /**
...@@ -96,7 +96,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> { ...@@ -96,7 +96,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> {
*/ */
public JCDiagnostic mandatoryWarning( public JCDiagnostic mandatoryWarning(
DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
return new JCDiagnostic(formatter, WARNING, true, source, pos, qualify(WARNING, key), args); return create(WARNING, true, source, pos, key, args);
} }
/** /**
...@@ -108,7 +108,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> { ...@@ -108,7 +108,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> {
*/ */
public JCDiagnostic warning( public JCDiagnostic warning(
DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
return new JCDiagnostic(formatter, WARNING, false, source, pos, qualify(WARNING, key), args); return create(WARNING, false, source, pos, key, args);
} }
/** /**
...@@ -118,7 +118,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> { ...@@ -118,7 +118,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> {
* @see MandatoryWarningHandler * @see MandatoryWarningHandler
*/ */
public JCDiagnostic mandatoryNote(DiagnosticSource source, String key, Object... args) { public JCDiagnostic mandatoryNote(DiagnosticSource source, String key, Object... args) {
return new JCDiagnostic(formatter, NOTE, true, source, null, qualify(NOTE, key), args); return create(NOTE, true, source, null, key, args);
} }
/** /**
...@@ -127,7 +127,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> { ...@@ -127,7 +127,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> {
* @param args Fields of the error message. * @param args Fields of the error message.
*/ */
public JCDiagnostic note(String key, Object... args) { public JCDiagnostic note(String key, Object... args) {
return note(null, null, key, args); return create(NOTE, false, null, null, key, args);
} }
/** /**
...@@ -139,7 +139,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> { ...@@ -139,7 +139,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> {
*/ */
public JCDiagnostic note( public JCDiagnostic note(
DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
return new JCDiagnostic(formatter, NOTE, false, source, pos, qualify(NOTE, key), args); return create(NOTE, false, source, pos, key, args);
} }
/** /**
...@@ -148,7 +148,21 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> { ...@@ -148,7 +148,21 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> {
* @param args Fields of the error message. * @param args Fields of the error message.
*/ */
public JCDiagnostic fragment(String key, Object... args) { public JCDiagnostic fragment(String key, Object... args) {
return new JCDiagnostic(formatter, FRAGMENT, false, null, null, qualify(FRAGMENT, key), args); return create(FRAGMENT, false, null, null, key, args);
}
/**
* Create a new diagnostic of the given kind.
* @param kind The diagnostic kind
* @param isMandatory is diagnostic mandatory?
* @param source The source of the compilation unit, if any, in which to report the note.
* @param pos The source position at which to report the note.
* @param key The key for the localized error message.
* @param args Fields of the error message.
*/
public JCDiagnostic create(
DiagnosticType kind, boolean isMandatory, DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
return new JCDiagnostic(formatter, kind, isMandatory, source, pos, qualify(kind, key), args);
} }
protected String qualify(DiagnosticType t, String key) { protected String qualify(DiagnosticType t, String key) {
......
...@@ -68,7 +68,10 @@ public class RichDiagnosticFormatter extends ...@@ -68,7 +68,10 @@ public class RichDiagnosticFormatter extends
final JavacMessages messages; final JavacMessages messages;
/* name simplifier used by this formatter */ /* name simplifier used by this formatter */
ClassNameSimplifier nameSimplifier; protected ClassNameSimplifier nameSimplifier;
/* type/symbol printer used by this formatter */
private RichPrinter printer;
/* map for keeping track of a where clause associated to a given type */ /* map for keeping track of a where clause associated to a given type */
Map<WhereClauseKind, Map<Type, JCDiagnostic>> whereClauses; Map<WhereClauseKind, Map<Type, JCDiagnostic>> whereClauses;
...@@ -83,7 +86,7 @@ public class RichDiagnosticFormatter extends ...@@ -83,7 +86,7 @@ public class RichDiagnosticFormatter extends
protected RichDiagnosticFormatter(Context context) { protected RichDiagnosticFormatter(Context context) {
super((AbstractDiagnosticFormatter)Log.instance(context).getDiagnosticFormatter()); super((AbstractDiagnosticFormatter)Log.instance(context).getDiagnosticFormatter());
this.formatter.setPrinter(printer); setRichPrinter(new RichPrinter());
this.syms = Symtab.instance(context); this.syms = Symtab.instance(context);
this.diags = JCDiagnostic.Factory.instance(context); this.diags = JCDiagnostic.Factory.instance(context);
this.types = Types.instance(context); this.types = Types.instance(context);
...@@ -116,6 +119,23 @@ public class RichDiagnosticFormatter extends ...@@ -116,6 +119,23 @@ public class RichDiagnosticFormatter extends
return sb.toString(); return sb.toString();
} }
/**
* Sets the type/symbol printer used by this formatter.
* @param printer the rich printer to be set
*/
protected void setRichPrinter(RichPrinter printer) {
this.printer = printer;
formatter.setPrinter(printer);
}
/**
* Gets the type/symbol printer used by this formatter.
* @return type/symbol rich printer
*/
protected RichPrinter getRichPrinter() {
return printer;
}
/** /**
* Preprocess a given diagnostic by looking both into its arguments and into * Preprocess a given diagnostic by looking both into its arguments and into
* its subdiagnostics (if any). This preprocessing is responsible for * its subdiagnostics (if any). This preprocessing is responsible for
...@@ -217,7 +237,7 @@ public class RichDiagnosticFormatter extends ...@@ -217,7 +237,7 @@ public class RichDiagnosticFormatter extends
* name belong to different packages - in this case the formatter reverts * name belong to different packages - in this case the formatter reverts
* to fullnames as compact names might lead to a confusing diagnostic. * to fullnames as compact names might lead to a confusing diagnostic.
*/ */
class ClassNameSimplifier { protected class ClassNameSimplifier {
/* table for keeping track of all short name usages */ /* table for keeping track of all short name usages */
Map<Name, List<Symbol>> nameClashes = new HashMap<Name, List<Symbol>>(); Map<Name, List<Symbol>> nameClashes = new HashMap<Name, List<Symbol>>();
...@@ -272,7 +292,7 @@ public class RichDiagnosticFormatter extends ...@@ -272,7 +292,7 @@ public class RichDiagnosticFormatter extends
* discovered during type/symbol preprocessing. This printer is set on the delegate * discovered during type/symbol preprocessing. This printer is set on the delegate
* formatter so that rich type/symbol info can be properly rendered. * formatter so that rich type/symbol info can be properly rendered.
*/ */
protected Printer printer = new Printer() { protected class RichPrinter extends Printer {
@Override @Override
public String localize(Locale locale, String key, Object... args) { public String localize(Locale locale, String key, Object... args) {
......
/*
* Copyright 2009 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 6852595
* @summary Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
* @author mcimadamore
*/
import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.ToolProvider;
import com.sun.source.util.JavacTask;
import com.sun.source.tree.*;
import com.sun.source.util.TreePath;
import com.sun.source.util.Trees;
import com.sun.tools.javac.api.JavacTrees;
import com.sun.tools.javac.tree.JCTree.*;
import static javax.tools.JavaFileObject.Kind;
public class T6852595 {
public static void main(String[] args) throws IOException {
JavaFileObject sfo = new SimpleJavaFileObject(URI.create("myfo:/Test.java"),Kind.SOURCE) {
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return "class BadName { Object o = j; }";
}
};
List<? extends JavaFileObject> files = Arrays.asList(sfo);
JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, files);
Iterable<? extends CompilationUnitTree> compUnits = ct.parse();
CompilationUnitTree cu = compUnits.iterator().next();
ClassTree cdef = (ClassTree)cu.getTypeDecls().get(0);
JCVariableDecl vdef = (JCVariableDecl)cdef.getMembers().get(0);
TreePath path = TreePath.getPath(cu, vdef.init);
Trees.instance(ct).getScope(path);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册