| Jesse Wilson | 4890139 | 2012-07-02 11:50:40 -0400 | [diff] [blame] | 1 | /* |
| ronshapiro | 5dde42d | 2016-06-17 09:03:35 -0700 | [diff] [blame] | 2 | * Copyright (C) 2013 The Dagger Authors. |
| Jesse Wilson | 5ad73c0 | 2012-06-28 09:56:55 -0700 | [diff] [blame] | 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 | */ |
| dpb | 1b65b6a | 2016-07-11 12:11:24 -0700 | [diff] [blame] | 16 | |
| Jesse Wilson | 27ce148 | 2012-09-17 17:27:34 -0400 | [diff] [blame] | 17 | package dagger.internal.codegen; |
| Jesse Wilson | 5ad73c0 | 2012-06-28 09:56:55 -0700 | [diff] [blame] | 18 | |
| gak | e55f074 | 2016-07-12 15:36:42 -0700 | [diff] [blame] | 19 | import static javax.lang.model.element.ElementKind.CONSTRUCTOR; |
| 20 | import static javax.lang.model.element.Modifier.ABSTRACT; |
| 21 | import static javax.lang.model.element.Modifier.PRIVATE; |
| 22 | import static javax.lang.model.element.Modifier.STATIC; |
| 23 | |
| ronshapiro | ac6dd2b | 2017-08-04 12:48:07 -0700 | [diff] [blame] | 24 | import java.util.Map; |
| 25 | import java.util.function.Function; |
| gak | 78ac368 | 2015-03-30 15:48:00 -0700 | [diff] [blame] | 26 | import javax.lang.model.element.Element; |
| Jesse Wilson | b5d5fc1 | 2012-07-17 19:21:36 -0400 | [diff] [blame] | 27 | import javax.lang.model.element.ExecutableElement; |
| Jesse Wilson | dcc8190 | 2012-07-02 14:38:48 -0400 | [diff] [blame] | 28 | import javax.lang.model.element.TypeElement; |
| cgruber | 4136f4d | 2014-10-28 10:59:26 -0700 | [diff] [blame] | 29 | |
| Jesse Wilson | 5ad73c0 | 2012-06-28 09:56:55 -0700 | [diff] [blame] | 30 | /** |
| Christian Edward Gruber | 768c731 | 2013-06-05 12:05:52 -0700 | [diff] [blame] | 31 | * Utilities for handling types in annotation processors |
| Jesse Wilson | 5ad73c0 | 2012-06-28 09:56:55 -0700 | [diff] [blame] | 32 | */ |
| Steven Goldfeder | 7d0bb65 | 2013-07-24 02:12:34 -0700 | [diff] [blame] | 33 | final class Util { |
| gak | e1b68a2 | 2016-04-07 11:54:06 -0700 | [diff] [blame] | 34 | /** |
| gak | 78ac368 | 2015-03-30 15:48:00 -0700 | [diff] [blame] | 35 | * 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) |
| gak | 5721a93 | 2015-07-16 11:40:57 -0700 | [diff] [blame] | 60 | && ((ExecutableElement) enclosed).getParameters().isEmpty() |
| 61 | && !enclosed.getModifiers().contains(PRIVATE)) { |
| gak | 78ac368 | 2015-03-30 15:48:00 -0700 | [diff] [blame] | 62 | return true; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // TODO(gak): still need checks for visibility |
| 67 | |
| 68 | return false; |
| 69 | } |
| 70 | |
| dpb | f8c5c7f | 2015-11-19 08:16:43 -0800 | [diff] [blame] | 71 | 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 | |
| dpb | 141148b | 2016-10-20 12:31:34 -0700 | [diff] [blame] | 86 | /** |
| ronshapiro | ac6dd2b | 2017-08-04 12:48:07 -0700 | [diff] [blame] | 87 | * 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 | |
| cgruber | 5a047e4 | 2014-09-04 22:57:10 -0700 | [diff] [blame] | 102 | private Util() {} |
| Jesse Wilson | 5ad73c0 | 2012-06-28 09:56:55 -0700 | [diff] [blame] | 103 | } |