1 /* 2 * Copyright 2013 Google LLC 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 com.google.auto.factory.processor; 17 18 import static com.google.common.base.Preconditions.checkArgument; 19 import static com.google.common.base.Preconditions.checkNotNull; 20 import static javax.lang.model.element.ElementKind.CLASS; 21 import static javax.lang.model.element.ElementKind.PACKAGE; 22 import static javax.lang.model.element.Modifier.FINAL; 23 import static javax.lang.model.element.Modifier.STATIC; 24 25 import com.google.auto.common.MoreTypes; 26 import com.google.common.collect.ImmutableSet; 27 import javax.lang.model.element.ExecutableElement; 28 import javax.lang.model.element.TypeElement; 29 import javax.lang.model.type.ExecutableType; 30 import javax.lang.model.type.TypeKind; 31 import javax.lang.model.type.TypeMirror; 32 import javax.lang.model.util.ElementFilter; 33 import javax.lang.model.util.Types; 34 35 final class Elements2 { Elements2()36 private Elements2() {} 37 getConstructors(TypeElement type)38 static ImmutableSet<ExecutableElement> getConstructors(TypeElement type) { 39 checkNotNull(type); 40 checkArgument(type.getKind() == CLASS); 41 return ImmutableSet.copyOf(ElementFilter.constructorsIn(type.getEnclosedElements())); 42 } 43 isValidSupertypeForClass(TypeElement type)44 static boolean isValidSupertypeForClass(TypeElement type) { 45 if (!type.getKind().equals(CLASS)) { 46 return false; 47 } 48 if (type.getModifiers().contains(FINAL)) { 49 return false; 50 } 51 if (!type.getEnclosingElement().getKind().equals(PACKAGE) 52 && !type.getModifiers().contains(STATIC)) { 53 return false; 54 } 55 if (type.getSimpleName().length() == 0) { 56 return false; 57 } 58 return true; 59 } 60 61 /** 62 * Given an executable element in a supertype, returns its ExecutableType when it is viewed as a 63 * member of a subtype. 64 */ getExecutableElementAsMemberOf( Types types, ExecutableElement executableElement, TypeElement subTypeElement)65 static ExecutableType getExecutableElementAsMemberOf( 66 Types types, ExecutableElement executableElement, TypeElement subTypeElement) { 67 checkNotNull(types); 68 checkNotNull(executableElement); 69 checkNotNull(subTypeElement); 70 TypeMirror subTypeMirror = subTypeElement.asType(); 71 if (!subTypeMirror.getKind().equals(TypeKind.DECLARED)) { 72 throw new IllegalStateException( 73 "Expected subTypeElement.asType() to return a class/interface type."); 74 } 75 TypeMirror subExecutableTypeMirror = 76 types.asMemberOf(MoreTypes.asDeclared(subTypeMirror), executableElement); 77 if (!subExecutableTypeMirror.getKind().equals(TypeKind.EXECUTABLE)) { 78 throw new IllegalStateException("Expected subExecutableTypeMirror to be an executable type."); 79 } 80 return MoreTypes.asExecutable(subExecutableTypeMirror); 81 } 82 } 83