public AnnotationMirror getAnnotationMirror() { List<? extends AnnotationMirror> annotationMirrors = element.getAnnotationMirrors(); for (AnnotationMirror annotationMirror : annotationMirrors) { TypeElement annotationElement = (TypeElement) annotationMirror.getAnnotationType().asElement(); if (annotationElement.getQualifiedName().toString().equals(annotationName)) { return annotationMirror; } } return null; }
private void collectBuiltins(Map<String, TypeMirror> collected) { for (TypeKind kind : TypeKind.values()) { if (kind.isPrimitive()) { TypeElement boxedClass = types.boxedClass(types.getPrimitiveType(kind)); collected.put(boxedClass.getSimpleName().toString(), boxedClass.asType()); } } TypeElement typeElement = elements.getTypeElement(String.class.getCanonicalName()); collected.put(typeElement.getSimpleName().toString(), typeElement.asType()); typeElement = elements.getTypeElement(Templates.Invokable.class.getCanonicalName()); collected.put(typeElement.getSimpleName().toString(), typeElement.asType()); }
private TypeMirror createStringTypeMirror() { TypeElement element = typeElement; while (element.getSuperclass().getKind() != TypeKind.NONE) { logger.finest("finding toString in " + element); element = (TypeElement) ((DeclaredType) element.getSuperclass()).asElement(); } for (Element enclosed : typeElement.getEnclosedElements()) { if (enclosed.getKind() == ElementKind.METHOD) { ExecutableElement method = (ExecutableElement) enclosed; if (method.getSimpleName().contentEquals("toString")) { return method.getReturnType(); } } } throw new IllegalStateException("Cannot find toString method in Object"); } private static boolean find(String interfaceName, TypeElement typeElement) {
private boolean isAbstractClass(Element annotatedElement) { if (annotatedElement instanceof TypeElement) { TypeElement typeElement = (TypeElement) annotatedElement; return typeElement.getKind() == ElementKind.CLASS && typeElement.getModifiers().contains(Modifier.ABSTRACT); } else { return false; } }
private TypeElement getSuperclass(TypeElement type) { if (type.getSuperclass().getKind() == TypeKind.DECLARED) { TypeElement superclass = (TypeElement) processingEnv.getTypeUtils().asElement(type.getSuperclass()); String name = superclass.getQualifiedName().toString(); if (name.startsWith("java.") || name.startsWith("javax.") || name.startsWith("android.")) { // Skip system classes, this just degrades performance return null; } else { return superclass; } } else { return null; } }
private GraphAnalysisInjectBinding(String provideKey, String membersKey, TypeElement type, List<String> keys, String supertypeKey) { super(provideKey, membersKey, type.getAnnotation(Singleton.class) != null, type.getQualifiedName().toString()); this.type = type; this.keys = keys; this.bindings = new Binding<?>[keys.size()]; this.supertypeKey = supertypeKey; }
private void parseResourceInt(Element element, Map<TypeElement, BindingSet.Builder> builderMap, Set<TypeElement> erasedTargetNames) { boolean hasError = false; TypeElement enclosingElement = (TypeElement) element.getEnclosingElement(); // Verify that the target type is int. if (element.asType().getKind() != TypeKind.INT) { error(element, "@%s field type must be 'int'. (%s.%s)", BindInt.class.getSimpleName(), enclosingElement.getQualifiedName(), element.getSimpleName()); hasError = true; } // Verify common generated code restrictions. hasError |= isInaccessibleViaGeneratedCode(BindInt.class, "fields", element); hasError |= isBindingInWrongPackage(BindInt.class, element); if (hasError) { return; } // Assemble information on the field. String name = element.getSimpleName().toString(); int id = element.getAnnotation(BindInt.class).value(); Id resourceId = elementToId(element, BindInt.class, id); BindingSet.Builder builder = getOrCreateBindingBuilder(builderMap, enclosingElement); builder.addResource( new FieldResourceBinding(resourceId, name, FieldResourceBinding.Type.INT)); erasedTargetNames.add(enclosingElement); }
private void parseResourceAnimation(Element element, Map<TypeElement, BindingSet.Builder> builderMap, Set<TypeElement> erasedTargetNames) { boolean hasError = false; TypeElement enclosingElement = (TypeElement) element.getEnclosingElement(); // Verify that the target type is Animation. if (!ANIMATION_TYPE.equals(element.asType().toString())) { error(element, "@%s field type must be 'Animation'. (%s.%s)", BindAnim.class.getSimpleName(), enclosingElement.getQualifiedName(), element.getSimpleName()); hasError = true; } // Verify common generated code restrictions. hasError |= isInaccessibleViaGeneratedCode(BindAnim.class, "fields", element); hasError |= isBindingInWrongPackage(BindAnim.class, element); if (hasError) { return; } // Assemble information on the field. String name = element.getSimpleName().toString(); int id = element.getAnnotation(BindAnim.class).value(); Id resourceId = elementToId(element, BindAnim.class, id); BindingSet.Builder builder = getOrCreateBindingBuilder(builderMap, enclosingElement); builder.addResource(new FieldAnimationBinding(resourceId, name)); erasedTargetNames.add(enclosingElement); }
public boolean isCallbackMethodAnnotationValid(Element element, String annotationName) { TypeElement enclosingElement = (TypeElement)element.getEnclosingElement(); if (enclosingElement.getAnnotation(JsonObject.class) == null) { error(enclosingElement, "%s: @%s methods can only be in classes annotated with @%s.", enclosingElement.getQualifiedName(), annotationName, JsonObject.class.getSimpleName()); return false; } ExecutableElement executableElement = (ExecutableElement)element; if (executableElement.getParameters().size() > 0) { error(element, "%s: @%s methods must not take any parameters.", enclosingElement.getQualifiedName(), annotationName); return false; } List<? extends Element> allElements = enclosingElement.getEnclosedElements(); int methodInstances = 0; for (Element enclosedElement : allElements) { for (AnnotationMirror am : enclosedElement.getAnnotationMirrors()) { if (am.getAnnotationType().asElement().getSimpleName().toString().equals(annotationName)) { methodInstances++; } } } if (methodInstances != 1) { error(element, "%s: There can only be one @%s method per class.", enclosingElement.getQualifiedName(), annotationName); return false; } return true; }
private Stream<CompilationMessage> validateReturnType( ExecutableElement method ) { String streamClassName = Stream.class.getCanonicalName(); TypeMirror streamType = typeUtils.erasure( elementUtils.getTypeElement( streamClassName ).asType() ); TypeMirror returnType = method.getReturnType(); TypeMirror erasedReturnType = typeUtils.erasure( returnType ); TypeMirror voidType = typeUtils.getNoType( TypeKind.VOID ); if ( typeUtils.isSameType( returnType, voidType ) ) { return Stream.empty(); } if ( !typeUtils.isSubtype( erasedReturnType, streamType ) ) { return Stream.of( new ReturnTypeError( method, "Return type of %s#%s must be %s", method.getEnclosingElement().getSimpleName(), method.getSimpleName(), streamClassName ) ); } return recordVisitor.visit( returnType ); }
/** * Retrieve the string value inside an @Contract annotation without statically depending on the * type. * * @param sym A method which has an @Contract annotation. * @return The string value spec inside the annotation. */ private static @Nullable String getContractFromAnnotation(Symbol.MethodSymbol sym) { for (AnnotationMirror annotation : sym.getAnnotationMirrors()) { Element element = annotation.getAnnotationType().asElement(); assert element.getKind().equals(ElementKind.ANNOTATION_TYPE); if (((TypeElement) element) .getQualifiedName() .contentEquals("org.jetbrains.annotations.Contract")) { for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> e : annotation.getElementValues().entrySet()) { if (e.getKey().getSimpleName().contentEquals("value")) { String value = e.getValue().toString(); if (value.startsWith("\"") && value.endsWith("\"")) { value = value.substring(1, value.length() - 1); } return value; } } } } return null; } }
private void writeCreateSubscriberMethods(BufferedWriter writer, List<ExecutableElement> methods, String callPrefix, String myPackage) throws IOException { for (ExecutableElement method : methods) { List<? extends VariableElement> parameters = method.getParameters(); TypeMirror paramType = getParamTypeMirror(parameters.get(0), null); TypeElement paramElement = (TypeElement) processingEnv.getTypeUtils().asElement(paramType); String methodName = method.getSimpleName().toString(); String eventClass = getClassString(paramElement, myPackage) + ".class"; Subscribe subscribe = method.getAnnotation(Subscribe.class); List<String> parts = new ArrayList<>(); parts.add(callPrefix + "(\"" + methodName + "\","); method.getEnclosingElement().getSimpleName() + "." + methodName + "(" + paramElement.getSimpleName() + ")");
private void setClosestRequiredAncestor( ExtraBindingTarget extraBindingTarget, TypeElement superIntentBuilder) { for (ExecutableElement method : methodsIn(superIntentBuilder.getEnclosedElements())) { if (method.getSimpleName().contentEquals(NEXT_STATE_METHOD)) { final TypeMirror returnTypeMirror = method.getReturnType(); if (compilerUtil.isAssignable(returnTypeMirror, "dart.henson.AllRequiredSetState")) { return; } final Element reqElement = ((DeclaredType) typeUtils.erasure(returnTypeMirror)).asElement(); final TypeElement intentBuilderTypeElement = (TypeElement) reqElement.getEnclosingElement(); extraBindingTarget.closestRequiredAncestorPackage = compilerUtil.getPackageName(intentBuilderTypeElement); final String intentBuilderClass = compilerUtil.getClassName( intentBuilderTypeElement, extraBindingTarget.closestRequiredAncestorPackage); extraBindingTarget.closestRequiredAncestorClass = intentBuilderClass.substring(0, intentBuilderClass.indexOf(BUNDLE_BUILDER_SUFFIX)); } } }
static boolean isKotlinSingleton(TypeElement element) { return element.getKind() == ElementKind.CLASS && element .getEnclosedElements() .stream() .anyMatch( e -> { final CharSequence instanceFieldName = "INSTANCE"; return e.getSimpleName().contentEquals(instanceFieldName) && e.asType().toString().equals(((Symbol.ClassSymbol) element).className()) && e.getModifiers() .containsAll( ImmutableList.of(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)); }); }
public void validateRestSimpleParameter(Element element, String requiredClass, String parameterName, ElementValidation validation) { TypeMirror requiredType = annotationHelper.typeElementFromQualifiedName(requiredClass).asType(); DeclaredType paramterType = annotationHelper.extractAnnotationClassParameter(element, annotationHelper.getTarget(), parameterName); if (paramterType != null) { if (annotationHelper.isSubtype(paramterType, requiredType)) { Element parameterElement = paramterType.asElement(); if (parameterElement.getKind().isClass()) { if (!annotationHelper.isAbstract(parameterElement)) { if (parameterElement.getAnnotation(EBean.class) != null) { typeIsValid(EBean.class, paramterType, validation); return; } List<ExecutableElement> constructors = ElementFilter.constructorsIn(parameterElement.getEnclosedElements()); for (ExecutableElement constructor : constructors) { if (annotationHelper.isPublic(constructor) && constructor.getParameters().isEmpty()) { return; } } validation.addError(element, "The " + parameterName + " class must have a public no argument constructor or must be annotated with @EBean"); } else { validation.addError(element, "The " + parameterName + " class must not be abstract"); } } else { validation.addError(element, "The " + parameterName + " class must be a class"); } } else { validation.addError(element, "The " + parameterName + " class must be a subtype of " + requiredClass); } } }
private boolean isInaccessibleViaGeneratedCode(Class<? extends Annotation> annotationClass, String targetThing, Element element) { boolean hasError = false; TypeElement enclosingElement = (TypeElement) element.getEnclosingElement(); // Verify field or method modifiers. Set<Modifier> modifiers = element.getModifiers(); if (modifiers.contains(PRIVATE) || modifiers.contains(STATIC)) { error(element, "@%s %s must not be private or static. (%s.%s)", annotationClass.getSimpleName(), targetThing, enclosingElement.getQualifiedName(), element.getSimpleName()); hasError = true; } // Verify containing type. if (enclosingElement.getKind() != CLASS) { error(enclosingElement, "@%s %s may only be contained in classes. (%s.%s)", annotationClass.getSimpleName(), targetThing, enclosingElement.getQualifiedName(), element.getSimpleName()); hasError = true; } // Verify containing class visibility is not private. if (enclosingElement.getModifiers().contains(PRIVATE)) { error(enclosingElement, "@%s %s may not be contained in private classes. (%s.%s)", annotationClass.getSimpleName(), targetThing, enclosingElement.getQualifiedName(), element.getSimpleName()); hasError = true; } return hasError; }
private boolean isMethodInBaseRequestOptions(ExecutableElement toFind) { // toFind is a method in a GlideExtension whose first argument is a BaseRequestOptions<?> type. // Since we're comparing against methods in BaseRequestOptions itself, we need to drop that // first type. TypeElement requestOptionsType = processingEnvironment .getElementUtils() .getTypeElement(RequestOptionsGenerator.BASE_REQUEST_OPTIONS_QUALIFIED_NAME); List<String> toFindParameterNames = getComparableParameterNames(toFind, true /*skipFirst*/); String toFindSimpleName = toFind.getSimpleName().toString(); for (Element element : requestOptionsType.getEnclosedElements()) { if (element.getKind() != ElementKind.METHOD) { continue; } ExecutableElement inBase = (ExecutableElement) element; if (toFindSimpleName.equals(inBase.getSimpleName().toString())) { List<String> parameterNamesInBase = getComparableParameterNames(inBase, false /*skipFirst*/); if (parameterNamesInBase.equals(toFindParameterNames)) { return true; } } } return false; }
private void writeAsyncClass(Element elem) throws ClassNotFoundException, IOException, Exception { if (elem.getKind().isInterface()) { TypeElement interfaceClazz = (TypeElement) elem; String className = interfaceClazz.getSimpleName().toString(); TypeSpec.Builder classBuilder = TypeSpec.interfaceBuilder(className + ASYNC).addModifiers(Modifier.PUBLIC) .addSuperinterface(TypeName.get(elem.asType())); // add class generic type classBuilder.addTypeVariables(getTypeNames(interfaceClazz.getTypeParameters())); // add direct method addMethods(interfaceClazz, classBuilder); // add method form superinterface addSuperInterfaceMethods(interfaceClazz.getInterfaces(), classBuilder); // write class JavaFile javaFile = JavaFile.builder(processingEnv.getElementUtils().getPackageOf(interfaceClazz).getQualifiedName().toString(), classBuilder.build()).build(); javaFile.writeTo(new File(System.getProperty("basedir"), TARGET_DIR)); } else { processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "MotanAsyncProcessor not process, because " + elem.toString() + " not a interface."); } }
static String getClassFQName(TypeElement elem) { StringBuilder name = new StringBuilder(); while (isClassy(elem.getEnclosingElement().getKind())) { name.insert(0, "$" + elem.getSimpleName()); elem = (TypeElement) elem.getEnclosingElement(); } name.insert(0, elem.getQualifiedName()); return name.toString(); }
private static String findMappedSuperClass(MetaEntity entity, Context context) { TypeMirror superClass = entity.getTypeElement().getSuperclass(); //superclass of Object is of NoType which returns some other kind while ( superClass.getKind() == TypeKind.DECLARED ) { //F..king Ch...t Have those people used their horrible APIs even once? final Element superClassElement = ( (DeclaredType) superClass ).asElement(); String superClassName = ( (TypeElement) superClassElement ).getQualifiedName().toString(); if ( extendsSuperMetaModel( superClassElement, entity.isMetaComplete(), context ) ) { return superClassName; } superClass = ( (TypeElement) superClassElement ).getSuperclass(); } return null; }