compiler/src/main/java/io/sweers/barber/Barbershop.java
private MethodSpec generateStyleMethod() {
        MethodSpec.Builder builder = MethodSpec.methodBuilder("style")
                .addAnnotation(Override.class)
                .addModifiers(Modifier.PUBLIC)
                .returns(void.class)
                .addParameter(TypeVariableName.get("T"), "target", Modifier.FINAL)
                .addParameter(AttributeSet.class, "set", Modifier.FINAL)
                .addParameter(int[].class, "attrs", Modifier.FINAL)
                .addParameter(int.class, "defStyleAttr", Modifier.FINAL)
                .addParameter(int.class, "defStyleRes", Modifier.FINAL);

        if (parentBarbershop != null) {
            builder.beginControlFlow("if (!super.hasStyled(target))")
                    .addStatement("super.style(target, set, attrs, defStyleAttr, defStyleRes)")
                    .addStatement("return")
                    .endControlFlow();
        }

        // Update our latest target
        builder.addStatement("this.lastStyledTargets.add(target)");

        // Don't do anything if there's no AttributeSet instance
        builder.beginControlFlow("if (set == null)")
                .addStatement("return")
                .endControlFlow();

        if (!styleableBindings.isEmpty()) {
            if (hasDefaults) {
                builder.addStatement("$T res = target.getContext().getResources()", Resources.class);
            }

            // Proceed with obtaining the TypedArray if we got here
            builder.addStatement("$T a = target.getContext().obtainStyledAttributes(set, attrs, defStyleAttr, defStyleRes)", TypedArray.class);


            builder.addCode("// Retrieve custom attributes\n");
            for (StyleableBinding binding : styleableBindings.values()) {

                if (binding.kind == RES_ID && binding.hasDefaultValue() && !binding.isRequired) {
                    // No overhead in resource retrieval for these, so no need to wrap in a hasValue check
                    builder.addStatement(generateSetterStatement(binding), binding.name, binding.getFormattedStatement("a."));
                    continue;
                }

                // Wrap the styling with if-statement to check if there's a value first, this way we can
                // keep existing default values if there isn't one and don't overwrite them.
                builder.beginControlFlow("if (a.hasValue($L))", binding.id);
                builder.addStatement(generateSetterStatement(binding), binding.name, binding.getFormattedStatement("a."));

                if (binding.isRequired) {
                    builder.nextControlFlow("else");
                    builder.addStatement("throw new $T(\"Missing required attribute \'$L\' while styling \'$L\'\")", IllegalStateException.class, binding.name, targetClass);
                } else if (binding.hasDefaultValue()) {
                    builder.nextControlFlow("else");
                    if (binding.kind != FRACTION && binding.kind != DIMEN && ("float".equals(binding.type) || "java.lang.Float".equals(binding.type))) {
                        // Getting a float from resources is nasty
                        builder.addStatement(generateSetterStatement(binding), binding.name, "Barber.resolveFloatResource(res, " + binding.defaultValue + ")");
                    } else if ("android.graphics.drawable.Drawable".equals(binding.type)) {
                        // Compatibility using ResourcesCompat.getDrawable(...)
                        builder.addStatement(generateSetterStatement(binding), binding.name,
                                "android.support.v4.content.res.ResourcesCompat.getDrawable(res, "
                                        + binding.defaultValue
                                        + ", target.getContext().getTheme())");
                    } else {
                        builder.addStatement(generateSetterStatement(binding), binding.name, generateResourceStatement(binding, "res.", true));
                    }
                }
                builder.endControlFlow();
            }

            builder.addStatement("a.recycle()");
        }

        if (!androidAttrBindings.isEmpty()) {
            builder.addCode("// Retrieve android attr values\n");
            for (AndroidAttrBinding binding : androidAttrBindings.values()) {
                builder.addStatement(generateSetterStatement(binding), binding.name, binding.getFormattedStatement("set."));
            }
        }

        return builder.build();
    }
