1 /* 2 * Copyright (C) 2015 The Dagger Authors. 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 dagger.internal.codegen.writing; 18 19 import static com.google.common.base.Preconditions.checkNotNull; 20 21 import com.squareup.javapoet.ClassName; 22 import com.squareup.javapoet.CodeBlock; 23 import dagger.internal.codegen.writing.ComponentImplementation.ShardImplementation; 24 25 /** 26 * Represents a {@link com.sun.source.tree.MemberSelectTree} as a {@link CodeBlock}. 27 */ 28 abstract class MemberSelect { 29 30 /** 31 * Returns a {@link MemberSelect} that accesses the field given by {@code fieldName} owned by 32 * {@code owningClass}. In this context "local" refers to the fact that the field is owned by the 33 * type (or an enclosing type) from which the code block will be used. The returned {@link 34 * MemberSelect} will not be valid for accessing the field from a different class (regardless of 35 * accessibility). 36 */ localField(ShardImplementation owningShard, String fieldName)37 static MemberSelect localField(ShardImplementation owningShard, String fieldName) { 38 return new LocalField(owningShard, fieldName); 39 } 40 41 private static final class LocalField extends MemberSelect { 42 final ShardImplementation owningShard; 43 final String fieldName; 44 LocalField(ShardImplementation owningShard, String fieldName)45 LocalField(ShardImplementation owningShard, String fieldName) { 46 super(owningShard.name(), false); 47 this.owningShard = owningShard; 48 this.fieldName = checkNotNull(fieldName); 49 } 50 51 @Override getExpressionFor(ClassName usingClass)52 CodeBlock getExpressionFor(ClassName usingClass) { 53 return owningClass().equals(usingClass) 54 ? CodeBlock.of("$N", fieldName) 55 : CodeBlock.of("$L.$N", owningShard.shardFieldReference(), fieldName); 56 } 57 } 58 59 private final ClassName owningClass; 60 private final boolean staticMember; 61 MemberSelect(ClassName owningClass, boolean staticMemeber)62 MemberSelect(ClassName owningClass, boolean staticMemeber) { 63 this.owningClass = owningClass; 64 this.staticMember = staticMemeber; 65 } 66 67 /** Returns the class that owns the member being selected. */ owningClass()68 ClassName owningClass() { 69 return owningClass; 70 } 71 72 /** 73 * Returns true if the member being selected is static and does not require an instance of 74 * {@link #owningClass()}. 75 */ staticMember()76 boolean staticMember() { 77 return staticMember; 78 } 79 80 /** 81 * Returns a {@link CodeBlock} suitable for accessing the member from the given {@code 82 * usingClass}. 83 */ getExpressionFor(ClassName usingClass)84 abstract CodeBlock getExpressionFor(ClassName usingClass); 85 } 86