blob: 7a5694445d9bb0f894a956625a24c940dd4cf9b4 [file] [log] [blame]
Jesse Wilson48901392012-07-02 11:50:40 -04001/*
ronshapiro5dde42d2016-06-17 09:03:35 -07002 * Copyright (C) 2013 The Dagger Authors.
Jesse Wilson5ad73c02012-06-28 09:56:55 -07003 *
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 */
dpb1b65b6a2016-07-11 12:11:24 -070016
Jesse Wilson27ce1482012-09-17 17:27:34 -040017package dagger.internal.codegen;
Jesse Wilson5ad73c02012-06-28 09:56:55 -070018
gake55f0742016-07-12 15:36:42 -070019import static javax.lang.model.element.ElementKind.CONSTRUCTOR;
20import static javax.lang.model.element.Modifier.ABSTRACT;
21import static javax.lang.model.element.Modifier.PRIVATE;
22import static javax.lang.model.element.Modifier.STATIC;
23
ronshapiroac6dd2b2017-08-04 12:48:07 -070024import java.util.Map;
25import java.util.function.Function;
gak78ac3682015-03-30 15:48:00 -070026import javax.lang.model.element.Element;
Jesse Wilsonb5d5fc12012-07-17 19:21:36 -040027import javax.lang.model.element.ExecutableElement;
Jesse Wilsondcc81902012-07-02 14:38:48 -040028import javax.lang.model.element.TypeElement;
cgruber4136f4d2014-10-28 10:59:26 -070029
Jesse Wilson5ad73c02012-06-28 09:56:55 -070030/**
Christian Edward Gruber768c7312013-06-05 12:05:52 -070031 * Utilities for handling types in annotation processors
Jesse Wilson5ad73c02012-06-28 09:56:55 -070032 */
Steven Goldfeder7d0bb652013-07-24 02:12:34 -070033final class Util {
gake1b68a22016-04-07 11:54:06 -070034 /**
gak78ac3682015-03-30 15:48:00 -070035 * Returns true if and only if a component can instantiate new instances (typically of a module)
36 * rather than requiring that they be passed.
37 */
38 static boolean componentCanMakeNewInstances(TypeElement typeElement) {
39 switch (typeElement.getKind()) {
40 case CLASS:
41 break;
42 case ENUM:
43 case ANNOTATION_TYPE:
44 case INTERFACE:
45 return false;
46 default:
47 throw new AssertionError("TypeElement cannot have kind: " + typeElement.getKind());
48 }
49
50 if (typeElement.getModifiers().contains(ABSTRACT)) {
51 return false;
52 }
53
54 if (requiresEnclosingInstance(typeElement)) {
55 return false;
56 }
57
58 for (Element enclosed : typeElement.getEnclosedElements()) {
59 if (enclosed.getKind().equals(CONSTRUCTOR)
gak5721a932015-07-16 11:40:57 -070060 && ((ExecutableElement) enclosed).getParameters().isEmpty()
61 && !enclosed.getModifiers().contains(PRIVATE)) {
gak78ac3682015-03-30 15:48:00 -070062 return true;
63 }
64 }
65
66 // TODO(gak): still need checks for visibility
67
68 return false;
69 }
70
dpbf8c5c7f2015-11-19 08:16:43 -080071 private static boolean requiresEnclosingInstance(TypeElement typeElement) {
72 switch (typeElement.getNestingKind()) {
73 case TOP_LEVEL:
74 return false;
75 case MEMBER:
76 return !typeElement.getModifiers().contains(STATIC);
77 case ANONYMOUS:
78 case LOCAL:
79 return true;
80 default:
81 throw new AssertionError(
82 "TypeElement cannot have nesting kind: " + typeElement.getNestingKind());
83 }
84 }
85
dpb141148b2016-10-20 12:31:34 -070086 /**
ronshapiroac6dd2b2017-08-04 12:48:07 -070087 * A version of {@link Map#computeIfAbsent(Object, Function)} that allows {@code mappingFunction}
88 * to update {@code map}.
89 */
90 static <K, V> V reentrantComputeIfAbsent(
91 Map<K, V> map, K key, Function<? super K, ? extends V> mappingFunction) {
92 V value = map.get(key);
93 if (value == null) {
94 value = mappingFunction.apply(key);
95 if (value != null) {
96 map.put(key, value);
97 }
98 }
99 return value;
100 }
101
cgruber5a047e42014-09-04 22:57:10 -0700102 private Util() {}
Jesse Wilson5ad73c02012-06-28 09:56:55 -0700103}