Search in sources :

Example 1 with TypeName

use of com.squareup.javapoet.TypeName in project epoxy by airbnb.

the class EpoxyProcessor method generateEquals.

private MethodSpec generateEquals(ClassToGenerateInfo helperClass) {
    Builder builder = MethodSpec.methodBuilder("equals").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).returns(boolean.class).addParameter(Object.class, "o").beginControlFlow("if (o == this)").addStatement("return true").endControlFlow().beginControlFlow("if (!(o instanceof $T))", helperClass.getGeneratedName()).addStatement("return false").endControlFlow().beginControlFlow("if (!super.equals(o))").addStatement("return false").endControlFlow().addStatement("$T that = ($T) o", helperClass.getGeneratedName(), helperClass.getGeneratedName());
    for (AttributeInfo attributeInfo : helperClass.getAttributeInfo()) {
        TypeName type = attributeInfo.getType();
        if (!attributeInfo.useInHash() && type.isPrimitive()) {
            continue;
        }
        String name = attributeInfo.getName();
        if (attributeInfo.useInHash()) {
            if (type == FLOAT) {
                builder.beginControlFlow("if (Float.compare(that.$L, $L) != 0)", name, name).addStatement("return false").endControlFlow();
            } else if (type == DOUBLE) {
                builder.beginControlFlow("if (Double.compare(that.$L, $L) != 0)", name, name).addStatement("return false").endControlFlow();
            } else if (type.isPrimitive()) {
                builder.beginControlFlow("if ($L != that.$L)", name, name).addStatement("return false").endControlFlow();
            } else if (type instanceof ArrayTypeName) {
                builder.beginControlFlow("if (!$T.equals($L, that.$L))", TypeName.get(Arrays.class), name, name).addStatement("return false").endControlFlow();
            } else {
                builder.beginControlFlow("if ($L != null ? !$L.equals(that.$L) : that.$L != null)", name, name, name, name).addStatement("return false").endControlFlow();
            }
        } else {
            builder.beginControlFlow("if ($L != null && that.$L == null" + " || $L == null && that.$L != null)", name, name, name, name).addStatement("return false").endControlFlow();
        }
    }
    return builder.addStatement("return true").build();
}
Also used : TypeName(com.squareup.javapoet.TypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) Builder(com.squareup.javapoet.MethodSpec.Builder)

Example 2 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) {
    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" + 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])) {
                            builder.add("$T.<$T>castParam(p$L, $S, $L, $S, $L)", UTILS, parameter.getType(), listenerPosition, method.name(), listenerPosition, methodBinding.getName(), i);
                        } 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 3 with TypeName

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

the class BindingSet method bestGuess.

private static TypeName bestGuess(String type) {
    switch(type) {
        case "void":
            return TypeName.VOID;
        case "boolean":
            return TypeName.BOOLEAN;
        case "byte":
            return TypeName.BYTE;
        case "char":
            return TypeName.CHAR;
        case "double":
            return TypeName.DOUBLE;
        case "float":
            return TypeName.FLOAT;
        case "int":
            return TypeName.INT;
        case "long":
            return TypeName.LONG;
        case "short":
            return TypeName.SHORT;
        default:
            int left = type.indexOf('<');
            if (left != -1) {
                ClassName typeClassName = ClassName.bestGuess(type.substring(0, left));
                List<TypeName> typeArguments = new ArrayList<>();
                do {
                    typeArguments.add(WildcardTypeName.subtypeOf(Object.class));
                    left = type.indexOf('<', left + 1);
                } while (left != -1);
                return ParameterizedTypeName.get(typeClassName, typeArguments.toArray(new TypeName[typeArguments.size()]));
            }
            return ClassName.bestGuess(type);
    }
}
Also used : WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) ClassName(com.squareup.javapoet.ClassName) ArrayList(java.util.ArrayList)

Example 4 with TypeName

use of com.squareup.javapoet.TypeName in project caffeine by ben-manes.

the class NodeFactoryGenerator method addNodeSpec.

private void addNodeSpec(String className, boolean isFinal, Set<Feature> features) {
    TypeName superClass;
    Set<Feature> parentFeatures;
    Set<Feature> generateFeatures;
    if (features.size() == 2) {
        parentFeatures = ImmutableSet.of();
        generateFeatures = features;
        superClass = TypeName.OBJECT;
    } else {
        parentFeatures = ImmutableSet.copyOf(Iterables.limit(features, features.size() - 1));
        generateFeatures = ImmutableSet.of(Iterables.getLast(features));
        superClass = ParameterizedTypeName.get(ClassName.get(PACKAGE_NAME + ".NodeFactory", encode(Feature.makeClassName(parentFeatures))), kTypeVar, vTypeVar);
    }
    NodeContext context = new NodeContext(superClass, className, isFinal, parentFeatures, generateFeatures);
    for (NodeRule rule : rules) {
        rule.accept(context);
    }
    nodeFactory.addType(context.nodeSubtype.build());
    addEnumConstant(className, features);
}
Also used : TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) NodeRule(com.github.benmanes.caffeine.cache.node.NodeRule) NodeContext(com.github.benmanes.caffeine.cache.node.NodeContext)

Example 5 with TypeName

use of com.squareup.javapoet.TypeName in project glide by bumptech.

the class RootModuleGenerator method generateGetExcludedModuleClasses.

// TODO: When we drop support for parsing GlideModules from AndroidManifests, remove this method.
private static 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