Refine search
/** * Return true if all element types are currently available in this code * generation pass. Unavailable types will be of kind {@link TypeKind#ERROR}. */ private boolean allTypesExist(Collection<? extends Element> elements) { for (Element element : elements) { if (element.asType().getKind() == TypeKind.ERROR) { return false; } } return true; }
public TypeMirror getActualTypeOfEnclosingElementOfInjectedElement(GeneratedClassHolder holder, Element param) { DeclaredType enclosingClassType; if (param.getKind() == ElementKind.PARAMETER) { enclosingClassType = (DeclaredType) param.getEnclosingElement().getEnclosingElement().asType(); } else { enclosingClassType = (DeclaredType) param.getEnclosingElement().asType(); } return getActualType(param, enclosingClassType, holder); }
private void debugElement(Element element, String s) { logger.finest(format(s + ": kind=%s, cls=%s, simpleName=%s, type=%s, typeKind=%s, enclosed=%s, enclosing=%s", element.getKind(), element.getClass().getName(), element.getSimpleName(), element.asType(), element.asType().getKind(), element.getEnclosedElements(), element.getEnclosingElement())); TypeMirror typeMirror = element.asType(); if (element.getKind() == ENUM) { for (Element enclosed : element.getEnclosedElements()) { debugElement(enclosed, s + " "); } } else { debugType(typeMirror, element, s + " "); } }
@Override public void attach(Linker linker) { for (Element enclosedElement : enclosingClass.getEnclosedElements()) { if (enclosedElement.getKind().isField() && isStatic(enclosedElement)) { Inject injectAnnotation = enclosedElement.getAnnotation(Inject.class); if (injectAnnotation != null) { String key = GeneratorKeys.get(enclosedElement.asType()); linker.requestBinding(key, enclosingClass.toString(), getClass().getClassLoader()); } } } }
/** * Get information about methods returning class type of the original class so we can duplicate * them in the generated class for chaining purposes */ protected void collectMethodsReturningClassType(TypeElement modelClass, Types typeUtils) { TypeElement clazz = modelClass; while (clazz.getSuperclass().getKind() != TypeKind.NONE) { for (Element subElement : clazz.getEnclosedElements()) { Set<Modifier> modifiers = subElement.getModifiers(); if (subElement.getKind() == ElementKind.METHOD && !modifiers.contains(Modifier.PRIVATE) && !modifiers.contains(Modifier.FINAL) && !modifiers.contains(Modifier.STATIC)) { TypeMirror methodReturnType = ((ExecutableType) subElement.asType()).getReturnType(); if (methodReturnType.equals(clazz.asType()) || typeUtils.isSubtype(clazz.asType(), methodReturnType)) { ExecutableElement castedSubElement = ((ExecutableElement) subElement); List<? extends VariableElement> params = castedSubElement.getParameters(); String methodName = subElement.getSimpleName().toString(); if (methodName.equals(RESET_METHOD) && params.isEmpty()) { continue; } methodsReturningClassType.add(new MethodInfo(methodName, modifiers, buildParamSpecs(params), castedSubElement.isVarArgs())); } } } clazz = (TypeElement) typeUtils.asElement(clazz.getSuperclass()); } }
/** * Returns a method name from the {@code android.content.res.Resources} class for array resource * binding, null if the element type is not supported. */ private static @Nullable FieldResourceBinding.Type getArrayResourceMethodName(Element element) { TypeMirror typeMirror = element.asType(); if (TYPED_ARRAY_TYPE.equals(typeMirror.toString())) { return FieldResourceBinding.Type.TYPED_ARRAY; } if (TypeKind.ARRAY.equals(typeMirror.getKind())) { ArrayType arrayType = (ArrayType) typeMirror; String componentType = arrayType.getComponentType().toString(); if (STRING_TYPE.equals(componentType)) { return FieldResourceBinding.Type.STRING_ARRAY; } else if ("int".equals(componentType)) { return FieldResourceBinding.Type.INT_ARRAY; } else if ("java.lang.CharSequence".equals(componentType)) { return FieldResourceBinding.Type.TEXT_ARRAY; } } return null; }
/** * Check if the given class or any of its super classes have a super method with the given name. * Private methods are ignored since the generated subclass can't call super on those. */ protected boolean hasSuperMethod(TypeElement classElement, Element attribute) { if (!Utils.isEpoxyModel(classElement.asType())) { return false; } for (Element subElement : classElement.getEnclosedElements()) { if (subElement.getKind() == ElementKind.METHOD) { ExecutableElement method = (ExecutableElement) subElement; if (!method.getModifiers().contains(Modifier.PRIVATE) && method.getSimpleName().toString().equals(attribute.getSimpleName().toString()) && method.getParameters().size() == 1 && method.getParameters().get(0).asType().equals(attribute.asType())) { return true; } } } Element superClass = typeUtils.asElement(classElement.getSuperclass()); return (superClass instanceof TypeElement) && hasSuperMethod((TypeElement) superClass, attribute); }
private void buildMixins(RoundEnvironment roundEnv, IFactory factory, Map<Element, CommandSpec> mixinsDeclared, List<MixinInfo> mixinInfoList, Map<TypeMirror, List<CommandSpec>> commandTypes, Map<Element, OptionSpec.Builder> options, Map<Element, PositionalParamSpec.Builder> parameters) { Set<? extends Element> explicitMixins = roundEnv.getElementsAnnotatedWith(Mixin.class); for (Element element : explicitMixins) { if (element.asType().getKind() != TypeKind.DECLARED) { error(element, "@Mixin must have a declared type, not %s", element.asType()); continue; } TypeElement type = (TypeElement) ((DeclaredType) element.asType()).asElement(); CommandSpec mixin = buildCommand(type, factory, mixinsDeclared, commandTypes, options, parameters); logger.fine("Built mixin: " + mixin + " from " + element); if (EnumSet.of(ElementKind.FIELD, ElementKind.PARAMETER).contains(element.getKind())) { VariableElement variableElement = (VariableElement) element; String name = element.getAnnotation(Mixin.class).name(); if (name.length() == 0) { name = variableElement.getSimpleName().toString(); } Element targetType = element.getEnclosingElement(); CommandSpec mixee = buildCommand(targetType, factory, mixinsDeclared, commandTypes, options, parameters); mixinInfoList.add(new MixinInfo(mixee, name, mixin)); logger.fine("Mixin name=" + name + ", target command=" + mixee.userObject()); } } }
private TypeMirror getEntityOrIdType(Element element, int index) { if (isSubtypeOfDao(element.asType())) { DeclaredType declaredType = (DeclaredType) element.asType(); List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments(); if (typeArguments.size() == 2) { return typeArguments.get(index); } } List<? extends TypeMirror> superTypes = helper.directSupertypes(element.asType()); for (TypeMirror superType : superTypes) { if (superType.getKind() == TypeKind.DECLARED && isSubtypeOfDao(superType)) { DeclaredType superDeclaredType = (DeclaredType) superType; List<? extends TypeMirror> typeArguments = superDeclaredType.getTypeArguments(); if (typeArguments.size() == 2) { return typeArguments.get(index); } } } return null; }
public static <T extends Element> ImmutableList<FieldModel> extractFields(T element) { final List<FieldModel> fields = new ArrayList<>(); for (Element enclosedElement : element.getEnclosedElements()) { if (ElementKind.FIELD.equals(enclosedElement.getKind())) { Set<Modifier> modifiers = enclosedElement.getModifiers(); fields.add( new FieldModel( FieldSpec.builder( TypeName.get(enclosedElement.asType()), enclosedElement.getSimpleName().toString(), modifiers.toArray(new Modifier[modifiers.size()])) .build(), enclosedElement)); } } return ImmutableList.copyOf(fields); } }
if ( !expectedElementKind.equals( elem.getKind() ) ) { continue; if ( ElementKind.METHOD.equals( elem.getKind() ) ) { name = StringUtil.getPropertyName( name ); mirror = ( (ExecutableElement) elem ).getReturnType(); mirror = elem.asType(); switch ( mirror.getKind() ) { case INT: { return "java.lang.Integer"; return ((DeclaredType)mirror).asElement().asType().toString();
public void isSharedPreference(Element element, ElementValidation valid) { TypeMirror type = element.asType(); if (element instanceof ExecutableElement) { element = ((ExecutableElement) element).getParameters().get(0); type = element.asType(); if (type instanceof ErrorType || type.getKind() == TypeKind.ERROR) { String elementTypeName = type.toString();
private void handleEmbeddedType(Element element, Set<TypeElement> elements) { TypeMirror type = element.asType(); if (element.getKind() == ElementKind.METHOD) { type = ((ExecutableElement) element).getReturnType(); } String typeName = type.toString(); if (typeName.startsWith(Collection.class.getName()) || typeName.startsWith(List.class.getName()) || typeName.startsWith(Set.class.getName())) { type = ((DeclaredType) type).getTypeArguments().get(0); } else if (typeName.startsWith(Map.class.getName())) { type = ((DeclaredType) type).getTypeArguments().get(1); } TypeElement typeElement = typeExtractor.visit(type); if (typeElement != null && !TypeUtils.hasAnnotationOfType(typeElement, conf.getEntityAnnotations())) { if (!typeElement.getQualifiedName().toString().startsWith("java.")) { elements.add(typeElement); } } }
} else { if (element().getKind().isField()) { isNullable = !element().asType().getKind().isPrimitive(); } else if(element().getKind() == ElementKind.METHOD) { ExecutableElement executableElement = (ExecutableElement) element(); isNullable = !executableElement.getReturnType().getKind().isPrimitive();
private ControllerModelField buildFieldInfo(Element modelFieldElement) { validateFieldAccessibleViaGeneratedCode(modelFieldElement, AutoModel.class, errorLogger); TypeMirror fieldType = modelFieldElement.asType(); if (fieldType.getKind() != TypeKind.ERROR) { // If the field is a generated Epoxy model then the class won't have been generated // yet and it won't have type info. If the type can't be found that we assume it is // a generated model and is ok. if (!isEpoxyModel(fieldType)) { errorLogger.logError("Fields with %s annotations must be of type %s (%s#%s)", AutoModel.class.getSimpleName(), EPOXY_MODEL_TYPE, modelFieldElement.getEnclosingElement().getSimpleName(), modelFieldElement.getSimpleName()); } } return new ControllerModelField(modelFieldElement); }
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."); } }