Search in sources :

Example 11 with TypeName

use of com.squareup.javapoet.TypeName in project react4j by react4j.

the class Generator method buildStaticCallbackMethod.

@Nonnull
private static MethodSpec.Builder buildStaticCallbackMethod(@Nonnull final ComponentDescriptor descriptor, @Nonnull final CallbackDescriptor callback) {
    final TypeName handlerType = TypeName.get(callback.getCallbackType().asType());
    final String handlerName = "_" + callback.getMethod().getSimpleName();
    final MethodSpec.Builder method = MethodSpec.methodBuilder(handlerName).addAnnotation(NONNULL_CLASSNAME).returns(handlerType);
    method.addModifiers(Modifier.STATIC);
    final ParameterSpec.Builder parameter = ParameterSpec.builder(TypeName.get(descriptor.getElement().asType()), "component", Modifier.FINAL).addAnnotation(NONNULL_CLASSNAME);
    method.addParameter(parameter.build());
    if (!descriptor.getElement().getTypeParameters().isEmpty()) {
        method.addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "unused").build());
        ProcessorUtil.copyTypeParameters(descriptor.getElement(), method);
    }
    method.addStatement("return (($T) component).$N", descriptor.getEnhancedClassName(), handlerName);
    return method;
}
Also used : WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec) Nonnull(javax.annotation.Nonnull)

Example 12 with TypeName

use of com.squareup.javapoet.TypeName in project butterknife by JakeWharton.

the class BindingSet method addMethodBindings.

private void addMethodBindings(MethodSpec.Builder result, ViewBinding binding, boolean debuggable) {
    Map<ListenerClass, Map<ListenerMethod, Set<MethodViewBinding>>> classMethodBindings = binding.getMethodBindings();
    if (classMethodBindings.isEmpty()) {
        return;
    }
    // We only need to emit the null check if there are zero required bindings.
    boolean needsNullChecked = binding.getRequiredBindings().isEmpty();
    if (needsNullChecked) {
        result.beginControlFlow("if (view != null)");
    }
    // Add the view reference to the binding.
    String fieldName = "viewSource";
    String bindName = "source";
    if (!binding.isBoundToRoot()) {
        fieldName = "view" + Integer.toHexString(binding.getId().value);
        bindName = "view";
    }
    result.addStatement("$L = $N", fieldName, bindName);
    for (Map.Entry<ListenerClass, Map<ListenerMethod, Set<MethodViewBinding>>> e : classMethodBindings.entrySet()) {
        ListenerClass listener = e.getKey();
        Map<ListenerMethod, Set<MethodViewBinding>> methodBindings = e.getValue();
        TypeSpec.Builder callback = TypeSpec.anonymousClassBuilder("").superclass(ClassName.bestGuess(listener.type()));
        for (ListenerMethod method : getListenerMethods(listener)) {
            MethodSpec.Builder callbackMethod = MethodSpec.methodBuilder(method.name()).addAnnotation(Override.class).addModifiers(PUBLIC).returns(bestGuess(method.returnType()));
            String[] parameterTypes = method.parameters();
            for (int i = 0, count = parameterTypes.length; i < count; i++) {
                callbackMethod.addParameter(bestGuess(parameterTypes[i]), "p" + i);
            }
            boolean hasReturnType = !"void".equals(method.returnType());
            CodeBlock.Builder builder = CodeBlock.builder();
            if (hasReturnType) {
                builder.add("return ");
            }
            if (methodBindings.containsKey(method)) {
                for (MethodViewBinding methodBinding : methodBindings.get(method)) {
                    builder.add("target.$L(", methodBinding.getName());
                    List<Parameter> parameters = methodBinding.getParameters();
                    String[] listenerParameters = method.parameters();
                    for (int i = 0, count = parameters.size(); i < count; i++) {
                        if (i > 0) {
                            builder.add(", ");
                        }
                        Parameter parameter = parameters.get(i);
                        int listenerPosition = parameter.getListenerPosition();
                        if (parameter.requiresCast(listenerParameters[listenerPosition])) {
                            if (debuggable) {
                                builder.add("$T.castParam(p$L, $S, $L, $S, $L, $T.class)", UTILS, listenerPosition, method.name(), listenerPosition, methodBinding.getName(), i, parameter.getType());
                            } else {
                                builder.add("($T) p$L", parameter.getType(), listenerPosition);
                            }
                        } else {
                            builder.add("p$L", listenerPosition);
                        }
                    }
                    builder.add(");\n");
                }
            } else if (hasReturnType) {
                builder.add("$L;\n", method.defaultReturn());
            }
            callbackMethod.addCode(builder.build());
            callback.addMethod(callbackMethod.build());
        }
        boolean requiresRemoval = listener.remover().length() != 0;
        String listenerField = null;
        if (requiresRemoval) {
            TypeName listenerClassName = bestGuess(listener.type());
            listenerField = fieldName + ((ClassName) listenerClassName).simpleName();
            result.addStatement("$L = $L", listenerField, callback.build());
        }
        if (!VIEW_TYPE.equals(listener.targetType())) {
            result.addStatement("(($T) $N).$L($L)", bestGuess(listener.targetType()), bindName, listener.setter(), requiresRemoval ? listenerField : callback.build());
        } else {
            result.addStatement("$N.$L($L)", bindName, listener.setter(), requiresRemoval ? listenerField : callback.build());
        }
    }
    if (needsNullChecked) {
        result.endControlFlow();
    }
}
Also used : WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) ListenerClass(butterknife.internal.ListenerClass) Set(java.util.Set) MethodSpec(com.squareup.javapoet.MethodSpec) CodeBlock(com.squareup.javapoet.CodeBlock) ClassName(com.squareup.javapoet.ClassName) ListenerMethod(butterknife.internal.ListenerMethod) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 13 with TypeName