private MethodSpec generateStyleMethod() {
        MethodSpec.Builder builder = MethodSpec.methodBuilder("style")
                .addAnnotation(Override.class)
                .addModifiers(Modifier.PUBLIC)
                .returns(void.class)
                .addParameter(TypeVariableName.get("T"), "target", Modifier.FINAL)
                .addParameter(AttributeSet.class, "set", Modifier.FINAL)
                .addParameter(int[].class, "attrs", Modifier.FINAL)
                .addParameter(int.class, "defStyleAttr", Modifier.FINAL)
                .addParameter(int.class, "defStyleRes", Modifier.FINAL);

        if (parentBarbershop != null) {
            builder.beginControlFlow("if (!super.hasStyled(target))")
                    .addStatement("super.style(target, set, attrs, defStyleAttr, defStyleRes)")
                    .addStatement("return")
                    .endControlFlow();
        }

        // Update our latest target
        builder.addStatement("this.lastStyledTargets.add(target)");

        // Don't do anything if there's no AttributeSet instance
        builder.beginControlFlow("if (set == null)")
                .addStatement("return")
                .endControlFlow();

        if (!styleableBindings.isEmpty()) {
            if (hasDefaults) {
                builder.addStatement("$T res = target.getContext().getResources()", Resources.class);
            }

            // Proceed with obtaining the TypedArray if we got here
            builder.addStatement("$T a = target.getContext().obtainStyledAttributes(set, attrs, defStyleAttr, defStyleRes)", TypedArray.class);


            builder.addCode("// Retrieve custom attributes\n");
            for (StyleableBinding binding : styleableBindings.values()) {

                if (binding.kind == RES_ID && binding.hasDefaultValue() && !binding.isRequired) {
                    // No overhead in resource retrieval for these, so no need to wrap in a hasValue check
                    builder.addStatement(generateSetterStatement(binding), binding.name, binding.getFormattedStatement("a."));
                    continue;
                }

                // Wrap the styling with if-statement to check if there's a value first, this way we can
                // keep existing default values if there isn't one and don't overwrite them.
                builder.beginControlFlow("if (a.hasValue($L))", binding.id);
                builder.addStatement(generateSetterStatement(binding), binding.name, binding.getFormattedStatement("a."));

                if (binding.isRequired) {
                    builder.nextControlFlow("else");
                    builder.addStatement("throw new $T(\"Missing required attribute \'$L\' while styling \'$L\'\")", IllegalStateException.class, binding.name, targetClass);
                } else if (binding.hasDefaultValue()) {
                    builder.nextControlFlow("else");
                    if (binding.kind != FRACTION && binding.kind != DIMEN && ("float".equals(binding.type) || "java.lang.Float".equals(binding.type))) {
                        // Getting a float from resources is nasty
                        builder.addStatement(generateSetterStatement(binding), binding.name, "Barber.resolveFloatResource(res, " + binding.defaultValue + ")");
                    } else if ("android.graphics.drawable.Drawable".equals(binding.type)) {
                        // Compatibility using ResourcesCompat.getDrawable(...)
                        builder.addStatement(generateSetterStatement(binding), binding.name,
                                "android.support.v4.content.res.ResourcesCompat.getDrawable(res, "
                                        + binding.defaultValue
                                        + ", target.getContext().getTheme())");
                    } else {
                        builder.addStatement(generateSetterStatement(binding), binding.name, generateResourceStatement(binding, "res.", true));
                    }
                }
                builder.endControlFlow();
            }

            builder.addStatement("a.recycle()");
        }

        if (!androidAttrBindings.isEmpty()) {
            builder.addCode("// Retrieve android attr values\n");
            for (AndroidAttrBinding binding : androidAttrBindings.values()) {
                builder.addStatement(generateSetterStatement(binding), binding.name, binding.getFormattedStatement("set."));
            }
        }

        return builder.build();
    }
