• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 Google, Inc.
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 package dagger.internal.codegen.writer;
17 
18 import com.google.common.base.Optional;
19 import com.google.common.collect.ImmutableSet;
20 import java.io.IOException;
21 import java.util.Set;
22 import javax.lang.model.type.WildcardType;
23 
24 import static dagger.internal.codegen.writer.TypeNames.FOR_TYPE_MIRROR;
25 
26 public final class WildcardName implements TypeName {
27   private final Optional<TypeName> extendsBound;
28   private final Optional<TypeName> superBound;
29 
WildcardName(Optional<TypeName> extendsBound, Optional<TypeName> superBound)30   WildcardName(Optional<TypeName> extendsBound,
31       Optional<TypeName> superBound) {
32     this.extendsBound = extendsBound;
33     this.superBound = superBound;
34   }
35 
forTypeMirror(WildcardType mirror)36   static WildcardName forTypeMirror(WildcardType mirror) {
37     return new WildcardName(
38         Optional.fromNullable(mirror.getExtendsBound()).transform(FOR_TYPE_MIRROR),
39         Optional.fromNullable(mirror.getSuperBound()).transform(FOR_TYPE_MIRROR));
40   }
41 
42   @Override
referencedClasses()43   public Set<ClassName> referencedClasses() {
44     ImmutableSet.Builder<ClassName> builder = new ImmutableSet.Builder<ClassName>();
45     if (extendsBound.isPresent()) {
46       builder.addAll(extendsBound.get().referencedClasses());
47     }
48     if (superBound.isPresent()) {
49       builder.addAll(superBound.get().referencedClasses());
50     }
51     return builder.build();
52   }
53 
54   @Override
write(Appendable appendable, Context context)55   public Appendable write(Appendable appendable, Context context) throws IOException {
56     appendable.append('?');
57     if (extendsBound.isPresent()) {
58       appendable.append(" extends ");
59       extendsBound.get().write(appendable, context);
60     }
61     if (superBound.isPresent()) {
62       appendable.append(" super ");
63       superBound.get().write(appendable, context);
64     }
65     return appendable;
66   }
67 }
68