1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.google.api.generator.engine.ast;
16 
17 import com.google.auto.value.AutoValue;
18 import com.google.common.base.Preconditions;
19 
20 @AutoValue
21 public abstract class InstanceofExpr implements Expr {
expr()22   public abstract Expr expr();
23 
checkType()24   public abstract TypeNode checkType();
25 
26   @Override
type()27   public TypeNode type() {
28     return TypeNode.BOOLEAN;
29   }
30 
31   @Override
accept(AstNodeVisitor visitor)32   public void accept(AstNodeVisitor visitor) {
33     visitor.visit(this);
34   }
35 
builder()36   public static Builder builder() {
37     return new AutoValue_InstanceofExpr.Builder();
38   }
39 
40   @AutoValue.Builder
41   public abstract static class Builder {
setExpr(Expr expr)42     public abstract Builder setExpr(Expr expr);
43 
setCheckType(TypeNode checkType)44     public abstract Builder setCheckType(TypeNode checkType);
45 
autoBuild()46     abstract InstanceofExpr autoBuild();
47 
build()48     public InstanceofExpr build() {
49       InstanceofExpr instanceofExpr = autoBuild();
50       Preconditions.checkState(
51           TypeNode.isReferenceType(instanceofExpr.checkType()),
52           "Instanceof can only check reference types");
53       return instanceofExpr;
54     }
55   }
56 }
57