private MethodSpec generateStyleMethod ( ) { MethodSpec . Builder builder = MethodSpec . methodBuilder ( "style" ) . addAnnotation ( Override . class ) . addModifiers ( Modifier . PUBLIC ) . returns ( void . class ) . addParameter ( TypeVariableName . get ( "T" ) , "target" , Modifier . FINAL ) . addParameter ( AttributeSet . class , "set" , Modifier . FINAL ) . addParameter ( int [ ] . class , "attrs" , Modifier . FINAL ) . addParameter ( int . class , "defStyleAttr" , Modifier . FINAL ) . addParameter ( int . class , "defStyleRes" , Modifier . FINAL ) ; if ( parentBarbershop != null ) { builder . beginControlFlow ( "if (!super.hasStyled(target))" ) . addStatement ( "super.style(target, set, attrs, defStyleAttr, defStyleRes)" ) . addStatement ( "return" ) . endControlFlow ( ) ; } // Update our latest target builder . addStatement ( "this.lastStyledTargets.add(target)" ) ; // Don't do anything if there's no AttributeSet instance builder . beginControlFlow ( "if (set == null)" ) . addStatement ( "return" ) . endControlFlow ( ) ; if ( ! styleableBindings . isEmpty ( ) ) { if ( hasDefaults ) { builder . addStatement ( "$T res = target.getContext().getResources()" , Resources . class ) ; } // Proceed with obtaining the TypedArray if we got here builder . addStatement ( "$T a = target.getContext().obtainStyledAttributes(set, attrs, defStyleAttr, defStyleRes)" , TypedArray . class ) ; builder . addCode ( "// Retrieve custom attributes\n" ) ; for ( StyleableBinding binding : styleableBindings . values ( ) ) { if ( binding . kind == RES_ID && binding . hasDefaultValue ( ) && ! binding . isRequired ) { // No overhead in resource retrieval for these, so no need to wrap in a hasValue check builder . addStatement ( generateSetterStatement ( binding ) , binding . name , binding . getFormattedStatement ( "a." ) ) ; continue ; } // Wrap the styling with if-statement to check if there's a value first, this way we can // keep existing default values if there isn't one and don't overwrite them. builder . beginControlFlow ( "if (a.hasValue($L))" , binding . id ) ; builder . addStatement ( generateSetterStatement ( binding ) , binding . name , binding . getFormattedStatement ( "a." ) ) ; if ( binding . isRequired ) { builder . nextControlFlow ( "else" ) ; builder . addStatement ( "throw new $T(\"Missing required attribute \'$L\' while styling \'$L\'\")" , IllegalStateException . class , binding . name , targetClass ) ; } else if ( binding . hasDefaultValue ( ) ) { builder . nextControlFlow ( "else" ) ; if ( binding . kind != FRACTION && binding . kind != DIMEN && ( "float" . equals ( binding . type ) || "java.lang.Float" . equals ( binding . type ) ) ) { // Getting a float from resources is nasty builder . addStatement ( generateSetterStatement ( binding ) , binding . name , "Barber.resolveFloatResource(res, " + binding . defaultValue + ")" ) ; } else if ( "android.graphics.drawable.Drawable" . equals ( binding . type ) ) { // Compatibility using ResourcesCompat.getDrawable(...) builder . addStatement ( generateSetterStatement ( binding ) , binding . name , "android.support.v4.content.res.ResourcesCompat.getDrawable(res, " + binding . defaultValue + ", target.getContext().getTheme())" ) ; } else { builder . addStatement ( generateSetterStatement ( binding ) , binding . name , generateResourceStatement ( binding , "res." , true ) ) ; } } builder . endControlFlow ( ) ; } builder . addStatement ( "a.recycle()" ) ; } if ( ! androidAttrBindings . isEmpty ( ) ) { builder . addCode ( "// Retrieve android attr values\n" ) ; for ( AndroidAttrBinding binding : androidAttrBindings . values ( ) ) { builder . addStatement ( generateSetterStatement ( binding ) , binding . name , binding . getFormattedStatement ( "set." ) ) ; } } return builder . build ( ) ; }
This generates the actual style() method implementation for the $$Barbershop class
@return A complete MethodSpec implementation for the class's style() method.
This generates the actual style () method implementation for the $$Barbershop class