Home | History | Annotate | Download | only in type
      1 /*
      2  * Copyright 2016 Google Inc. All Rights Reserved.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.turbine.type;
     18 
     19 import com.google.common.collect.ImmutableList;
     20 import com.google.common.collect.ImmutableMap;
     21 import com.google.turbine.binder.sym.ClassSymbol;
     22 import com.google.turbine.diag.SourceFile;
     23 import com.google.turbine.model.Const;
     24 import com.google.turbine.tree.Tree;
     25 import com.google.turbine.tree.Tree.Anno;
     26 import com.google.turbine.tree.Tree.Expression;
     27 import java.util.Objects;
     28 
     29 /** An annotation use. */
     30 public class AnnoInfo {
     31   private final SourceFile source;
     32   private final ClassSymbol sym;
     33   private final Tree.Anno tree;
     34   private final ImmutableMap<String, Const> values;
     35 
     36   public AnnoInfo(
     37       SourceFile source, ClassSymbol sym, Anno tree, ImmutableMap<String, Const> values) {
     38     this.source = source;
     39     this.sym = sym;
     40     this.tree = tree;
     41     this.values = values;
     42   }
     43 
     44   /** The annotation's source, for diagnostics. */
     45   public SourceFile source() {
     46     return source;
     47   }
     48 
     49   /** The annotation's diagnostic position. */
     50   public int position() {
     51     return tree.position();
     52   }
     53 
     54   /** Arguments, either assignments or a single expression. */
     55   public ImmutableList<Expression> args() {
     56     return tree.args();
     57   }
     58 
     59   /** Bound element-value pairs. */
     60   public ImmutableMap<String, Const> values() {
     61     return values;
     62   }
     63 
     64   /** The annotation's declaration. */
     65   public ClassSymbol sym() {
     66     return sym;
     67   }
     68 
     69   public AnnoInfo withValues(ImmutableMap<String, Const> values) {
     70     return new AnnoInfo(source, sym, tree, values);
     71   }
     72 
     73   @Override
     74   public int hashCode() {
     75     return Objects.hash(sym, values);
     76   }
     77 
     78   @Override
     79   public boolean equals(Object obj) {
     80     if (!(obj instanceof AnnoInfo)) {
     81       return false;
     82     }
     83     AnnoInfo that = (AnnoInfo) obj;
     84     return sym.equals(that.sym) && values.equals(that.values);
     85   }
     86 }
     87