Merge jdk8-b06

Mon, 19 Sep 2011 19:41:46 -0700

author
lana
date
Mon, 19 Sep 2011 19:41:46 -0700
changeset 1082
d2422276f9da
parent 1070
4e754e4b0a52
parent 1081
f1431cace56e
child 1083
116980ecec5c
child 1098
0c6f79fc8441

Merge

     1.1 --- a/src/share/classes/com/sun/tools/javac/api/ClientCodeWrapper.java	Thu Sep 15 18:53:41 2011 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javac/api/ClientCodeWrapper.java	Mon Sep 19 19:41:46 2011 -0700
     1.3 @@ -37,6 +37,7 @@
     1.4  import java.util.HashMap;
     1.5  import java.util.Iterator;
     1.6  import java.util.List;
     1.7 +import java.util.Locale;
     1.8  import java.util.Map;
     1.9  import java.util.Set;
    1.10  
    1.11 @@ -51,6 +52,7 @@
    1.12  import com.sun.source.util.TaskListener;
    1.13  import com.sun.tools.javac.util.ClientCodeException;
    1.14  import com.sun.tools.javac.util.Context;
    1.15 +import com.sun.tools.javac.util.JCDiagnostic;
    1.16  import java.lang.annotation.ElementType;
    1.17  import java.lang.annotation.Retention;
    1.18  import java.lang.annotation.RetentionPolicy;
    1.19 @@ -146,7 +148,7 @@
    1.20              return fo;
    1.21      }
    1.22  
    1.23 -    <T> DiagnosticListener<T> wrap(DiagnosticListener<T> dl) {
    1.24 +    <T /*super JavaFileOject*/> DiagnosticListener<T> wrap(DiagnosticListener<T> dl) {
    1.25          if (isTrusted(dl))
    1.26              return dl;
    1.27          return new WrappedDiagnosticListener<T>(dl);
    1.28 @@ -158,6 +160,16 @@
    1.29          return new WrappedTaskListener(tl);
    1.30      }
    1.31  
    1.32 +    @SuppressWarnings("unchecked")
    1.33 +    private <T> Diagnostic<T> unwrap(final Diagnostic<T> diagnostic) {
    1.34 +        if (diagnostic instanceof JCDiagnostic) {
    1.35 +            JCDiagnostic d = (JCDiagnostic) diagnostic;
    1.36 +            return (Diagnostic<T>) new DiagnosticSourceUnwrapper(d);
    1.37 +        } else {
    1.38 +            return diagnostic;
    1.39 +        }
    1.40 +    }
    1.41 +
    1.42      protected boolean isTrusted(Object o) {
    1.43          Class<?> c = o.getClass();
    1.44          Boolean trusted = trustedClasses.get(c);
    1.45 @@ -534,7 +546,7 @@
    1.46          }
    1.47      }
    1.48  
    1.49 -    protected class WrappedDiagnosticListener<T> implements DiagnosticListener<T> {
    1.50 +    protected class WrappedDiagnosticListener<T /*super JavaFileObject*/> implements DiagnosticListener<T> {
    1.51          protected DiagnosticListener<T> clientDiagnosticListener;
    1.52          WrappedDiagnosticListener(DiagnosticListener<T> clientDiagnosticListener) {
    1.53              clientDiagnosticListener.getClass(); // null check
    1.54 @@ -544,7 +556,7 @@
    1.55          @Override
    1.56          public void report(Diagnostic<? extends T> diagnostic) {
    1.57              try {
    1.58 -                clientDiagnosticListener.report(diagnostic);
    1.59 +                clientDiagnosticListener.report(unwrap(diagnostic));
    1.60              } catch (ClientCodeException e) {
    1.61                  throw e;
    1.62              } catch (RuntimeException e) {
    1.63 @@ -555,6 +567,54 @@
    1.64          }
    1.65      }
    1.66  
    1.67 +    public class DiagnosticSourceUnwrapper implements Diagnostic<JavaFileObject> {
    1.68 +        public final JCDiagnostic d;
    1.69 +
    1.70 +        DiagnosticSourceUnwrapper(JCDiagnostic d) {
    1.71 +            this.d = d;
    1.72 +        }
    1.73 +
    1.74 +        public Diagnostic.Kind getKind() {
    1.75 +            return d.getKind();
    1.76 +        }
    1.77 +
    1.78 +        public JavaFileObject getSource() {
    1.79 +            return unwrap(d.getSource());
    1.80 +        }
    1.81 +
    1.82 +        public long getPosition() {
    1.83 +            return d.getPosition();
    1.84 +        }
    1.85 +
    1.86 +        public long getStartPosition() {
    1.87 +            return d.getStartPosition();
    1.88 +        }
    1.89 +
    1.90 +        public long getEndPosition() {
    1.91 +            return d.getEndPosition();
    1.92 +        }
    1.93 +
    1.94 +        public long getLineNumber() {
    1.95 +            return d.getLineNumber();
    1.96 +        }
    1.97 +
    1.98 +        public long getColumnNumber() {
    1.99 +            return d.getColumnNumber();
   1.100 +        }
   1.101 +
   1.102 +        public String getCode() {
   1.103 +            return d.getCode();
   1.104 +        }
   1.105 +
   1.106 +        public String getMessage(Locale locale) {
   1.107 +            return d.getMessage(locale);
   1.108 +        }
   1.109 +
   1.110 +        public String toString() {
   1.111 +            return d.toString();
   1.112 +        }
   1.113 +    }
   1.114 +
   1.115      protected class WrappedTaskListener implements TaskListener {
   1.116          protected TaskListener clientTaskListener;
   1.117          WrappedTaskListener(TaskListener clientTaskListener) {
     2.1 --- a/src/share/classes/com/sun/tools/javac/code/Types.java	Thu Sep 15 18:53:41 2011 -0700
     2.2 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java	Mon Sep 19 19:41:46 2011 -0700
     2.3 @@ -269,10 +269,12 @@
     2.4  
     2.5      // <editor-fold defaultstate="collapsed" desc="isConvertible">
     2.6      /**
     2.7 -     * Is t a subtype of or convertiable via boxing/unboxing
     2.8 -     * convertions to s?
     2.9 +     * Is t a subtype of or convertible via boxing/unboxing
    2.10 +     * conversion to s?
    2.11       */
    2.12      public boolean isConvertible(Type t, Type s, Warner warn) {
    2.13 +        if (t.tag == ERROR)
    2.14 +            return true;
    2.15          boolean tPrimitive = t.isPrimitive();
    2.16          boolean sPrimitive = s.isPrimitive();
    2.17          if (tPrimitive == sPrimitive) {
    2.18 @@ -2117,6 +2119,8 @@
    2.19              }
    2.20          }
    2.21  
    2.22 +        List<TypeSymbol> seenTypes = List.nil();
    2.23 +
    2.24          /** members closure visitor methods **/
    2.25  
    2.26          public CompoundScope visitType(Type t, Boolean skipInterface) {
    2.27 @@ -2125,21 +2129,33 @@
    2.28  
    2.29          @Override
    2.30          public CompoundScope visitClassType(ClassType t, Boolean skipInterface) {
    2.31 -            ClassSymbol csym = (ClassSymbol)t.tsym;
    2.32 -            Entry e = _map.get(csym);
    2.33 -            if (e == null || !e.matches(skipInterface)) {
    2.34 -                CompoundScope membersClosure = new CompoundScope(csym);
    2.35 -                if (!skipInterface) {
    2.36 -                    for (Type i : interfaces(t)) {
    2.37 -                        membersClosure.addSubScope(visit(i, skipInterface));
    2.38 +            if (seenTypes.contains(t.tsym)) {
    2.39 +                //this is possible when an interface is implemented in multiple
    2.40 +                //superclasses, or when a classs hierarchy is circular - in such
    2.41 +                //cases we don't need to recurse (empty scope is returned)
    2.42 +                return new CompoundScope(t.tsym);
    2.43 +            }
    2.44 +            try {
    2.45 +                seenTypes = seenTypes.prepend(t.tsym);
    2.46 +                ClassSymbol csym = (ClassSymbol)t.tsym;
    2.47 +                Entry e = _map.get(csym);
    2.48 +                if (e == null || !e.matches(skipInterface)) {
    2.49 +                    CompoundScope membersClosure = new CompoundScope(csym);
    2.50 +                    if (!skipInterface) {
    2.51 +                        for (Type i : interfaces(t)) {
    2.52 +                            membersClosure.addSubScope(visit(i, skipInterface));
    2.53 +                        }
    2.54                      }
    2.55 +                    membersClosure.addSubScope(visit(supertype(t), skipInterface));
    2.56 +                    membersClosure.addSubScope(csym.members());
    2.57 +                    e = new Entry(skipInterface, membersClosure);
    2.58 +                    _map.put(csym, e);
    2.59                  }
    2.60 -                membersClosure.addSubScope(visit(supertype(t), skipInterface));
    2.61 -                membersClosure.addSubScope(csym.members());
    2.62 -                e = new Entry(skipInterface, membersClosure);
    2.63 -                _map.put(csym, e);
    2.64 +                return e.compoundScope;
    2.65              }
    2.66 -            return e.compoundScope;
    2.67 +            finally {
    2.68 +                seenTypes = seenTypes.tail;
    2.69 +            }
    2.70          }
    2.71  
    2.72          @Override
     3.1 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java	Thu Sep 15 18:53:41 2011 -0700
     3.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java	Mon Sep 19 19:41:46 2011 -0700
     3.3 @@ -594,7 +594,15 @@
     3.4              lintEnv = lintEnv.next;
     3.5  
     3.6          // Having found the enclosing lint value, we can initialize the lint value for this class
     3.7 -        env.info.lint = lintEnv.info.lint.augment(env.info.enclVar.attributes_field, env.info.enclVar.flags());
     3.8 +        // ... but ...
     3.9 +        // There's a problem with evaluating annotations in the right order, such that
    3.10 +        // env.info.enclVar.attributes_field might not yet have been evaluated, and so might be
    3.11 +        // null. In that case, calling augment will throw an NPE. To avoid this, for now we
    3.12 +        // revert to the jdk 6 behavior and ignore the (unevaluated) attributes.
    3.13 +        if (env.info.enclVar.attributes_field == null)
    3.14 +            env.info.lint = lintEnv.info.lint;
    3.15 +        else
    3.16 +            env.info.lint = lintEnv.info.lint.augment(env.info.enclVar.attributes_field, env.info.enclVar.flags());
    3.17  
    3.18          Lint prevLint = chk.setLint(env.info.lint);
    3.19          JavaFileObject prevSource = log.useSource(env.toplevel.sourcefile);
     4.1 --- a/src/share/classes/com/sun/tools/javac/file/RegularFileObject.java	Thu Sep 15 18:53:41 2011 -0700
     4.2 +++ b/src/share/classes/com/sun/tools/javac/file/RegularFileObject.java	Mon Sep 19 19:41:46 2011 -0700
     4.3 @@ -1,5 +1,5 @@
     4.4  /*
     4.5 - * Copyright (c) 2005, 2009, Oracle and/or its affiliates. All rights reserved.
     4.6 + * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
     4.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4.8   *
     4.9   * This code is free software; you can redistribute it and/or modify it
    4.10 @@ -98,6 +98,7 @@
    4.11  
    4.12      @Override
    4.13      public OutputStream openOutputStream() throws IOException {
    4.14 +        fileManager.flushCache(this);
    4.15          ensureParentDirectoriesExist();
    4.16          return new FileOutputStream(file);
    4.17      }
    4.18 @@ -128,6 +129,7 @@
    4.19  
    4.20      @Override
    4.21      public Writer openWriter() throws IOException {
    4.22 +        fileManager.flushCache(this);
    4.23          ensureParentDirectoriesExist();
    4.24          return new OutputStreamWriter(new FileOutputStream(file), fileManager.getEncodingName());
    4.25      }
     5.1 --- a/src/share/classes/com/sun/tools/javac/jvm/Gen.java	Thu Sep 15 18:53:41 2011 -0700
     5.2 +++ b/src/share/classes/com/sun/tools/javac/jvm/Gen.java	Mon Sep 19 19:41:46 2011 -0700
     5.3 @@ -1689,6 +1689,8 @@
     5.4          // outer instance of a super(...) call appears as first parameter).
     5.5          genArgs(tree.args,
     5.6                  TreeInfo.symbol(tree.meth).externalType(types).getParameterTypes());
     5.7 +        code.statBegin(tree.pos);
     5.8 +        code.markStatBegin();
     5.9          result = m.invoke();
    5.10      }
    5.11  
     6.1 --- a/src/share/classes/com/sun/tools/javac/nio/PathFileObject.java	Thu Sep 15 18:53:41 2011 -0700
     6.2 +++ b/src/share/classes/com/sun/tools/javac/nio/PathFileObject.java	Mon Sep 19 19:41:46 2011 -0700
     6.3 @@ -205,6 +205,7 @@
     6.4  
     6.5      @Override
     6.6      public OutputStream openOutputStream() throws IOException {
     6.7 +        fileManager.flushCache(this);
     6.8          ensureParentDirectoriesExist();
     6.9          return Files.newOutputStream(path);
    6.10      }
    6.11 @@ -241,6 +242,7 @@
    6.12  
    6.13      @Override
    6.14      public Writer openWriter() throws IOException {
    6.15 +        fileManager.flushCache(this);
    6.16          ensureParentDirectoriesExist();
    6.17          return new OutputStreamWriter(Files.newOutputStream(path), fileManager.getEncodingName());
    6.18      }
     7.1 --- a/src/share/classes/com/sun/tools/javac/parser/JavacParser.java	Thu Sep 15 18:53:41 2011 -0700
     7.2 +++ b/src/share/classes/com/sun/tools/javac/parser/JavacParser.java	Mon Sep 19 19:41:46 2011 -0700
     7.3 @@ -27,15 +27,15 @@
     7.4  
     7.5  import java.util.*;
     7.6  
     7.7 +import com.sun.tools.javac.code.*;
     7.8  import com.sun.tools.javac.tree.*;
     7.9 -import com.sun.tools.javac.code.*;
    7.10 +import com.sun.tools.javac.tree.JCTree.*;
    7.11  import com.sun.tools.javac.util.*;
    7.12  import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
    7.13 +import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
    7.14  import com.sun.tools.javac.util.List;
    7.15 +
    7.16  import static com.sun.tools.javac.util.ListBuffer.lb;
    7.17 -
    7.18 -import com.sun.tools.javac.tree.JCTree.*;
    7.19 -
    7.20  import static com.sun.tools.javac.parser.Token.*;
    7.21  
    7.22  /** The parser maps a token sequence into an abstract syntax
    7.23 @@ -254,26 +254,44 @@
    7.24      }
    7.25  
    7.26      private JCErroneous syntaxError(int pos, String key, Token... args) {
    7.27 -        return syntaxError(pos, null, key, args);
    7.28 +        return syntaxError(pos, List.<JCTree>nil(), key, args);
    7.29      }
    7.30  
    7.31      private JCErroneous syntaxError(int pos, List<JCTree> errs, String key, Token... args) {
    7.32          setErrorEndPos(pos);
    7.33 -        reportSyntaxError(pos, key, (Object[])args);
    7.34 -        return toP(F.at(pos).Erroneous(errs));
    7.35 +        JCErroneous err = F.at(pos).Erroneous(errs);
    7.36 +        reportSyntaxError(err, key, (Object[])args);
    7.37 +        if (errs != null) {
    7.38 +            JCTree last = errs.last();
    7.39 +            if (last != null)
    7.40 +                storeEnd(last, pos);
    7.41 +        }
    7.42 +        return toP(err);
    7.43      }
    7.44  
    7.45      private int errorPos = Position.NOPOS;
    7.46 +
    7.47      /**
    7.48 -     * Report a syntax error at given position using the given
    7.49 -     * argument unless one was already reported at the same position.
    7.50 +     * Report a syntax using the given the position parameter and arguments,
    7.51 +     * unless one was already reported at the same position.
    7.52       */
    7.53      private void reportSyntaxError(int pos, String key, Object... args) {
    7.54 +        JCDiagnostic.DiagnosticPosition diag = new JCDiagnostic.SimpleDiagnosticPosition(pos);
    7.55 +        reportSyntaxError(diag, key, args);
    7.56 +    }
    7.57 +
    7.58 +    /**
    7.59 +     * Report a syntax error using the given DiagnosticPosition object and
    7.60 +     * arguments, unless one was already reported at the same position.
    7.61 +     */
    7.62 +    private void reportSyntaxError(JCDiagnostic.DiagnosticPosition diagPos, String key, Object... args) {
    7.63 +        int pos = diagPos.getPreferredPosition();
    7.64          if (pos > S.errPos() || pos == Position.NOPOS) {
    7.65 -            if (S.token() == EOF)
    7.66 -                error(pos, "premature.eof");
    7.67 -            else
    7.68 -                error(pos, key, args);
    7.69 +            if (S.token() == EOF) {
    7.70 +                error(diagPos, "premature.eof");
    7.71 +            } else {
    7.72 +                error(diagPos, key, args);
    7.73 +            }
    7.74          }
    7.75          S.errPos(pos);
    7.76          if (S.pos() == errorPos)
    7.77 @@ -311,7 +329,7 @@
    7.78      /** Report an illegal start of expression/type error at given position.
    7.79       */
    7.80      JCExpression illegal(int pos) {
    7.81 -        setErrorEndPos(S.pos());
    7.82 +        setErrorEndPos(pos);
    7.83          if ((mode & EXPR) != 0)
    7.84              return syntaxError(pos, "illegal.start.of.expr");
    7.85          else
    7.86 @@ -340,7 +358,7 @@
    7.87       *  indexed by the tree nodes they refer to.
    7.88       *  defined only if option flag keepDocComment is set.
    7.89       */
    7.90 -    Map<JCTree, String> docComments;
    7.91 +    private final Map<JCTree, String> docComments;
    7.92  
    7.93      /** Make an entry into docComments hashtable,
    7.94       *  provided flag keepDocComments is set and given doc comment is non-null.
    7.95 @@ -462,6 +480,10 @@
    7.96          return t;
    7.97      }
    7.98  
    7.99 +    JCExpression literal(Name prefix) {
   7.100 +        return literal(prefix, S.pos());
   7.101 +    }
   7.102 +
   7.103      /**
   7.104       * Literal =
   7.105       *     INTLITERAL
   7.106 @@ -474,8 +496,7 @@
   7.107       *   | FALSE
   7.108       *   | NULL
   7.109       */
   7.110 -    JCExpression literal(Name prefix) {
   7.111 -        int pos = S.pos();
   7.112 +    JCExpression literal(Name prefix, int pos) {
   7.113          JCExpression t = errorTree;
   7.114          switch (S.token()) {
   7.115          case INTLITERAL:
   7.116 @@ -869,7 +890,7 @@
   7.117                      (S.token() == INTLITERAL || S.token() == LONGLITERAL) &&
   7.118                      S.radix() == 10) {
   7.119                      mode = EXPR;
   7.120 -                    t = literal(names.hyphen);
   7.121 +                    t = literal(names.hyphen, pos);
   7.122                  } else {
   7.123                      t = term3();
   7.124                      return F.at(pos).Unary(unoptag(token), t);
   7.125 @@ -1267,15 +1288,17 @@
   7.126                  case GTGT:
   7.127                      S.token(GT);
   7.128                      break;
   7.129 +                case GT:
   7.130 +                    S.nextToken();
   7.131 +                    break;
   7.132                  default:
   7.133 -                    accept(GT);
   7.134 +                    args.append(syntaxError(S.pos(), "expected", GT));
   7.135                      break;
   7.136                  }
   7.137                  return args.toList();
   7.138              }
   7.139          } else {
   7.140 -            syntaxError(S.pos(), "expected", LT);
   7.141 -            return List.nil();
   7.142 +            return List.<JCExpression>of(syntaxError(S.pos(), "expected", LT));
   7.143          }
   7.144      }
   7.145  
   7.146 @@ -1300,12 +1323,12 @@
   7.147              return F.at(pos).Wildcard(t, bound);
   7.148          } else if (S.token() == IDENTIFIER) {
   7.149              //error recovery
   7.150 -            reportSyntaxError(S.prevEndPos(), "expected3",
   7.151 -                    GT, EXTENDS, SUPER);
   7.152              TypeBoundKind t = F.at(Position.NOPOS).TypeBoundKind(BoundKind.UNBOUND);
   7.153              JCExpression wc = toP(F.at(pos).Wildcard(t, null));
   7.154              JCIdent id = toP(F.at(S.pos()).Ident(ident()));
   7.155 -            return F.at(pos).Erroneous(List.<JCTree>of(wc, id));
   7.156 +            JCErroneous err = F.at(pos).Erroneous(List.<JCTree>of(wc, id));
   7.157 +            reportSyntaxError(err, "expected3", GT, EXTENDS, SUPER);
   7.158 +            return err;
   7.159          } else {
   7.160              TypeBoundKind t = toP(F.at(pos).TypeBoundKind(BoundKind.UNBOUND));
   7.161              return toP(F.at(pos).Wildcard(t, null));
   7.162 @@ -1391,7 +1414,7 @@
   7.163          while (S.token() == DOT) {
   7.164              if (diamondFound) {
   7.165                  //cannot select after a diamond
   7.166 -                illegal(S.pos());
   7.167 +                illegal();
   7.168              }
   7.169              int pos = S.pos();
   7.170              S.nextToken();
   7.171 @@ -1419,15 +1442,16 @@
   7.172                      pos = typeArgs.head.pos;
   7.173                  }
   7.174                  setErrorEndPos(S.prevEndPos());
   7.175 -                reportSyntaxError(pos, "cannot.create.array.with.type.arguments");
   7.176 -                return toP(F.at(newpos).Erroneous(typeArgs.prepend(e)));
   7.177 +                JCErroneous err = F.at(pos).Erroneous(typeArgs.prepend(e));
   7.178 +                reportSyntaxError(err, "cannot.create.array.with.type.arguments");
   7.179 +                return toP(err);
   7.180              }
   7.181              return e;
   7.182          } else if (S.token() == LPAREN) {
   7.183              return classCreatorRest(newpos, null, typeArgs, t);
   7.184          } else {
   7.185 -            reportSyntaxError(S.pos(), "expected2",
   7.186 -                               LPAREN, LBRACKET);
   7.187 +            setErrorEndPos(S.pos());
   7.188 +            reportSyntaxError(S.pos(), "expected2", LPAREN, LBRACKET);
   7.189              t = toP(F.at(newpos).NewClass(null, typeArgs, t, List.<JCExpression>nil(), null));
   7.190              return toP(F.at(newpos).Erroneous(List.<JCTree>of(t)));
   7.191          }
   7.192 @@ -1457,7 +1481,8 @@
   7.193              if (S.token() == LBRACE) {
   7.194                  return arrayInitializer(newpos, elemtype);
   7.195              } else {
   7.196 -                return syntaxError(S.pos(), "array.dimension.missing");
   7.197 +                JCExpression t = toP(F.at(newpos).NewArray(elemtype, List.<JCExpression>nil(), null));
   7.198 +                return syntaxError(S.pos(), List.<JCTree>of(t), "array.dimension.missing");
   7.199              }
   7.200          } else {
   7.201              ListBuffer<JCExpression> dims = new ListBuffer<JCExpression>();
   7.202 @@ -1843,7 +1868,7 @@
   7.203  
   7.204      /** CatchClause     = CATCH "(" FormalParameter ")" Block
   7.205       */
   7.206 -    JCCatch catchClause() {
   7.207 +    protected JCCatch catchClause() {
   7.208          int pos = S.pos();
   7.209          accept(CATCH);
   7.210          accept(LPAREN);
   7.211 @@ -1973,7 +1998,7 @@
   7.212      JCModifiers modifiersOpt() {
   7.213          return modifiersOpt(null);
   7.214      }
   7.215 -    JCModifiers modifiersOpt(JCModifiers partial) {
   7.216 +    protected JCModifiers modifiersOpt(JCModifiers partial) {
   7.217          long flags;
   7.218          ListBuffer<JCAnnotation> annotations = new ListBuffer<JCAnnotation>();
   7.219          int pos;
   7.220 @@ -2006,6 +2031,7 @@
   7.221              case SYNCHRONIZED: flag = Flags.SYNCHRONIZED; break;
   7.222              case STRICTFP    : flag = Flags.STRICTFP; break;
   7.223              case MONKEYS_AT  : flag = Flags.ANNOTATION; break;
   7.224 +            case ERROR       : flag = 0; S.nextToken(); break;
   7.225              default: break loop;
   7.226              }
   7.227              if ((flags & flag) != 0) error(S.pos(), "repeated.modifier");
   7.228 @@ -2219,9 +2245,12 @@
   7.229  
   7.230      /** Resource = VariableModifiersOpt Type VariableDeclaratorId = Expression
   7.231       */
   7.232 -    JCTree resource() {
   7.233 -        return variableDeclaratorRest(S.pos(), optFinal(Flags.FINAL),
   7.234 -                                      parseType(), ident(), true, null);
   7.235 +    protected JCTree resource() {
   7.236 +        JCModifiers optFinal = optFinal(Flags.FINAL);
   7.237 +        JCExpression type = parseType();
   7.238 +        int pos = S.pos();
   7.239 +        Name ident = ident();
   7.240 +        return variableDeclaratorRest(pos, optFinal, type, ident, true, null);
   7.241      }
   7.242  
   7.243      /** CompilationUnit = [ { "@" Annotation } PACKAGE Qualident ";"] {ImportDeclaration} {TypeDeclaration}
   7.244 @@ -2568,7 +2597,7 @@
   7.245       *    | ModifiersOpt Type Ident
   7.246       *      ( ConstantDeclaratorsRest | InterfaceMethodDeclaratorRest ";" )
   7.247       */
   7.248 -    List<JCTree> classOrInterfaceBodyDeclaration(Name className, boolean isInterface) {
   7.249 +    protected List<JCTree> classOrInterfaceBodyDeclaration(Name className, boolean isInterface) {
   7.250          if (S.token() == SEMI) {
   7.251              S.nextToken();
   7.252              return List.<JCTree>nil();
   7.253 @@ -2770,7 +2799,7 @@
   7.254      /** FormalParameter = { FINAL | '@' Annotation } Type VariableDeclaratorId
   7.255       *  LastFormalParameter = { FINAL | '@' Annotation } Type '...' Ident | FormalParameter
   7.256       */
   7.257 -    JCVariableDecl formalParameter() {
   7.258 +    protected JCVariableDecl formalParameter() {
   7.259          JCModifiers mods = optFinal(Flags.PARAMETER);
   7.260          JCExpression type = parseType();
   7.261          if (S.token() == ELLIPSIS) {
   7.262 @@ -2788,6 +2817,10 @@
   7.263          log.error(DiagnosticFlag.SYNTAX, pos, key, args);
   7.264      }
   7.265  
   7.266 +    void error(DiagnosticPosition pos, String key, Object ... args) {
   7.267 +        log.error(DiagnosticFlag.SYNTAX, pos, key, args);
   7.268 +    }
   7.269 +
   7.270      void warning(int pos, String key, Object ... args) {
   7.271          log.warning(pos, key, args);
   7.272      }
   7.273 @@ -2807,8 +2840,9 @@
   7.274          case JCTree.ERRONEOUS:
   7.275              return t;
   7.276          default:
   7.277 -            error(t.pos, "not.stmt");
   7.278 -            return F.at(t.pos).Erroneous(List.<JCTree>of(t));
   7.279 +            JCExpression ret = F.at(t.pos).Erroneous(List.<JCTree>of(t));
   7.280 +            error(ret, "not.stmt");
   7.281 +            return ret;
   7.282          }
   7.283      }
   7.284  
     8.1 --- a/src/share/classes/com/sun/tools/javac/parser/Scanner.java	Thu Sep 15 18:53:41 2011 -0700
     8.2 +++ b/src/share/classes/com/sun/tools/javac/parser/Scanner.java	Mon Sep 19 19:41:46 2011 -0700
     8.3 @@ -982,8 +982,16 @@
     8.4      }
     8.5  
     8.6      /** Sets the current token.
     8.7 +     * This method is primarily used to update the token stream when the
     8.8 +     * parser is handling the end of nested type arguments such as
     8.9 +     * {@code List<List<String>>} and needs to disambiguate between
    8.10 +     * repeated use of ">" and relation operators such as ">>" and ">>>". Noting
    8.11 +     * that this does not handle arbitrary tokens containing Unicode escape
    8.12 +     * sequences.
    8.13       */
    8.14      public void token(Token token) {
    8.15 +        pos += this.token.name.length() - token.name.length();
    8.16 +        prevEndPos = pos;
    8.17          this.token = token;
    8.18      }
    8.19  
     9.1 --- a/src/share/classes/com/sun/tools/javac/util/AbstractLog.java	Thu Sep 15 18:53:41 2011 -0700
     9.2 +++ b/src/share/classes/com/sun/tools/javac/util/AbstractLog.java	Mon Sep 19 19:41:46 2011 -0700
     9.3 @@ -1,5 +1,5 @@
     9.4  /*
     9.5 - * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
     9.6 + * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
     9.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     9.8   *
     9.9   * This code is free software; you can redistribute it and/or modify it
    9.10 @@ -96,6 +96,19 @@
    9.11  
    9.12      /** Report an error, unless another error was already reported at same
    9.13       *  source position.
    9.14 +     *  @param flag   A flag to set on the diagnostic
    9.15 +     *  @param pos    The source position at which to report the error.
    9.16 +     *  @param key    The key for the localized error message.
    9.17 +     *  @param args   Fields of the error message.
    9.18 +     */
    9.19 +    public void error(DiagnosticFlag flag, DiagnosticPosition pos, String key, Object ... args) {
    9.20 +        JCDiagnostic d = diags.error(source, pos, key, args);
    9.21 +        d.setFlag(flag);
    9.22 +        report(d);
    9.23 +    }
    9.24 +
    9.25 +    /** Report an error, unless another error was already reported at same
    9.26 +     *  source position.
    9.27       *  @param pos    The source position at which to report the error.
    9.28       *  @param key    The key for the localized error message.
    9.29       *  @param args   Fields of the error message.
    10.1 --- a/src/share/classes/com/sun/tools/javac/util/BaseFileManager.java	Thu Sep 15 18:53:41 2011 -0700
    10.2 +++ b/src/share/classes/com/sun/tools/javac/util/BaseFileManager.java	Mon Sep 19 19:41:46 2011 -0700
    10.3 @@ -1,5 +1,5 @@
    10.4  /*
    10.5 - * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
    10.6 + * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
    10.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    10.8   *
    10.9   * This code is free software; you can redistribute it and/or modify it
   10.10 @@ -322,16 +322,46 @@
   10.11  
   10.12      // <editor-fold defaultstate="collapsed" desc="Content cache">
   10.13      public CharBuffer getCachedContent(JavaFileObject file) {
   10.14 -        SoftReference<CharBuffer> r = contentCache.get(file);
   10.15 -        return (r == null ? null : r.get());
   10.16 +        ContentCacheEntry e = contentCache.get(file);
   10.17 +        if (e == null)
   10.18 +            return null;
   10.19 +
   10.20 +        if (!e.isValid(file)) {
   10.21 +            contentCache.remove(file);
   10.22 +            return null;
   10.23 +        }
   10.24 +
   10.25 +        return e.getValue();
   10.26      }
   10.27  
   10.28      public void cache(JavaFileObject file, CharBuffer cb) {
   10.29 -        contentCache.put(file, new SoftReference<CharBuffer>(cb));
   10.30 +        contentCache.put(file, new ContentCacheEntry(file, cb));
   10.31      }
   10.32  
   10.33 -    protected final Map<JavaFileObject, SoftReference<CharBuffer>> contentCache
   10.34 -            = new HashMap<JavaFileObject, SoftReference<CharBuffer>>();
   10.35 +    public void flushCache(JavaFileObject file) {
   10.36 +        contentCache.remove(file);
   10.37 +    }
   10.38 +
   10.39 +    protected final Map<JavaFileObject, ContentCacheEntry> contentCache
   10.40 +            = new HashMap<JavaFileObject, ContentCacheEntry>();
   10.41 +
   10.42 +    protected static class ContentCacheEntry {
   10.43 +        final long timestamp;
   10.44 +        final SoftReference<CharBuffer> ref;
   10.45 +
   10.46 +        ContentCacheEntry(JavaFileObject file, CharBuffer cb) {
   10.47 +            this.timestamp = file.getLastModified();
   10.48 +            this.ref = new SoftReference<CharBuffer>(cb);
   10.49 +        }
   10.50 +
   10.51 +        boolean isValid(JavaFileObject file) {
   10.52 +            return timestamp == file.getLastModified();
   10.53 +        }
   10.54 +
   10.55 +        CharBuffer getValue() {
   10.56 +            return ref.get();
   10.57 +        }
   10.58 +    }
   10.59      // </editor-fold>
   10.60  
   10.61      public static Kind getKind(String name) {
    11.1 --- a/src/share/classes/javax/tools/JavaCompiler.java	Thu Sep 15 18:53:41 2011 -0700
    11.2 +++ b/src/share/classes/javax/tools/JavaCompiler.java	Mon Sep 19 19:41:46 2011 -0700
    11.3 @@ -26,10 +26,8 @@
    11.4  package javax.tools;
    11.5  
    11.6  import java.io.File;
    11.7 -import java.io.InputStream;
    11.8  import java.io.Writer;
    11.9  import java.nio.charset.Charset;
   11.10 -import java.util.List;
   11.11  import java.util.Locale;
   11.12  import java.util.concurrent.Callable;
   11.13  import javax.annotation.processing.Processor;
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/test/tools/javac/7079713/TestCircularClassfile.java	Mon Sep 19 19:41:46 2011 -0700
    12.3 @@ -0,0 +1,168 @@
    12.4 +/*
    12.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
    12.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    12.7 + *
    12.8 + * This code is free software; you can redistribute it and/or modify it
    12.9 + * under the terms of the GNU General Public License version 2 only, as
   12.10 + * published by the Free Software Foundation.
   12.11 + *
   12.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
   12.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   12.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   12.15 + * version 2 for more details (a copy is included in the LICENSE file that
   12.16 + * accompanied this code).
   12.17 + *
   12.18 + * You should have received a copy of the GNU General Public License version
   12.19 + * 2 along with this work; if not, write to the Free Software Foundation,
   12.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   12.21 + *
   12.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   12.23 + * or visit www.oracle.com if you need additional information or have any
   12.24 + * questions.
   12.25 + */
   12.26 +
   12.27 +/*
   12.28 + * @test
   12.29 + * @bug 7079713
   12.30 + * @summary javac hangs when compiling a class that references a cyclically inherited class
   12.31 + * @run main TestCircularClassfile
   12.32 + */
   12.33 +
   12.34 +import java.io.*;
   12.35 +import java.net.URI;
   12.36 +import java.util.Arrays;
   12.37 +import javax.tools.JavaCompiler;
   12.38 +import javax.tools.JavaFileObject;
   12.39 +import javax.tools.SimpleJavaFileObject;
   12.40 +import javax.tools.StandardJavaFileManager;
   12.41 +import javax.tools.StandardLocation;
   12.42 +import javax.tools.ToolProvider;
   12.43 +
   12.44 +import com.sun.source.util.JavacTask;
   12.45 +
   12.46 +public class TestCircularClassfile {
   12.47 +
   12.48 +    enum SourceKind {
   12.49 +        A_EXTENDS_B("class B {} class A extends B { void m() {} }"),
   12.50 +        B_EXTENDS_A("class A { void m() {} } class B extends A {}");
   12.51 +
   12.52 +        String sourceStr;
   12.53 +
   12.54 +        private SourceKind(String sourceStr) {
   12.55 +            this.sourceStr = sourceStr;
   12.56 +        }
   12.57 +
   12.58 +        SimpleJavaFileObject getSource() {
   12.59 +            return new SimpleJavaFileObject(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE) {
   12.60 +                @Override
   12.61 +                public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
   12.62 +                    return sourceStr;
   12.63 +                }
   12.64 +            };
   12.65 +        }
   12.66 +    }
   12.67 +
   12.68 +    enum TestKind {
   12.69 +        REPLACE_A("A.class"),
   12.70 +        REPLACE_B("B.class");
   12.71 +
   12.72 +        String targetClass;
   12.73 +
   12.74 +        private TestKind(String targetClass) {
   12.75 +            this.targetClass = targetClass;
   12.76 +        }
   12.77 +    }
   12.78 +
   12.79 +    enum ClientKind {
   12.80 +        METHOD_CALL1("A a = null; a.m();"),
   12.81 +        METHOD_CALL2("B b = null; b.m();"),
   12.82 +        CONSTR_CALL1("new A();"),
   12.83 +        CONSTR_CALL2("new B();"),
   12.84 +        ASSIGN1("A a = null; B b = a;"),
   12.85 +        ASSIGN2("B b = null; A a = b;");
   12.86 +
   12.87 +        String mainMethod;
   12.88 +
   12.89 +        private ClientKind(String mainMethod) {
   12.90 +            this.mainMethod = mainMethod;
   12.91 +        }
   12.92 +
   12.93 +        SimpleJavaFileObject getSource() {
   12.94 +            return new SimpleJavaFileObject(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE) {
   12.95 +                @Override
   12.96 +                public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
   12.97 +                    return "class Test { public static void main(String[] args) { #M } }"
   12.98 +                            .replace("#M", mainMethod);
   12.99 +                }
  12.100 +            };
  12.101 +        }
  12.102 +    }
  12.103 +
  12.104 +    public static void main(String... args) throws Exception {
  12.105 +        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
  12.106 +        StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
  12.107 +        int count = 0;
  12.108 +        for (SourceKind sk1 : SourceKind.values()) {
  12.109 +            for (SourceKind sk2 : SourceKind.values()) {
  12.110 +                for (TestKind tk : TestKind.values()) {
  12.111 +                    for (ClientKind ck : ClientKind.values()) {
  12.112 +                        new TestCircularClassfile("sub_"+count++, sk1, sk2, tk, ck).check(comp, fm);
  12.113 +                    }
  12.114 +                }
  12.115 +            }
  12.116 +        }
  12.117 +    }
  12.118 +
  12.119 +    static String workDir = System.getProperty("user.dir");
  12.120 +
  12.121 +    String destPath;
  12.122 +    SourceKind sk1;
  12.123 +    SourceKind sk2;
  12.124 +    TestKind tk;
  12.125 +    ClientKind ck;
  12.126 +
  12.127 +    TestCircularClassfile(String destPath, SourceKind sk1, SourceKind sk2, TestKind tk, ClientKind ck) {
  12.128 +        this.destPath = destPath;
  12.129 +        this.sk1 = sk1;
  12.130 +        this.sk2 = sk2;
  12.131 +        this.tk = tk;
  12.132 +        this.ck = ck;
  12.133 +    }
  12.134 +
  12.135 +    void check(JavaCompiler comp, StandardJavaFileManager fm) throws Exception {
  12.136 +        //step 1: compile first source code in the test subfolder
  12.137 +        File destDir = new File(workDir, destPath); destDir.mkdir();
  12.138 +        //output dir must be set explicitly as we are sharing the fm (see bug 7026941)
  12.139 +        fm.setLocation(javax.tools.StandardLocation.CLASS_OUTPUT, Arrays.asList(destDir));
  12.140 +        JavacTask ct = (JavacTask)comp.getTask(null, fm, null,
  12.141 +                null, null, Arrays.asList(sk1.getSource()));
  12.142 +        ct.generate();
  12.143 +
  12.144 +        //step 2: compile second source code in a temp folder
  12.145 +        File tmpDir = new File(destDir, "tmp"); tmpDir.mkdir();
  12.146 +        //output dir must be set explicitly as we are sharing the fm (see bug 7026941)
  12.147 +        fm.setLocation(javax.tools.StandardLocation.CLASS_OUTPUT, Arrays.asList(tmpDir));
  12.148 +        ct = (JavacTask)comp.getTask(null, fm, null,
  12.149 +                null, null, Arrays.asList(sk2.getSource()));
  12.150 +        ct.generate();
  12.151 +
  12.152 +        //step 3: move a classfile from the temp folder to the test subfolder
  12.153 +        File fileToMove = new File(tmpDir, tk.targetClass);
  12.154 +        File target = new File(destDir, tk.targetClass);
  12.155 +        target.delete();
  12.156 +        boolean success = fileToMove.renameTo(target);
  12.157 +
  12.158 +        if (!success) {
  12.159 +            throw new AssertionError("error when moving file " + tk.targetClass);
  12.160 +        }
  12.161 +
  12.162 +        //step 4: compile the client class against the classes in the test subfolder
  12.163 +        //input/output dir must be set explicitly as we are sharing the fm (see bug 7026941)
  12.164 +        fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(destDir));
  12.165 +        fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(destDir));
  12.166 +        ct = (JavacTask)comp.getTask(null, fm, null,
  12.167 +                null, null, Arrays.asList(ck.getSource()));
  12.168 +
  12.169 +        ct.generate();
  12.170 +    }
  12.171 +}
    13.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.2 +++ b/test/tools/javac/7085024/T7085024.java	Mon Sep 19 19:41:46 2011 -0700
    13.3 @@ -0,0 +1,12 @@
    13.4 +/*
    13.5 + * @test /nodynamiccopyright/
    13.6 + * @bug 7085024
    13.7 + * @summary internal error; cannot instantiate Foo
    13.8 + * @compile/fail/ref=T7085024.out -XDrawDiagnostics T7085024.java
    13.9 + */
   13.10 +
   13.11 +class T7085024 {
   13.12 +    T7085024 (boolean ret) { } //internal error goes away if constructor accepts a reference type
   13.13 +
   13.14 +    T7085024 f = new T7085024((NonExistentClass) null );
   13.15 +}
    14.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.2 +++ b/test/tools/javac/7085024/T7085024.out	Mon Sep 19 19:41:46 2011 -0700
    14.3 @@ -0,0 +1,2 @@
    14.4 +T7085024.java:11:32: compiler.err.cant.resolve.location: kindname.class, NonExistentClass, , , (compiler.misc.location: kindname.class, T7085024, null)
    14.5 +1 error
    15.1 --- a/test/tools/javac/TryWithResources/BadTwr.out	Thu Sep 15 18:53:41 2011 -0700
    15.2 +++ b/test/tools/javac/TryWithResources/BadTwr.out	Mon Sep 19 19:41:46 2011 -0700
    15.3 @@ -1,5 +1,5 @@
    15.4 -BadTwr.java:13:39: compiler.err.already.defined: r1, main(java.lang.String...)
    15.5 -BadTwr.java:18:13: compiler.err.already.defined: args, main(java.lang.String...)
    15.6 +BadTwr.java:13:46: compiler.err.already.defined: r1, main(java.lang.String...)
    15.7 +BadTwr.java:18:20: compiler.err.already.defined: args, main(java.lang.String...)
    15.8  BadTwr.java:21:13: compiler.err.cant.assign.val.to.final.var: thatsIt
    15.9 -BadTwr.java:26:17: compiler.err.already.defined: name, main(java.lang.String...)
   15.10 +BadTwr.java:26:24: compiler.err.already.defined: name, main(java.lang.String...)
   15.11  4 errors
    16.1 --- a/test/tools/javac/TryWithResources/DuplicateResourceDecl.out	Thu Sep 15 18:53:41 2011 -0700
    16.2 +++ b/test/tools/javac/TryWithResources/DuplicateResourceDecl.out	Mon Sep 19 19:41:46 2011 -0700
    16.3 @@ -1,2 +1,2 @@
    16.4 -DuplicateResourceDecl.java:12:45: compiler.err.already.defined: c, main(java.lang.String[])
    16.5 +DuplicateResourceDecl.java:12:56: compiler.err.already.defined: c, main(java.lang.String[])
    16.6  1 error
    17.1 --- a/test/tools/javac/TryWithResources/ResourceInterface.out	Thu Sep 15 18:53:41 2011 -0700
    17.2 +++ b/test/tools/javac/TryWithResources/ResourceInterface.out	Mon Sep 19 19:41:46 2011 -0700
    17.3 @@ -1,2 +1,2 @@
    17.4 -ResourceInterface.java:38:13: compiler.err.unreported.exception.implicit.close: ResourceInterface.E1, r2
    17.5 +ResourceInterface.java:38:23: compiler.err.unreported.exception.implicit.close: ResourceInterface.E1, r2
    17.6  1 error
    18.1 --- a/test/tools/javac/TryWithResources/TwrFlow.out	Thu Sep 15 18:53:41 2011 -0700
    18.2 +++ b/test/tools/javac/TryWithResources/TwrFlow.out	Mon Sep 19 19:41:46 2011 -0700
    18.3 @@ -1,3 +1,3 @@
    18.4  TwrFlow.java:14:11: compiler.err.except.never.thrown.in.try: java.io.IOException
    18.5 -TwrFlow.java:12:13: compiler.err.unreported.exception.implicit.close: CustomCloseException, twrFlow
    18.6 +TwrFlow.java:12:21: compiler.err.unreported.exception.implicit.close: CustomCloseException, twrFlow
    18.7  2 errors
    19.1 --- a/test/tools/javac/TryWithResources/TwrLint.out	Thu Sep 15 18:53:41 2011 -0700
    19.2 +++ b/test/tools/javac/TryWithResources/TwrLint.out	Mon Sep 19 19:41:46 2011 -0700
    19.3 @@ -1,3 +1,3 @@
    19.4  TwrLint.java:14:15: compiler.warn.try.explicit.close.call
    19.5 -TwrLint.java:13:13: compiler.warn.try.resource.not.referenced: r3
    19.6 +TwrLint.java:13:21: compiler.warn.try.resource.not.referenced: r3
    19.7  2 warnings
    20.1 --- a/test/tools/javac/TryWithResources/TwrOnNonResource.out	Thu Sep 15 18:53:41 2011 -0700
    20.2 +++ b/test/tools/javac/TryWithResources/TwrOnNonResource.out	Mon Sep 19 19:41:46 2011 -0700
    20.3 @@ -1,4 +1,4 @@
    20.4 -TwrOnNonResource.java:12:13: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable
    20.5 -TwrOnNonResource.java:15:13: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable
    20.6 -TwrOnNonResource.java:18:13: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable
    20.7 +TwrOnNonResource.java:12:30: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable
    20.8 +TwrOnNonResource.java:15:30: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable
    20.9 +TwrOnNonResource.java:18:30: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable
   20.10  3 errors
    21.1 --- a/test/tools/javac/TryWithResources/UnusedResourcesTest.java	Thu Sep 15 18:53:41 2011 -0700
    21.2 +++ b/test/tools/javac/TryWithResources/UnusedResourcesTest.java	Mon Sep 19 19:41:46 2011 -0700
    21.3 @@ -28,6 +28,7 @@
    21.4   */
    21.5  
    21.6  import com.sun.source.util.JavacTask;
    21.7 +import com.sun.tools.javac.api.ClientCodeWrapper;
    21.8  import com.sun.tools.javac.api.JavacTool;
    21.9  import com.sun.tools.javac.util.JCDiagnostic;
   21.10  import java.net.URI;
   21.11 @@ -236,7 +237,7 @@
   21.12          public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   21.13              if (diagnostic.getKind() == Diagnostic.Kind.WARNING &&
   21.14                      diagnostic.getCode().contains("try.resource.not.referenced")) {
   21.15 -                String varName = ((JCDiagnostic)diagnostic).getArgs()[0].toString();
   21.16 +                String varName = unwrap(diagnostic).getArgs()[0].toString();
   21.17                  if (varName.equals(TwrStmt.TWR1.resourceName)) {
   21.18                      unused_r1 = true;
   21.19                  } else if (varName.equals(TwrStmt.TWR2.resourceName)) {
   21.20 @@ -246,5 +247,13 @@
   21.21                  }
   21.22              }
   21.23          }
   21.24 +
   21.25 +        private JCDiagnostic unwrap(Diagnostic<? extends JavaFileObject> diagnostic) {
   21.26 +            if (diagnostic instanceof JCDiagnostic)
   21.27 +                return (JCDiagnostic) diagnostic;
   21.28 +            if (diagnostic instanceof ClientCodeWrapper.DiagnosticSourceUnwrapper)
   21.29 +                return ((ClientCodeWrapper.DiagnosticSourceUnwrapper)diagnostic).d;
   21.30 +            throw new IllegalArgumentException();
   21.31 +        }
   21.32      }
   21.33  }
    22.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    22.2 +++ b/test/tools/javac/annotations/T7043371.java	Mon Sep 19 19:41:46 2011 -0700
    22.3 @@ -0,0 +1,43 @@
    22.4 +/*
    22.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
    22.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    22.7 + *
    22.8 + * This code is free software; you can redistribute it and/or modify it
    22.9 + * under the terms of the GNU General Public License version 2 only, as
   22.10 + * published by the Free Software Foundation.
   22.11 + *
   22.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
   22.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   22.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   22.15 + * version 2 for more details (a copy is included in the LICENSE file that
   22.16 + * accompanied this code).
   22.17 + *
   22.18 + * You should have received a copy of the GNU General Public License version
   22.19 + * 2 along with this work; if not, write to the Free Software Foundation,
   22.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   22.21 + *
   22.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   22.23 + * or visit www.oracle.com if you need additional information or have any
   22.24 + * questions.
   22.25 + */
   22.26 +
   22.27 +/*
   22.28 + * @test
   22.29 + * @bug 7043371
   22.30 + * @summary javac7 fails with NPE during compilation
   22.31 + * @compile T7043371.java
   22.32 + */
   22.33 +
   22.34 +@interface Anno {
   22.35 +    String value();
   22.36 +}
   22.37 +
   22.38 +class B {
   22.39 +    @Anno(value=A.a)
   22.40 +    public static final int b = 0;
   22.41 +}
   22.42 +
   22.43 +class A {
   22.44 +    @Deprecated
   22.45 +    public static final String a = "a";
   22.46 +}
    23.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    23.2 +++ b/test/tools/javac/annotations/T7073477.java	Mon Sep 19 19:41:46 2011 -0700
    23.3 @@ -0,0 +1,39 @@
    23.4 +/*
    23.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
    23.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    23.7 + *
    23.8 + * This code is free software; you can redistribute it and/or modify it
    23.9 + * under the terms of the GNU General Public License version 2 only, as
   23.10 + * published by the Free Software Foundation.
   23.11 + *
   23.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
   23.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   23.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   23.15 + * version 2 for more details (a copy is included in the LICENSE file that
   23.16 + * accompanied this code).
   23.17 + *
   23.18 + * You should have received a copy of the GNU General Public License version
   23.19 + * 2 along with this work; if not, write to the Free Software Foundation,
   23.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   23.21 + *
   23.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   23.23 + * or visit www.oracle.com if you need additional information or have any
   23.24 + * questions.
   23.25 + */
   23.26 +
   23.27 +/*
   23.28 + * @test
   23.29 + * @bug 7073477
   23.30 + * @summary NPE in com.sun.tools.javac.code.Symbol$VarSymbol.getConstValue
   23.31 + * @compile T7073477.java
   23.32 + */
   23.33 +
   23.34 +@SuppressWarnings(T7073477A.S)
   23.35 +class T7073477 {
   23.36 +}
   23.37 +
   23.38 +class T7073477A {
   23.39 +  @SuppressWarnings("")
   23.40 +  static final String S = "";
   23.41 +}
   23.42 +
    24.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    24.2 +++ b/test/tools/javac/api/7086261/T7086261.java	Mon Sep 19 19:41:46 2011 -0700
    24.3 @@ -0,0 +1,78 @@
    24.4 +/*
    24.5 + * Copyright (c) 20011, Oracle and/or its affiliates. All rights reserved.
    24.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    24.7 + *
    24.8 + * This code is free software; you can redistribute it and/or modify it
    24.9 + * under the terms of the GNU General Public License version 2 only, as
   24.10 + * published by the Free Software Foundation.
   24.11 + *
   24.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
   24.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   24.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   24.15 + * version 2 for more details (a copy is included in the LICENSE file that
   24.16 + * accompanied this code).
   24.17 + *
   24.18 + * You should have received a copy of the GNU General Public License version
   24.19 + * 2 along with this work; if not, write to the Free Software Foundation,
   24.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   24.21 + *
   24.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   24.23 + * or visit www.oracle.com if you need additional information or have any
   24.24 + * questions.
   24.25 + */
   24.26 +
   24.27 +/*
   24.28 + * @test
   24.29 + * @bug 7086261
   24.30 + * @summary javac doesn't report error as expected, it only reports ClientCodeWrapper$DiagnosticSourceUnwrapper
   24.31 + */
   24.32 +
   24.33 +import javax.tools.*;
   24.34 +
   24.35 +import com.sun.tools.javac.api.ClientCodeWrapper.DiagnosticSourceUnwrapper;
   24.36 +import com.sun.tools.javac.util.JCDiagnostic;
   24.37 +
   24.38 +import java.net.URI;
   24.39 +import java.util.Arrays;
   24.40 +
   24.41 +import static javax.tools.StandardLocation.*;
   24.42 +import static javax.tools.JavaFileObject.Kind.*;
   24.43 +
   24.44 +
   24.45 +public class T7086261 {
   24.46 +
   24.47 +    static class ErroneousSource extends SimpleJavaFileObject {
   24.48 +        public ErroneousSource() {
   24.49 +            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   24.50 +        }
   24.51 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   24.52 +            return "class Test { NonexistentClass c = null; }";
   24.53 +        }
   24.54 +    }
   24.55 +
   24.56 +    static class DiagnosticChecker implements DiagnosticListener<javax.tools.JavaFileObject> {
   24.57 +        public void report(Diagnostic message) {
   24.58 +            if (!(message instanceof DiagnosticSourceUnwrapper)) {
   24.59 +                throw new AssertionError("Wrapped diagnostic expected!");
   24.60 +            }
   24.61 +            String actual = message.toString();
   24.62 +            JCDiagnostic jd = (JCDiagnostic)((DiagnosticSourceUnwrapper)message).d;
   24.63 +            String expected = jd.toString();
   24.64 +            if (!actual.equals(expected)) {
   24.65 +                throw new AssertionError("expected = " + expected + "\nfound = " + actual);
   24.66 +            }
   24.67 +        }
   24.68 +    };
   24.69 +
   24.70 +    void test() throws Throwable {
   24.71 +        JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
   24.72 +        JavaFileManager jfm = javac.getStandardFileManager(null, null, null);
   24.73 +        JavaCompiler.CompilationTask task =
   24.74 +                javac.getTask(null, jfm, new DiagnosticChecker(), null, null, Arrays.asList(new ErroneousSource()));
   24.75 +        task.call();
   24.76 +    }
   24.77 +
   24.78 +    public static void main(String[] args) throws Throwable {
   24.79 +        new T7086261().test();
   24.80 +    }
   24.81 +}
    25.1 --- a/test/tools/javac/diags/Example.java	Thu Sep 15 18:53:41 2011 -0700
    25.2 +++ b/test/tools/javac/diags/Example.java	Mon Sep 19 19:41:46 2011 -0700
    25.3 @@ -21,10 +21,12 @@
    25.4   * questions.
    25.5   */
    25.6  
    25.7 -import com.sun.tools.javac.file.JavacFileManager;
    25.8  import java.io.*;
    25.9 +import java.net.URL;
   25.10 +import java.net.URLClassLoader;
   25.11  import java.util.*;
   25.12  import java.util.regex.*;
   25.13 +import javax.annotation.processing.Processor;
   25.14  import javax.tools.Diagnostic;
   25.15  import javax.tools.DiagnosticCollector;
   25.16  import javax.tools.JavaCompiler;
   25.17 @@ -37,12 +39,11 @@
   25.18  // import com.sun.tools.javac.Main
   25.19  // import com.sun.tools.javac.main.Main
   25.20  
   25.21 +import com.sun.tools.javac.api.ClientCodeWrapper;
   25.22 +import com.sun.tools.javac.file.JavacFileManager;
   25.23  import com.sun.tools.javac.util.Context;
   25.24  import com.sun.tools.javac.util.JavacMessages;
   25.25  import com.sun.tools.javac.util.JCDiagnostic;
   25.26 -import java.net.URL;
   25.27 -import java.net.URLClassLoader;
   25.28 -import javax.annotation.processing.Processor;
   25.29  
   25.30  /**
   25.31   * Class to handle example code designed to illustrate javac diagnostic messages.
   25.32 @@ -397,7 +398,7 @@
   25.33  
   25.34              if (keys != null) {
   25.35                  for (Diagnostic<? extends JavaFileObject> d: dc.getDiagnostics()) {
   25.36 -                    scanForKeys((JCDiagnostic) d, keys);
   25.37 +                    scanForKeys(unwrap(d), keys);
   25.38                  }
   25.39              }
   25.40  
   25.41 @@ -418,6 +419,14 @@
   25.42              for (JCDiagnostic sd: d.getSubdiagnostics())
   25.43                  scanForKeys(sd, keys);
   25.44          }
   25.45 +
   25.46 +        private JCDiagnostic unwrap(Diagnostic<? extends JavaFileObject> diagnostic) {
   25.47 +            if (diagnostic instanceof JCDiagnostic)
   25.48 +                return (JCDiagnostic) diagnostic;
   25.49 +            if (diagnostic instanceof ClientCodeWrapper.DiagnosticSourceUnwrapper)
   25.50 +                return ((ClientCodeWrapper.DiagnosticSourceUnwrapper)diagnostic).d;
   25.51 +            throw new IllegalArgumentException();
   25.52 +        }
   25.53      }
   25.54  
   25.55      /**
    26.1 --- a/test/tools/javac/diags/examples/EmptyCharLiteral.java	Thu Sep 15 18:53:41 2011 -0700
    26.2 +++ b/test/tools/javac/diags/examples/EmptyCharLiteral.java	Mon Sep 19 19:41:46 2011 -0700
    26.3 @@ -1,5 +1,5 @@
    26.4  /*
    26.5 - * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
    26.6 + * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
    26.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    26.8   *
    26.9   * This code is free software; you can redistribute it and/or modify it
   26.10 @@ -23,7 +23,6 @@
   26.11  
   26.12  // key: compiler.err.empty.char.lit
   26.13  // key: compiler.err.unclosed.char.lit
   26.14 -// key: compiler.err.expected
   26.15  // key: compiler.err.premature.eof
   26.16  
   26.17  class X {
    27.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    27.2 +++ b/test/tools/javac/file/T7068451.java	Mon Sep 19 19:41:46 2011 -0700
    27.3 @@ -0,0 +1,168 @@
    27.4 +/*
    27.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
    27.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    27.7 + *
    27.8 + * This code is free software; you can redistribute it and/or modify it
    27.9 + * under the terms of the GNU General Public License version 2 only, as
   27.10 + * published by the Free Software Foundation.
   27.11 + *
   27.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
   27.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   27.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   27.15 + * version 2 for more details (a copy is included in the LICENSE file that
   27.16 + * accompanied this code).
   27.17 + *
   27.18 + * You should have received a copy of the GNU General Public License version
   27.19 + * 2 along with this work; if not, write to the Free Software Foundation,
   27.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   27.21 + *
   27.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   27.23 + * or visit www.oracle.com if you need additional information or have any
   27.24 + * questions.
   27.25 + */
   27.26 +
   27.27 +/*
   27.28 + * @test
   27.29 + * @bug 7068451
   27.30 + * @summary Regression: javac compiles fixed sources against previous,
   27.31 + *              not current, version of generated sources
   27.32 + */
   27.33 +
   27.34 +import java.io.File;
   27.35 +import java.io.FileNotFoundException;
   27.36 +import java.io.FileWriter;
   27.37 +import java.io.IOException;
   27.38 +import java.io.Writer;
   27.39 +import java.util.Arrays;
   27.40 +import java.util.Collections;
   27.41 +import java.util.List;
   27.42 +import java.util.Set;
   27.43 +import javax.annotation.processing.AbstractProcessor;
   27.44 +import javax.annotation.processing.Filer;
   27.45 +import javax.annotation.processing.Messager;
   27.46 +import javax.annotation.processing.RoundEnvironment;
   27.47 +import javax.annotation.processing.SupportedAnnotationTypes;
   27.48 +import javax.lang.model.SourceVersion;
   27.49 +import javax.lang.model.element.TypeElement;
   27.50 +import javax.tools.Diagnostic.Kind;
   27.51 +import javax.tools.JavaCompiler;
   27.52 +import javax.tools.JavaCompiler.CompilationTask;
   27.53 +import javax.tools.StandardLocation;
   27.54 +import javax.tools.ToolProvider;
   27.55 +
   27.56 +public class T7068451 {
   27.57 +    public static void main(String[] args) throws Exception {
   27.58 +        new T7068451().run();
   27.59 +    }
   27.60 +
   27.61 +    void run() throws Exception {
   27.62 +        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
   27.63 +        System.err.println("using " + compiler.getClass() + " from " + compiler.getClass().getProtectionDomain().getCodeSource());
   27.64 +
   27.65 +        File tmp = new File("tmp");
   27.66 +        tmp.mkdir();
   27.67 +        for (File f: tmp.listFiles())
   27.68 +            f.delete();
   27.69 +
   27.70 +        File input = writeFile(tmp, "X.java", "package p; class X { { p.C.first(); } }");
   27.71 +
   27.72 +        List<String> opts = Arrays.asList(
   27.73 +                "-s", tmp.getPath(),
   27.74 +                "-d", tmp.getPath(),
   27.75 +                "-XprintRounds");
   27.76 +
   27.77 +        System.err.println();
   27.78 +        System.err.println("FIRST compilation");
   27.79 +        System.err.println();
   27.80 +
   27.81 +        CompilationTask task = compiler.getTask(null, null, null, opts, null,
   27.82 +                compiler.getStandardFileManager(null, null, null).getJavaFileObjects(input));
   27.83 +        task.setProcessors(Collections.singleton(new Proc("first")));
   27.84 +        check("compilation", task.call());
   27.85 +
   27.86 +        writeFile(tmp, "X.java", "package p; class X { { p.C.second(); } }");
   27.87 +
   27.88 +        //Thread.sleep(2000);
   27.89 +
   27.90 +        System.err.println();
   27.91 +        System.err.println("SECOND compilation");
   27.92 +        System.err.println();
   27.93 +
   27.94 +        task = compiler.getTask(null, null, null, opts, null,
   27.95 +                compiler.getStandardFileManager(null, null, null).getJavaFileObjects(input));
   27.96 +        task.setProcessors(Collections.singleton(new Proc("second")));
   27.97 +        check("compilation", task.call());
   27.98 +
   27.99 +        //Thread.sleep(2000);
  27.100 +
  27.101 +        System.err.println();
  27.102 +        System.err.println("SECOND compilation, REPEATED");
  27.103 +        System.err.println();
  27.104 +
  27.105 +        task = compiler.getTask(null, null, null, opts, null,
  27.106 +                compiler.getStandardFileManager(null, null, null).getJavaFileObjects(input));
  27.107 +        task.setProcessors(Collections.singleton(new Proc("second")));
  27.108 +        check("compilation", task.call());
  27.109 +    }
  27.110 +
  27.111 +    void check(String msg, boolean ok) {
  27.112 +        System.err.println(msg + ": " + (ok ? "ok" : "failed"));
  27.113 +        if (!ok)
  27.114 +            throw new AssertionError(msg);
  27.115 +    }
  27.116 +
  27.117 +    static File writeFile(File base, String path, String body) throws IOException {
  27.118 +        File f = new File(base, path);
  27.119 +        FileWriter out = new FileWriter(f);
  27.120 +        out.write(body);
  27.121 +        out.close();
  27.122 +        System.err.println("wrote " + path + ": " + body);
  27.123 +        return f;
  27.124 +    }
  27.125 +
  27.126 +    @SupportedAnnotationTypes("*")
  27.127 +    private static class Proc extends AbstractProcessor {
  27.128 +        final String m;
  27.129 +        Proc(String m) {
  27.130 +            this.m = m;
  27.131 +        }
  27.132 +
  27.133 +        int count;
  27.134 +        @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
  27.135 +            if (roundEnv.processingOver() || count++ > 0) {
  27.136 +                return false;
  27.137 +            }
  27.138 +
  27.139 +            Filer filer = processingEnv.getFiler();
  27.140 +            Messager messager = processingEnv.getMessager();
  27.141 +
  27.142 +            System.err.println("running Proc");
  27.143 +            try {
  27.144 +                int len = filer.getResource(StandardLocation.SOURCE_OUTPUT, "p", "C.java").getCharContent(false).length();
  27.145 +                messager.printMessage(Kind.NOTE, "C.java: found previous content of length " + len);
  27.146 +            } catch (FileNotFoundException x) {
  27.147 +                messager.printMessage(Kind.NOTE, "C.java: not previously there");
  27.148 +            } catch (IOException x) {
  27.149 +                messager.printMessage(Kind.ERROR, "while reading: " + x);
  27.150 +            }
  27.151 +
  27.152 +            try {
  27.153 +                String body = "package p; public class C { public static void " + m + "() {} }";
  27.154 +                Writer w = filer.createSourceFile("p.C").openWriter();
  27.155 +                w.write(body);
  27.156 +                w.close();
  27.157 +                messager.printMessage(Kind.NOTE, "C.java: wrote new content: " + body);
  27.158 +            } catch (IOException x) {
  27.159 +                messager.printMessage(Kind.ERROR, "while writing: " + x);
  27.160 +            }
  27.161 +
  27.162 +            return true;
  27.163 +        }
  27.164 +
  27.165 +        @Override
  27.166 +        public SourceVersion getSupportedSourceVersion() {
  27.167 +            return SourceVersion.latest();
  27.168 +        }
  27.169 +    }
  27.170 +}
  27.171 +
    28.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    28.2 +++ b/test/tools/javac/jvm/T7024096.java	Mon Sep 19 19:41:46 2011 -0700
    28.3 @@ -0,0 +1,31 @@
    28.4 +/*
    28.5 + * @test  /nodynamiccopyright/
    28.6 + * @bug 7024096
    28.7 + * @summary Stack trace has invalid line numbers
    28.8 + * @author Bruce Chapman
    28.9 + * @compile T7024096.java
   28.10 + * @run main T7024096
   28.11 + */
   28.12 +
   28.13 +public class T7024096 {
   28.14 +    private static final int START = 14; // starting line number for the test
   28.15 +    public static void main(String[] args) {
   28.16 +        T7024096 m = new T7024096();
   28.17 +        m.nest(START);
   28.18 +        m.nest(START + 1, m.nest(START + 1), m.nest(START + 1),
   28.19 +            m.nest(START + 2),
   28.20 +            m.nest(START + 3, m.nest(START + 3)));
   28.21 +    }
   28.22 +
   28.23 +    public T7024096 nest(int expectedline, T7024096... args) {
   28.24 +        Exception e = new Exception("expected line#: " + expectedline);
   28.25 +        int myline = e.getStackTrace()[1].getLineNumber();
   28.26 +        if( myline != expectedline) {
   28.27 +            throw new RuntimeException("Incorrect line number " +
   28.28 +                    "expected: " + expectedline +
   28.29 +                    ", got: " + myline, e);
   28.30 +        }
   28.31 +        System.out.format("Got expected line number %d correct %n", myline);
   28.32 +        return null;
   28.33 +    }
   28.34 +}
    29.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    29.2 +++ b/test/tools/javac/parser/netbeans/JavacParserTest.java	Mon Sep 19 19:41:46 2011 -0700
    29.3 @@ -0,0 +1,716 @@
    29.4 +/*
    29.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
    29.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    29.7 + *
    29.8 + * This code is free software; you can redistribute it and/or modify it
    29.9 + * under the terms of the GNU General Public License version 2 only, as
   29.10 + * published by the Free Software Foundation.
   29.11 + *
   29.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
   29.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   29.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   29.15 + * version 2 for more details (a copy is included in the LICENSE file that
   29.16 + * accompanied this code).
   29.17 + *
   29.18 + * You should have received a copy of the GNU General Public License version
   29.19 + * 2 along with this work; if not, write to the Free Software Foundation,
   29.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   29.21 + *
   29.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   29.23 + * or visit www.oracle.com if you need additional information or have any
   29.24 + * questions.
   29.25 + */
   29.26 +
   29.27 +/*
   29.28 + * @test
   29.29 + * @bug 7073631
   29.30 + * @summary tests error and diagnostics positions
   29.31 + * @author  jan.lahoda@oracle.com
   29.32 + */
   29.33 +
   29.34 +import com.sun.source.tree.BinaryTree;
   29.35 +import com.sun.source.tree.BlockTree;
   29.36 +import com.sun.source.tree.ClassTree;
   29.37 +import com.sun.source.tree.CompilationUnitTree;
   29.38 +import com.sun.source.tree.ExpressionStatementTree;
   29.39 +import com.sun.source.tree.ExpressionTree;
   29.40 +import com.sun.source.tree.MethodInvocationTree;
   29.41 +import com.sun.source.tree.MethodTree;
   29.42 +import com.sun.source.tree.ModifiersTree;
   29.43 +import com.sun.source.tree.StatementTree;
   29.44 +import com.sun.source.tree.Tree;
   29.45 +import com.sun.source.tree.Tree.Kind;
   29.46 +import com.sun.source.tree.VariableTree;
   29.47 +import com.sun.source.tree.WhileLoopTree;
   29.48 +import com.sun.source.util.SourcePositions;
   29.49 +import com.sun.source.util.TreeScanner;
   29.50 +import com.sun.source.util.Trees;
   29.51 +import com.sun.tools.javac.api.JavacTaskImpl;
   29.52 +import com.sun.tools.javac.tree.JCTree;
   29.53 +import java.io.IOException;
   29.54 +import java.net.URI;
   29.55 +import java.util.Arrays;
   29.56 +import java.util.LinkedList;
   29.57 +import java.util.List;
   29.58 +import javax.tools.Diagnostic;
   29.59 +import javax.tools.DiagnosticCollector;
   29.60 +import javax.tools.DiagnosticListener;
   29.61 +import javax.tools.JavaCompiler;
   29.62 +import javax.tools.JavaFileObject;
   29.63 +import javax.tools.SimpleJavaFileObject;
   29.64 +import javax.tools.ToolProvider;
   29.65 +
   29.66 +public class JavacParserTest extends TestCase {
   29.67 +    final JavaCompiler tool;
   29.68 +    public JavacParserTest(String testName) {
   29.69 +        tool = ToolProvider.getSystemJavaCompiler();
   29.70 +        System.out.println("java.home=" + System.getProperty("java.home"));
   29.71 +    }
   29.72 +
   29.73 +    static class MyFileObject extends SimpleJavaFileObject {
   29.74 +
   29.75 +        private String text;
   29.76 +
   29.77 +        public MyFileObject(String text) {
   29.78 +            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   29.79 +            this.text = text;
   29.80 +        }
   29.81 +
   29.82 +        @Override
   29.83 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   29.84 +            return text;
   29.85 +        }
   29.86 +    }
   29.87 +
   29.88 +    public void testPositionForSuperConstructorCalls() throws IOException {
   29.89 +        assert tool != null;
   29.90 +
   29.91 +        String code = "package test; public class Test {public Test() {super();}}";
   29.92 +
   29.93 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
   29.94 +                null, Arrays.asList(new MyFileObject(code)));
   29.95 +        CompilationUnitTree cut = ct.parse().iterator().next();
   29.96 +        SourcePositions pos = Trees.instance(ct).getSourcePositions();
   29.97 +
   29.98 +        MethodTree method =
   29.99 +                (MethodTree) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
  29.100 +        ExpressionStatementTree es =
  29.101 +                (ExpressionStatementTree) method.getBody().getStatements().get(0);
  29.102 +
  29.103 +        assertEquals("testPositionForSuperConstructorCalls",
  29.104 +                72 - 24, pos.getStartPosition(cut, es));
  29.105 +        assertEquals("testPositionForSuperConstructorCalls",
  29.106 +                80 - 24, pos.getEndPosition(cut, es));
  29.107 +
  29.108 +        MethodInvocationTree mit = (MethodInvocationTree) es.getExpression();
  29.109 +
  29.110 +        assertEquals("testPositionForSuperConstructorCalls",
  29.111 +                72 - 24, pos.getStartPosition(cut, mit));
  29.112 +        assertEquals("testPositionForSuperConstructorCalls",
  29.113 +                79 - 24, pos.getEndPosition(cut, mit));
  29.114 +
  29.115 +        assertEquals("testPositionForSuperConstructorCalls",
  29.116 +                72 - 24, pos.getStartPosition(cut, mit.getMethodSelect()));
  29.117 +        assertEquals("testPositionForSuperConstructorCalls",
  29.118 +                77 - 24, pos.getEndPosition(cut, mit.getMethodSelect()));
  29.119 +
  29.120 +    }
  29.121 +
  29.122 +    public void testPositionForEnumModifiers() throws IOException {
  29.123 +
  29.124 +        String code = "package test; public enum Test {A;}";
  29.125 +
  29.126 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
  29.127 +                null, Arrays.asList(new MyFileObject(code)));
  29.128 +        CompilationUnitTree cut = ct.parse().iterator().next();
  29.129 +        SourcePositions pos = Trees.instance(ct).getSourcePositions();
  29.130 +
  29.131 +        ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
  29.132 +        ModifiersTree mt = clazz.getModifiers();
  29.133 +
  29.134 +        assertEquals("testPositionForEnumModifiers",
  29.135 +                38 - 24, pos.getStartPosition(cut, mt));
  29.136 +        assertEquals("testPositionForEnumModifiers",
  29.137 +                44 - 24, pos.getEndPosition(cut, mt));
  29.138 +    }
  29.139 +
  29.140 +    public void testNewClassWithEnclosing() throws IOException {
  29.141 +
  29.142 +
  29.143 +        String code = "package test; class Test { " +
  29.144 +                "class d {} private void method() { " +
  29.145 +                "Object o = Test.this.new d(); } }";
  29.146 +
  29.147 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
  29.148 +                null, Arrays.asList(new MyFileObject(code)));
  29.149 +        CompilationUnitTree cut = ct.parse().iterator().next();
  29.150 +        SourcePositions pos = Trees.instance(ct).getSourcePositions();
  29.151 +
  29.152 +        ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
  29.153 +        ExpressionTree est =
  29.154 +                ((VariableTree) ((MethodTree) clazz.getMembers().get(1)).getBody().getStatements().get(0)).getInitializer();
  29.155 +
  29.156 +        assertEquals("testNewClassWithEnclosing",
  29.157 +                97 - 24, pos.getStartPosition(cut, est));
  29.158 +        assertEquals("testNewClassWithEnclosing",
  29.159 +                114 - 24, pos.getEndPosition(cut, est));
  29.160 +    }
  29.161 +
  29.162 +    public void testPreferredPositionForBinaryOp() throws IOException {
  29.163 +
  29.164 +        String code = "package test; public class Test {" +
  29.165 +                "private void test() {" +
  29.166 +                "Object o = null; boolean b = o != null && o instanceof String;" +
  29.167 +                "} private Test() {}}";
  29.168 +
  29.169 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
  29.170 +                null, Arrays.asList(new MyFileObject(code)));
  29.171 +        CompilationUnitTree cut = ct.parse().iterator().next();
  29.172 +
  29.173 +        ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
  29.174 +        MethodTree method = (MethodTree) clazz.getMembers().get(0);
  29.175 +        VariableTree condSt = (VariableTree) method.getBody().getStatements().get(1);
  29.176 +        BinaryTree cond = (BinaryTree) condSt.getInitializer();
  29.177 +
  29.178 +        JCTree condJC = (JCTree) cond;
  29.179 +
  29.180 +        assertEquals("testNewClassWithEnclosing",
  29.181 +                117 - 24, condJC.pos);
  29.182 +    }
  29.183 +
  29.184 +    public void testPositionBrokenSource126732a() throws IOException {
  29.185 +        String[] commands = new String[]{
  29.186 +            "return Runnable()",
  29.187 +            "do { } while (true)",
  29.188 +            "throw UnsupportedOperationException()",
  29.189 +            "assert true",
  29.190 +            "1 + 1",};
  29.191 +
  29.192 +        for (String command : commands) {
  29.193 +
  29.194 +            String code = "package test;\n"
  29.195 +                    + "public class Test {\n"
  29.196 +                    + "    public static void test() {\n"
  29.197 +                    + "        " + command + " {\n"
  29.198 +                    + "                new Runnable() {\n"
  29.199 +                    + "        };\n"
  29.200 +                    + "    }\n"
  29.201 +                    + "}";
  29.202 +            JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null,
  29.203 +                    null, null, Arrays.asList(new MyFileObject(code)));
  29.204 +            CompilationUnitTree cut = ct.parse().iterator().next();
  29.205 +
  29.206 +            ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
  29.207 +            MethodTree method = (MethodTree) clazz.getMembers().get(0);
  29.208 +            List<? extends StatementTree> statements =
  29.209 +                    method.getBody().getStatements();
  29.210 +
  29.211 +            StatementTree ret = statements.get(0);
  29.212 +            StatementTree block = statements.get(1);
  29.213 +
  29.214 +            Trees t = Trees.instance(ct);
  29.215 +            int len = code.indexOf(command + " {") + (command + " ").length();
  29.216 +            assertEquals(command, len,
  29.217 +                    t.getSourcePositions().getEndPosition(cut, ret));
  29.218 +            assertEquals(command, len,
  29.219 +                    t.getSourcePositions().getStartPosition(cut, block));
  29.220 +        }
  29.221 +    }
  29.222 +
  29.223 +    public void testPositionBrokenSource126732b() throws IOException {
  29.224 +        String[] commands = new String[]{
  29.225 +            "break",
  29.226 +            "break A",
  29.227 +            "continue ",
  29.228 +            "continue A",};
  29.229 +
  29.230 +        for (String command : commands) {
  29.231 +
  29.232 +            String code = "package test;\n"
  29.233 +                    + "public class Test {\n"
  29.234 +                    + "    public static void test() {\n"
  29.235 +                    + "        while (true) {\n"
  29.236 +                    + "            " + command + " {\n"
  29.237 +                    + "                new Runnable() {\n"
  29.238 +                    + "        };\n"
  29.239 +                    + "        }\n"
  29.240 +                    + "    }\n"
  29.241 +                    + "}";
  29.242 +
  29.243 +            JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null,
  29.244 +                    null, null, Arrays.asList(new MyFileObject(code)));
  29.245 +            CompilationUnitTree cut = ct.parse().iterator().next();
  29.246 +
  29.247 +            ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
  29.248 +            MethodTree method = (MethodTree) clazz.getMembers().get(0);
  29.249 +            List<? extends StatementTree> statements =
  29.250 +                    ((BlockTree) ((WhileLoopTree) method.getBody().getStatements().get(0)).getStatement()).getStatements();
  29.251 +
  29.252 +            StatementTree ret = statements.get(0);
  29.253 +            StatementTree block = statements.get(1);
  29.254 +
  29.255 +            Trees t = Trees.instance(ct);
  29.256 +            int len = code.indexOf(command + " {") + (command + " ").length();
  29.257 +            assertEquals(command, len,
  29.258 +                    t.getSourcePositions().getEndPosition(cut, ret));
  29.259 +            assertEquals(command, len,
  29.260 +                    t.getSourcePositions().getStartPosition(cut, block));
  29.261 +        }
  29.262 +    }
  29.263 +
  29.264 +    public void testErrorRecoveryForEnhancedForLoop142381() throws IOException {
  29.265 +
  29.266 +        String code = "package test; class Test { " +
  29.267 +                "private void method() { " +
  29.268 +                "java.util.Set<String> s = null; for (a : s) {} } }";
  29.269 +
  29.270 +        final List<Diagnostic<? extends JavaFileObject>> errors =
  29.271 +                new LinkedList<Diagnostic<? extends JavaFileObject>>();
  29.272 +
  29.273 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null,
  29.274 +                new DiagnosticListener<JavaFileObject>() {
  29.275 +            public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
  29.276 +                errors.add(diagnostic);
  29.277 +            }
  29.278 +        }, null, null, Arrays.asList(new MyFileObject(code)));
  29.279 +
  29.280 +        CompilationUnitTree cut = ct.parse().iterator().next();
  29.281 +
  29.282 +        ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
  29.283 +        StatementTree forStatement =
  29.284 +                ((MethodTree) clazz.getMembers().get(0)).getBody().getStatements().get(1);
  29.285 +
  29.286 +        assertEquals("testErrorRecoveryForEnhancedForLoop142381",
  29.287 +                Kind.ENHANCED_FOR_LOOP, forStatement.getKind());
  29.288 +        assertFalse("testErrorRecoveryForEnhancedForLoop142381", errors.isEmpty());
  29.289 +    }
  29.290 +
  29.291 +    public void testPositionAnnotationNoPackage187551() throws IOException {
  29.292 +
  29.293 +        String code = "\n@interface Test {}";
  29.294 +
  29.295 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
  29.296 +                null, Arrays.asList(new MyFileObject(code)));
  29.297 +
  29.298 +        CompilationUnitTree cut = ct.parse().iterator().next();
  29.299 +        ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
  29.300 +        Trees t = Trees.instance(ct);
  29.301 +
  29.302 +        assertEquals("testPositionAnnotationNoPackage187551",
  29.303 +                1, t.getSourcePositions().getStartPosition(cut, clazz));
  29.304 +    }
  29.305 +
  29.306 +    public void testPositionsSane() throws IOException {
  29.307 +        performPositionsSanityTest("package test; class Test { " +
  29.308 +                "private void method() { " +
  29.309 +                "java.util.List<? extends java.util.List<? extends String>> l; " +
  29.310 +                "} }");
  29.311 +        performPositionsSanityTest("package test; class Test { " +
  29.312 +                "private void method() { " +
  29.313 +                "java.util.List<? super java.util.List<? super String>> l; " +
  29.314 +                "} }");
  29.315 +        performPositionsSanityTest("package test; class Test { " +
  29.316 +                "private void method() { " +
  29.317 +                "java.util.List<? super java.util.List<?>> l; } }");
  29.318 +    }
  29.319 +
  29.320 +    private void performPositionsSanityTest(String code) throws IOException {
  29.321 +
  29.322 +        final List<Diagnostic<? extends JavaFileObject>> errors =
  29.323 +                new LinkedList<Diagnostic<? extends JavaFileObject>>();
  29.324 +
  29.325 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null,
  29.326 +                new DiagnosticListener<JavaFileObject>() {
  29.327 +
  29.328 +            public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
  29.329 +                errors.add(diagnostic);
  29.330 +            }
  29.331 +        }, null, null, Arrays.asList(new MyFileObject(code)));
  29.332 +
  29.333 +        final CompilationUnitTree cut = ct.parse().iterator().next();
  29.334 +        final Trees trees = Trees.instance(ct);
  29.335 +
  29.336 +        new TreeScanner<Void, Void>() {
  29.337 +
  29.338 +            private long parentStart = 0;
  29.339 +            private long parentEnd = Integer.MAX_VALUE;
  29.340 +
  29.341 +            @Override
  29.342 +            public Void scan(Tree node, Void p) {
  29.343 +                if (node == null) {
  29.344 +                    return null;
  29.345 +                }
  29.346 +
  29.347 +                long start = trees.getSourcePositions().getStartPosition(cut, node);
  29.348 +
  29.349 +                if (start == (-1)) {
  29.350 +                    return null; //synthetic tree
  29.351 +                }
  29.352 +                assertTrue(node.toString() + ":" + start + "/" + parentStart,
  29.353 +                        parentStart <= start);
  29.354 +
  29.355 +                long prevParentStart = parentStart;
  29.356 +
  29.357 +                parentStart = start;
  29.358 +
  29.359 +                long end = trees.getSourcePositions().getEndPosition(cut, node);
  29.360 +
  29.361 +                assertTrue(node.toString() + ":" + end + "/" + parentEnd,
  29.362 +                        end <= parentEnd);
  29.363 +
  29.364 +                long prevParentEnd = parentEnd;
  29.365 +
  29.366 +                parentEnd = end;
  29.367 +
  29.368 +                super.scan(node, p);
  29.369 +
  29.370 +                parentStart = prevParentStart;
  29.371 +                parentEnd = prevParentEnd;
  29.372 +
  29.373 +                return null;
  29.374 +            }
  29.375 +
  29.376 +            private void assertTrue(String message, boolean b) {
  29.377 +                if (!b) fail(message);
  29.378 +            }
  29.379 +        }.scan(cut, null);
  29.380 +    }
  29.381 +
  29.382 +    public void testCorrectWilcardPositions() throws IOException {
  29.383 +        performWildcardPositionsTest("package test; import java.util.List; " +
  29.384 +                "class Test { private void method() { List<? extends List<? extends String>> l; } }",
  29.385 +
  29.386 +                Arrays.asList("List<? extends List<? extends String>> l;",
  29.387 +                "List<? extends List<? extends String>>",
  29.388 +                "List",
  29.389 +                "? extends List<? extends String>",
  29.390 +                "List<? extends String>",
  29.391 +                "List",
  29.392 +                "? extends String",
  29.393 +                "String"));
  29.394 +        performWildcardPositionsTest("package test; import java.util.List; " +
  29.395 +                "class Test { private void method() { List<? super List<? super String>> l; } }",
  29.396 +
  29.397 +                Arrays.asList("List<? super List<? super String>> l;",
  29.398 +                "List<? super List<? super String>>",
  29.399 +                "List",
  29.400 +                "? super List<? super String>",
  29.401 +                "List<? super String>",
  29.402 +                "List",
  29.403 +                "? super String",
  29.404 +                "String"));
  29.405 +        performWildcardPositionsTest("package test; import java.util.List; " +
  29.406 +                "class Test { private void method() { List<? super List<?>> l; } }",
  29.407 +
  29.408 +                Arrays.asList("List<? super List<?>> l;",
  29.409 +                "List<? super List<?>>",
  29.410 +                "List",
  29.411 +                "? super List<?>",
  29.412 +                "List<?>",
  29.413 +                "List",
  29.414 +                "?"));
  29.415 +        performWildcardPositionsTest("package test; import java.util.List; " +
  29.416 +                "class Test { private void method() { " +
  29.417 +                "List<? extends List<? extends List<? extends String>>> l; } }",
  29.418 +
  29.419 +                Arrays.asList("List<? extends List<? extends List<? extends String>>> l;",
  29.420 +                "List<? extends List<? extends List<? extends String>>>",
  29.421 +                "List",
  29.422 +                "? extends List<? extends List<? extends String>>",
  29.423 +                "List<? extends List<? extends String>>",
  29.424 +                "List",
  29.425 +                "? extends List<? extends String>",
  29.426 +                "List<? extends String>",
  29.427 +                "List",
  29.428 +                "? extends String",
  29.429 +                "String"));
  29.430 +        performWildcardPositionsTest("package test; import java.util.List; " +
  29.431 +                "class Test { private void method() { " +
  29.432 +                "List<? extends List<? extends List<? extends String   >>> l; } }",
  29.433 +                Arrays.asList("List<? extends List<? extends List<? extends String   >>> l;",
  29.434 +                "List<? extends List<? extends List<? extends String   >>>",
  29.435 +                "List",
  29.436 +                "? extends List<? extends List<? extends String   >>",
  29.437 +                "List<? extends List<? extends String   >>",
  29.438 +                "List",
  29.439 +                "? extends List<? extends String   >",
  29.440 +                "List<? extends String   >",
  29.441 +                "List",
  29.442 +                "? extends String",
  29.443 +                "String"));
  29.444 +    }
  29.445 +
  29.446 +    public void performWildcardPositionsTest(final String code,
  29.447 +            List<String> golden) throws IOException {
  29.448 +
  29.449 +        final List<Diagnostic<? extends JavaFileObject>> errors =
  29.450 +                new LinkedList<Diagnostic<? extends JavaFileObject>>();
  29.451 +
  29.452 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null,
  29.453 +                new DiagnosticListener<JavaFileObject>() {
  29.454 +                    public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
  29.455 +                        errors.add(diagnostic);
  29.456 +                    }
  29.457 +                }, null, null, Arrays.asList(new MyFileObject(code)));
  29.458 +
  29.459 +        final CompilationUnitTree cut = ct.parse().iterator().next();
  29.460 +        final List<String> content = new LinkedList<String>();
  29.461 +        final Trees trees = Trees.instance(ct);
  29.462 +
  29.463 +        new TreeScanner<Void, Void>() {
  29.464 +            @Override
  29.465 +            public Void scan(Tree node, Void p) {
  29.466 +                if (node == null) {
  29.467 +                    return null;
  29.468 +                }
  29.469 +                long start = trees.getSourcePositions().getStartPosition(cut, node);
  29.470 +
  29.471 +                if (start == (-1)) {
  29.472 +                    return null; //synthetic tree
  29.473 +                }
  29.474 +                long end = trees.getSourcePositions().getEndPosition(cut, node);
  29.475 +                String s = code.substring((int) start, (int) end);
  29.476 +                content.add(s);
  29.477 +
  29.478 +                return super.scan(node, p);
  29.479 +            }
  29.480 +        }.scan(((MethodTree) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0)).getBody().getStatements().get(0), null);
  29.481 +
  29.482 +        assertEquals("performWildcardPositionsTest",golden.toString(),
  29.483 +                content.toString());
  29.484 +    }
  29.485 +
  29.486 +    public void testStartPositionForMethodWithoutModifiers() throws IOException {
  29.487 +
  29.488 +        String code = "package t; class Test { <T> void t() {} }";
  29.489 +
  29.490 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
  29.491 +                null, Arrays.asList(new MyFileObject(code)));
  29.492 +        CompilationUnitTree cut = ct.parse().iterator().next();
  29.493 +        ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
  29.494 +        MethodTree mt = (MethodTree) clazz.getMembers().get(0);
  29.495 +        Trees t = Trees.instance(ct);
  29.496 +        int start = (int) t.getSourcePositions().getStartPosition(cut, mt);
  29.497 +        int end = (int) t.getSourcePositions().getEndPosition(cut, mt);
  29.498 +
  29.499 +        assertEquals("testStartPositionForMethodWithoutModifiers",
  29.500 +                "<T> void t() {}", code.substring(start, end));
  29.501 +    }
  29.502 +
  29.503 +    public void testStartPositionEnumConstantInit() throws IOException {
  29.504 +
  29.505 +        String code = "package t; enum Test { AAA; }";
  29.506 +
  29.507 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
  29.508 +                null, Arrays.asList(new MyFileObject(code)));
  29.509 +        CompilationUnitTree cut = ct.parse().iterator().next();
  29.510 +        ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
  29.511 +        VariableTree enumAAA = (VariableTree) clazz.getMembers().get(0);
  29.512 +        Trees t = Trees.instance(ct);
  29.513 +        int start = (int) t.getSourcePositions().getStartPosition(cut,
  29.514 +                enumAAA.getInitializer());
  29.515 +
  29.516 +        assertEquals("testStartPositionEnumConstantInit", -1, start);
  29.517 +    }
  29.518 +
  29.519 +    public void testVariableInIfThen1() throws IOException {
  29.520 +
  29.521 +        String code = "package t; class Test { " +
  29.522 +                "private static void t(String name) { " +
  29.523 +                "if (name != null) String nn = name.trim(); } }";
  29.524 +
  29.525 +        DiagnosticCollector<JavaFileObject> coll =
  29.526 +                new DiagnosticCollector<JavaFileObject>();
  29.527 +
  29.528 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
  29.529 +                null, Arrays.asList(new MyFileObject(code)));
  29.530 +
  29.531 +        ct.parse();
  29.532 +
  29.533 +        List<String> codes = new LinkedList<String>();
  29.534 +
  29.535 +        for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
  29.536 +            codes.add(d.getCode());
  29.537 +        }
  29.538 +
  29.539 +        assertEquals("testVariableInIfThen1",
  29.540 +                Arrays.<String>asList("compiler.err.variable.not.allowed"),
  29.541 +                codes);
  29.542 +    }
  29.543 +
  29.544 +    public void testVariableInIfThen2() throws IOException {
  29.545 +
  29.546 +        String code = "package t; class Test { " +
  29.547 +                "private static void t(String name) { " +
  29.548 +                "if (name != null) class X {} } }";
  29.549 +        DiagnosticCollector<JavaFileObject> coll =
  29.550 +                new DiagnosticCollector<JavaFileObject>();
  29.551 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
  29.552 +                null, Arrays.asList(new MyFileObject(code)));
  29.553 +
  29.554 +        ct.parse();
  29.555 +
  29.556 +        List<String> codes = new LinkedList<String>();
  29.557 +
  29.558 +        for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
  29.559 +            codes.add(d.getCode());
  29.560 +        }
  29.561 +
  29.562 +        assertEquals("testVariableInIfThen2",
  29.563 +                Arrays.<String>asList("compiler.err.class.not.allowed"), codes);
  29.564 +    }
  29.565 +
  29.566 +    public void testVariableInIfThen3() throws IOException {
  29.567 +
  29.568 +        String code = "package t; class Test { "+
  29.569 +                "private static void t(String name) { " +
  29.570 +                "if (name != null) abstract } }";
  29.571 +        DiagnosticCollector<JavaFileObject> coll =
  29.572 +                new DiagnosticCollector<JavaFileObject>();
  29.573 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
  29.574 +                null, Arrays.asList(new MyFileObject(code)));
  29.575 +
  29.576 +        ct.parse();
  29.577 +
  29.578 +        List<String> codes = new LinkedList<String>();
  29.579 +
  29.580 +        for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
  29.581 +            codes.add(d.getCode());
  29.582 +        }
  29.583 +
  29.584 +        assertEquals("testVariableInIfThen3",
  29.585 +                Arrays.<String>asList("compiler.err.illegal.start.of.expr"),
  29.586 +                codes);
  29.587 +    }
  29.588 +
  29.589 +    //see javac bug #6882235, NB bug #98234:
  29.590 +    public void testMissingExponent() throws IOException {
  29.591 +
  29.592 +        String code = "\nclass Test { { System.err.println(0e); } }";
  29.593 +
  29.594 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
  29.595 +                null, Arrays.asList(new MyFileObject(code)));
  29.596 +
  29.597 +        assertNotNull(ct.parse().iterator().next());
  29.598 +    }
  29.599 +
  29.600 +    public void testTryResourcePos() throws IOException {
  29.601 +
  29.602 +        final String code = "package t; class Test { " +
  29.603 +                "{ try (java.io.InputStream in = null) { } } }";
  29.604 +
  29.605 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
  29.606 +                null, Arrays.asList(new MyFileObject(code)));
  29.607 +        CompilationUnitTree cut = ct.parse().iterator().next();
  29.608 +
  29.609 +        new TreeScanner<Void, Void>() {
  29.610 +            @Override
  29.611 +            public Void visitVariable(VariableTree node, Void p) {
  29.612 +                if ("in".contentEquals(node.getName())) {
  29.613 +                    JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) node;
  29.614 +                    System.out.println(node.getName() + "," + var.pos);
  29.615 +                    assertEquals("testTryResourcePos", "in = null) { } } }",
  29.616 +                            code.substring(var.pos));
  29.617 +                }
  29.618 +                return super.visitVariable(node, p);
  29.619 +            }
  29.620 +        }.scan(cut, null);
  29.621 +    }
  29.622 +
  29.623 +    public void testVarPos() throws IOException {
  29.624 +
  29.625 +        final String code = "package t; class Test { " +
  29.626 +                "{ java.io.InputStream in = null; } }";
  29.627 +
  29.628 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
  29.629 +                null, Arrays.asList(new MyFileObject(code)));
  29.630 +        CompilationUnitTree cut = ct.parse().iterator().next();
  29.631 +
  29.632 +        new TreeScanner<Void, Void>() {
  29.633 +
  29.634 +            @Override
  29.635 +            public Void visitVariable(VariableTree node, Void p) {
  29.636 +                if ("in".contentEquals(node.getName())) {
  29.637 +                    JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) node;
  29.638 +                    assertEquals("testVarPos","in = null; } }",
  29.639 +                            code.substring(var.pos));
  29.640 +                }
  29.641 +                return super.visitVariable(node, p);
  29.642 +            }
  29.643 +        }.scan(cut, null);
  29.644 +    }
  29.645 +
  29.646 +    void testsNotWorking() throws IOException {
  29.647 +
  29.648 +        // Fails with nb-javac, needs further investigation
  29.649 +        testPositionBrokenSource126732a();
  29.650 +        testPositionBrokenSource126732b();
  29.651 +
  29.652 +        // Fails, these tests yet to be addressed
  29.653 +        testVariableInIfThen1();
  29.654 +        testVariableInIfThen2();
  29.655 +        testPositionForEnumModifiers();
  29.656 +        testStartPositionEnumConstantInit();
  29.657 +    }
  29.658 +    void testPositions() throws IOException {
  29.659 +        testPositionsSane();
  29.660 +        testCorrectWilcardPositions();
  29.661 +        testPositionAnnotationNoPackage187551();
  29.662 +        testPositionForSuperConstructorCalls();
  29.663 +        testPreferredPositionForBinaryOp();
  29.664 +        testStartPositionForMethodWithoutModifiers();
  29.665 +        testVarPos();
  29.666 +        testVariableInIfThen3();
  29.667 +        testTryResourcePos();
  29.668 +    }
  29.669 +
  29.670 +    public static void main(String... args) throws IOException {
  29.671 +        JavacParserTest jpt = new JavacParserTest("JavacParserTest");
  29.672 +        jpt.testPositions();
  29.673 +        System.out.println("PASS");
  29.674 +    }
  29.675 +}
  29.676 +
  29.677 +abstract class TestCase {
  29.678 +
  29.679 +    void assertEquals(String message, int i, int pos) {
  29.680 +        if (i != pos) {
  29.681 +            fail(message);
  29.682 +        }
  29.683 +    }
  29.684 +
  29.685 +    void assertFalse(String message, boolean empty) {
  29.686 +        throw new UnsupportedOperationException("Not yet implemented");
  29.687 +    }
  29.688 +
  29.689 +    void assertEquals(String message, int i, long l) {
  29.690 +        if (i != l) {
  29.691 +            fail(message + ":" + i + ":" + l);
  29.692 +        }
  29.693 +    }
  29.694 +
  29.695 +    void assertEquals(String message, Object o1, Object o2) {
  29.696 +        System.out.println(o1);
  29.697 +        System.out.println(o2);
  29.698 +        if (o1 != null && o2 != null && !o1.equals(o2)) {
  29.699 +            fail(message);
  29.700 +        }
  29.701 +        if (o1 == null && o2 != null) {
  29.702 +            fail(message);
  29.703 +        }
  29.704 +    }
  29.705 +
  29.706 +    void assertNotNull(Object o) {
  29.707 +        if (o == null) {
  29.708 +            fail();
  29.709 +        }
  29.710 +    }
  29.711 +
  29.712 +    void fail() {
  29.713 +        fail("test failed");
  29.714 +    }
  29.715 +
  29.716 +    void fail(String message) {
  29.717 +        throw new RuntimeException(message);
  29.718 +    }
  29.719 +}
    30.1 --- a/test/tools/javac/processing/errors/TestSuppression.java	Thu Sep 15 18:53:41 2011 -0700
    30.2 +++ b/test/tools/javac/processing/errors/TestSuppression.java	Mon Sep 19 19:41:46 2011 -0700
    30.3 @@ -1,5 +1,5 @@
    30.4  /*
    30.5 - * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
    30.6 + * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
    30.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    30.8   *
    30.9   * This code is free software; you can redistribute it and/or modify it
   30.10 @@ -35,6 +35,7 @@
   30.11  import javax.tools.*;
   30.12  
   30.13  import com.sun.source.util.JavacTask;
   30.14 +import com.sun.tools.javac.api.ClientCodeWrapper;
   30.15  import com.sun.tools.javac.api.JavacTool;
   30.16  import com.sun.tools.javac.util.JCDiagnostic;
   30.17  
   30.18 @@ -171,7 +172,7 @@
   30.19  
   30.20          public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   30.21              System.err.println((++total) + ": "
   30.22 -                    + "resolveError:" + isResolveError((JCDiagnostic) diagnostic) + "\n"
   30.23 +                    + "resolveError:" + isResolveError(unwrap(diagnostic)) + "\n"
   30.24                      + diagnostic);
   30.25              Diagnostic.Kind dk = diagnostic.getKind();
   30.26              Integer c = counts.get(dk);
   30.27 @@ -181,6 +182,14 @@
   30.28          private static boolean isResolveError(JCDiagnostic d) {
   30.29              return d.isFlagSet(RESOLVE_ERROR);
   30.30          }
   30.31 +
   30.32 +        private JCDiagnostic unwrap(Diagnostic<? extends JavaFileObject> diagnostic) {
   30.33 +            if (diagnostic instanceof JCDiagnostic)
   30.34 +                return (JCDiagnostic) diagnostic;
   30.35 +            if (diagnostic instanceof ClientCodeWrapper.DiagnosticSourceUnwrapper)
   30.36 +                return ((ClientCodeWrapper.DiagnosticSourceUnwrapper)diagnostic).d;
   30.37 +            throw new IllegalArgumentException();
   30.38 +        }
   30.39      }
   30.40  
   30.41      @SupportedAnnotationTypes("*")

mercurial