src/jdk.compiler/share/classes/com/sun/tools/javac/comp/ValueCapableClassAttr.java

Print this page




  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.javac.comp;
  27 
  28 import com.sun.tools.javac.code.*;
  29 
  30 import static com.sun.tools.javac.code.Flags.asFlagSet;
  31 import static com.sun.tools.javac.code.Kinds.Kind.*;
  32 import com.sun.tools.javac.code.Lint.LintCategory;
  33 import com.sun.tools.javac.code.Symbol.MethodSymbol;
  34 import com.sun.tools.javac.code.Symbol.OperatorSymbol;
  35 import com.sun.tools.javac.code.Symbol.TypeSymbol;
  36 import com.sun.tools.javac.jvm.ByteCodes;
  37 import com.sun.tools.javac.tree.*;
  38 import com.sun.tools.javac.tree.JCTree.*;
  39 import com.sun.tools.javac.util.*;
  40 
  41 /**
  42  * Support for "Minimal Value Types" : Process classes annotated with @DeriveValueType -
  43  * All semantic checks are centralized in this place so that we can blow them all away when
  44  * moving to "Maximal Value Types".
  45  *
  46  * see: http://cr.openjdk.java.net/~jrose/values/shady-values.html
  47  *
  48  */
  49 
  50 public class ValueCapableClassAttr extends TreeTranslator {
  51 
  52     protected static final Context.Key<ValueCapableClassAttr> valueCapableClassAttr = new Context.Key<>();
  53     private JCMethodDecl currentMethod;
  54     private Log log;
  55     private Names names;
  56     private Symtab syms;
  57     private final JCDiagnostic.Factory diags;
  58     private final Types types;
  59     private final Check chk;
  60     private boolean inValue = false;
  61 
  62     public static ValueCapableClassAttr instance(Context context) {


  65             instance = new ValueCapableClassAttr(context);
  66         return instance;
  67     }
  68 
  69     protected ValueCapableClassAttr(Context context) {
  70         context.put(valueCapableClassAttr, this);
  71         log = Log.instance(context);
  72         names = Names.instance(context);
  73         syms = Symtab.instance(context);
  74         diags = JCDiagnostic.Factory.instance(context);
  75         types = Types.instance(context);
  76         chk = Check.instance(context);
  77     }
  78 
  79     public void visitClassDef(JCClassDecl tree) {
  80         boolean oldInValue = inValue;
  81         try {
  82             inValue = false;
  83             for (List<JCAnnotation> al = tree.mods.annotations; !al.isEmpty(); al = al.tail) {
  84                 JCAnnotation a = al.head;
  85                 if (a.annotationType.type == syms.deriveValueType && a.args.isEmpty()) {
  86                     inValue = true;
  87                     tree.sym.flags_field |= Flags.VALUE_CAPABLE;
  88                     break;
  89                 }
  90             }
  91             if (inValue) {
  92                 if (tree.extending != null) {
  93                     log.warning(LintCategory.VALUES, tree.pos(), "value.may.not.extend");
  94                 }
  95                 if ((tree.mods.flags & Flags.FINAL) == 0) {
  96                     log.warning(LintCategory.VALUES, tree.pos(), "value.must.be.final");
  97                 }
  98                 chk.checkNonCyclicMembership(tree);
  99             }
 100             super.visitClassDef(tree);
 101         } finally {
 102             inValue = oldInValue;
 103         }
 104     }
 105 




  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.javac.comp;
  27 
  28 import com.sun.tools.javac.code.*;
  29 
  30 import static com.sun.tools.javac.code.Flags.asFlagSet;
  31 import static com.sun.tools.javac.code.Kinds.Kind.*;
  32 import com.sun.tools.javac.code.Lint.LintCategory;
  33 import com.sun.tools.javac.code.Symbol.MethodSymbol;
  34 import com.sun.tools.javac.code.Symbol.OperatorSymbol;
  35 import com.sun.tools.javac.code.Symbol.TypeSymbol;
  36 import com.sun.tools.javac.jvm.ByteCodes;
  37 import com.sun.tools.javac.tree.*;
  38 import com.sun.tools.javac.tree.JCTree.*;
  39 import com.sun.tools.javac.util.*;
  40 
  41 /**
  42  * Support for "Minimal Value Types" : Process classes annotated with @ValueCapableClass -
  43  * All semantic checks are centralized in this place so that we can blow them all away when
  44  * moving to "Maximal Value Types".
  45  *
  46  * see: http://cr.openjdk.java.net/~jrose/values/shady-values.html
  47  *
  48  */
  49 
  50 public class ValueCapableClassAttr extends TreeTranslator {
  51 
  52     protected static final Context.Key<ValueCapableClassAttr> valueCapableClassAttr = new Context.Key<>();
  53     private JCMethodDecl currentMethod;
  54     private Log log;
  55     private Names names;
  56     private Symtab syms;
  57     private final JCDiagnostic.Factory diags;
  58     private final Types types;
  59     private final Check chk;
  60     private boolean inValue = false;
  61 
  62     public static ValueCapableClassAttr instance(Context context) {


  65             instance = new ValueCapableClassAttr(context);
  66         return instance;
  67     }
  68 
  69     protected ValueCapableClassAttr(Context context) {
  70         context.put(valueCapableClassAttr, this);
  71         log = Log.instance(context);
  72         names = Names.instance(context);
  73         syms = Symtab.instance(context);
  74         diags = JCDiagnostic.Factory.instance(context);
  75         types = Types.instance(context);
  76         chk = Check.instance(context);
  77     }
  78 
  79     public void visitClassDef(JCClassDecl tree) {
  80         boolean oldInValue = inValue;
  81         try {
  82             inValue = false;
  83             for (List<JCAnnotation> al = tree.mods.annotations; !al.isEmpty(); al = al.tail) {
  84                 JCAnnotation a = al.head;
  85                 if (a.annotationType.type == syms.valueCapableClass && a.args.isEmpty()) {
  86                     inValue = true;
  87                     tree.sym.flags_field |= Flags.VALUE_CAPABLE;
  88                     break;
  89                 }
  90             }
  91             if (inValue) {
  92                 if (tree.extending != null) {
  93                     log.warning(LintCategory.VALUES, tree.pos(), "value.may.not.extend");
  94                 }
  95                 if ((tree.mods.flags & Flags.FINAL) == 0) {
  96                     log.warning(LintCategory.VALUES, tree.pos(), "value.must.be.final");
  97                 }
  98                 chk.checkNonCyclicMembership(tree);
  99             }
 100             super.visitClassDef(tree);
 101         } finally {
 102             inValue = oldInValue;
 103         }
 104     }
 105