use of com.squareup.javapoet.TypeName in project butterknife by JakeWharton.

the class BindingSet method addFieldAndUnbindStatement.

private void addFieldAndUnbindStatement(TypeSpec.Builder result, MethodSpec.Builder unbindMethod, ViewBinding bindings) {
    // Only add fields to the binding if there are method bindings.
    Map<ListenerClass, Map<ListenerMethod, Set<MethodViewBinding>>> classMethodBindings = bindings.getMethodBindings();
    if (classMethodBindings.isEmpty()) {
        return;
    }
    String fieldName = bindings.isBoundToRoot() ? "viewSource" : "view" + Integer.toHexString(bindings.getId().value);
    result.addField(VIEW, fieldName, PRIVATE);
    // We only need to emit the null check if there are zero required bindings.
    boolean needsNullChecked = bindings.getRequiredBindings().isEmpty();
    if (needsNullChecked) {
        unbindMethod.beginControlFlow("if ($N != null)", fieldName);
    }
    for (ListenerClass listenerClass : classMethodBindings.keySet()) {
        // We need to keep a reference to the listener
        // in case we need to unbind it via a remove method.
        boolean requiresRemoval = !"".equals(listenerClass.remover());
        String listenerField = "null";
        if (requiresRemoval) {
            TypeName listenerClassName = bestGuess(listenerClass.type());
            listenerField = fieldName + ((ClassName) listenerClassName).simpleName();
            result.addField(listenerClassName, listenerField, PRIVATE);
        }
        if (!VIEW_TYPE.equals(listenerClass.targetType())) {
            unbindMethod.addStatement("(($T) $N).$N($N)", bestGuess(listenerClass.targetType()), fieldName, removerOrSetter(listenerClass, requiresRemoval), listenerField);
        } else {
            unbindMethod.addStatement("$N.$N($N)", fieldName, removerOrSetter(listenerClass, requiresRemoval), listenerField);
        }
        if (requiresRemoval) {
            unbindMethod.addStatement("$N = null", listenerField);
        }
    }
    unbindMethod.addStatement("$N = null", fieldName);
    if (needsNullChecked) {
        unbindMethod.endControlFlow();
    }
}
Also used : WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) ListenerClass(butterknife.internal.ListenerClass) ClassName(com.squareup.javapoet.ClassName) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 14 with TypeName

use of com.squareup.javapoet.TypeName in project web3sdk by FISCO-BCOS.

