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 SuperObjectValue implements ObjectValue { 22 private static final String SUPER_VALUE = "super"; 23 24 @Override type()25 public abstract TypeNode type(); 26 27 @Override value()28 public String value() { 29 return SUPER_VALUE; 30 } 31 32 // Note: This is the type of "super" object, and not the child type of the supertype. withType(TypeNode type)33 public static SuperObjectValue withType(TypeNode type) { 34 return builder().setType(type).build(); 35 } 36 builder()37 private static Builder builder() { 38 return new AutoValue_SuperObjectValue.Builder(); 39 } 40 41 @AutoValue.Builder 42 abstract static class Builder { setType(TypeNode type)43 abstract Builder setType(TypeNode type); 44 autoBuild()45 abstract SuperObjectValue autoBuild(); 46 build()47 private SuperObjectValue build() { 48 SuperObjectValue superObjectValue = autoBuild(); 49 Preconditions.checkState( 50 TypeNode.isReferenceType(superObjectValue.type()), 51 "The \"super\" can only refer to object types"); 52 return superObjectValue; 53 } 54 } 55 } 56