the class SolidityFunctionWrapper method buildVariableLengthReturnFunctionConstructor.

private static void buildVariableLengthReturnFunctionConstructor(MethodSpec.Builder methodBuilder, String functionName, String inputParameters, List<TypeName> outputParameterTypes) throws ClassNotFoundException {
    List<Object> objects = new ArrayList<>();
    objects.add(Function.class);
    objects.add(Function.class);
    objects.add(functionName);
    objects.add(Arrays.class);
    objects.add(Type.class);
    objects.add(inputParameters);
    objects.add(Arrays.class);
    objects.add(TypeReference.class);
    for (TypeName outputParameterType : outputParameterTypes) {
        objects.add(TypeReference.class);
        objects.add(outputParameterType);
    }
    String asListParams = Collection.join(outputParameterTypes, ", ", typeName -> "new $T<$T>() {}");
    methodBuilder.addStatement("$T function = new $T($S, \n$T.<$T>asList($L), \n$T" + ".<$T<?>>asList(" + asListParams + "))", objects.toArray());
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) ArrayList(java.util.ArrayList)

Example 15 with TypeName

use of com.squareup.javapoet.TypeName in project Rocket by mozilla-tw.

the class AppModuleGenerator method generateGetExcludedModuleClasses.

// TODO: When we drop support for parsing GlideModules from AndroidManifests, remove this method.
private MethodSpec generateGetExcludedModuleClasses(Set<String> excludedClassNames) {
    TypeName wildCardOfObject = WildcardTypeName.subtypeOf(Object.class);
    ParameterizedTypeName classOfWildcardOfObjet = ParameterizedTypeName.get(ClassName.get(Class.class), wildCardOfObject);
    ParameterizedTypeName setOfClassOfWildcardOfObject = ParameterizedTypeName.get(ClassName.get(Set.class), classOfWildcardOfObjet);
    ParameterizedTypeName hashSetOfClassOfWildcardOfObject = ParameterizedTypeName.get(ClassName.get(HashSet.class), classOfWildcardOfObjet);
    MethodSpec.Builder builder = MethodSpec.methodBuilder("getExcludedModuleClasses").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).returns(setOfClassOfWildcardOfObject);
    if (excludedClassNames.isEmpty()) {
        builder.addStatement("return $T.emptySet()", Collections.class);
    } else {
        builder.addStatement("$T excludedClasses = new $T()", setOfClassOfWildcardOfObject, hashSetOfClassOfWildcardOfObject);
        for (String excludedClassName : excludedClassNames) {
            // TODO: Remove this when we no longer support manifest parsing.
            // Using a Literal ($L) instead of a type ($T) to get a fully qualified import that allows
            // us to suppress deprecation warnings. Aimed at deprecated GlideModules.
            builder.addStatement("excludedClasses.add($L.class)", excludedClassName);
        }
        builder.addStatement("return excludedClasses");
    }
    return builder.build();
}
Also used : WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) Set(java.util.Set) HashSet(java.util.HashSet) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) HashSet(java.util.HashSet)

Aggregations

TypeName (com.squareup.javapoet.TypeName)296 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)232 MethodSpec (com.squareup.javapoet.MethodSpec)109 ClassName (com.squareup.javapoet.ClassName)82 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)78 TypeSpec (com.squareup.javapoet.TypeSpec)55 ArrayTypeName (com.squareup.javapoet.ArrayTypeName)50 TypeElement (javax.lang.model.element.TypeElement)46 ArrayList (java.util.ArrayList)45 TypeMirror (javax.lang.model.type.TypeMirror)41 ExecutableElement (javax.lang.model.element.ExecutableElement)36 ParameterSpec (com.squareup.javapoet.ParameterSpec)26 Builder (com.squareup.javapoet.TypeSpec.Builder)22 SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)21 CodeBlock (com.squareup.javapoet.CodeBlock)20 VariableElement (javax.lang.model.element.VariableElement)20 SQLiteEntity (com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity)19 SQLProperty (com.abubusoft.kripton.processor.sqlite.model.SQLProperty)18 List (java.util.List)18 Map (java.util.Map)17