Example usage for javax.lang.model.type TypeKind BOOLEAN

List of usage examples for javax.lang.model.type TypeKind BOOLEAN

Introduction

In this page you can find the example usage for javax.lang.model.type TypeKind BOOLEAN.

Prototype

TypeKind BOOLEAN

To view the source code for javax.lang.model.type TypeKind BOOLEAN.

Click Source Link

Document

The primitive type boolean .

Usage

From source file:info.archinnov.achilles.internals.parser.FieldInfoParser.java

protected List<String> deriveGetterName(VariableElement elm) {
    final String fieldName = elm.getSimpleName().toString();
    final TypeMirror typeMirror = elm.asType();
    String camelCase = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
    if (typeMirror.getKind() == TypeKind.BOOLEAN) {
        return asList("is" + camelCase, "get" + camelCase);
    } else {/*from   w ww . j  a v a2s  . c om*/
        return asList("get" + camelCase);
    }
}

From source file:cop.raml.utils.example.JsonExample.java

/**
 * Generates random example for given primitive {@code kind} using given {@code random} generator.
 *
 * @param kind   primitive kind type/* ww  w  .ja  va  2  s  .com*/
 * @param random not {@code null} random generator
 * @return generated random example for given primitive type
 */
private static Object getPrimitiveExample(TypeKind kind, @NotNull Random random) {
    if (kind == TypeKind.BOOLEAN)
        return random.nextBoolean();
    if (kind == TypeKind.BYTE)
        return (byte) random.nextInt(255);
    if (kind == TypeKind.SHORT)
        return (short) random.nextInt(1000);
    if (kind == TypeKind.INT)
        return random.nextInt(1000);
    if (kind == TypeKind.LONG)
        return (long) random.nextInt(1000);
    if (kind == TypeKind.CHAR)
        return (char) ('A' + random.nextInt(28));
    if (kind == TypeKind.FLOAT)
        return (float) random.nextInt(1000);
    if (kind == TypeKind.DOUBLE)
        return (double) random.nextInt(1000);
    return null;
}

From source file:io.soabase.halva.processor.caseclass.Templates.java

void addHashCode(CaseClassSpec spec, TypeSpec.Builder builder) {
    MethodSpec.Builder hashCodeBuilder = MethodSpec.methodBuilder("hashCode").returns(TypeName.INT)
            .addAnnotation(Override.class).addModifiers(Modifier.PUBLIC);
    boolean firstTime = true;
    for (CaseClassItem item : spec.getItems()) {
        String field;/*  w ww.j  a va 2 s  . c  om*/
        if (item.getType().getKind() == TypeKind.BOOLEAN) {
            field = "(" + item.getName() + " ? 1 : 0)";
        } else if (item.getType().getKind() == TypeKind.FLOAT) {
            field = "Float.hashCode(" + item.getName() + ")";
        } else if (item.getType().getKind() == TypeKind.DOUBLE) {
            field = "Double.hashCode(" + item.getName() + ")";
        } else if (item.getType().getKind() == TypeKind.LONG) {
            field = "Long.hashCode(" + item.getName() + ")";
        } else if (item.getType().getKind().isPrimitive()) {
            field = item.getName();
        } else {
            field = item.getName() + ".hashCode()";
        }
        String format = firstTime ? "int result = $L" : "result = 31 * result + $L";
        hashCodeBuilder.addStatement(format, field);
        firstTime = false;
    }
    if (spec.getItems().size() > 0) {
        hashCodeBuilder.addStatement("return result");
    } else {
        hashCodeBuilder.addStatement("return super.hashCode()");
    }
    builder.addMethod(hashCodeBuilder.build());
}

From source file:org.kie.workbench.common.forms.adf.processors.FormDefinitionsProcessor.java

private void processFieldDefinition(TypeElement fieldDefinitionElement) throws Exception {
    final Messager messager = processingEnv.getMessager();

    messager.printMessage(Diagnostic.Kind.NOTE,
            "Discovered FieldDefinition class [" + fieldDefinitionElement.getSimpleName() + "]");

    Collection<FieldInfo> fieldInfos = extractFieldInfos(fieldDefinitionElement, null);

    String modelClassName = fieldDefinitionElement.getQualifiedName().toString();

    String fieldModifierName = fixClassName(modelClassName) + "_FieldStatusModifier";

    Map<String, String> fieldDefinition = new HashMap<>();

    fieldDefinition.put("className", modelClassName);
    fieldDefinition.put("fieldModifierName", fieldModifierName);

    Map<String, Object> templateContext = new HashMap<>();

    templateContext.put("modelClassName", modelClassName);
    templateContext.put("fieldModifierName", fieldModifierName);

    FieldDefinition fieldDefinitionAnnotation = fieldDefinitionElement.getAnnotation(FieldDefinition.class);

    for (FieldInfo fieldInfo : fieldInfos) {
        AnnotationMirror annotation = GeneratorUtils.getAnnotation(elementUtils, fieldInfo.fieldElement,
                FieldValue.class.getName());

        if (annotation != null) {
            if (fieldDefinition.containsKey("value")) {
                throw new Exception("Problem processing FieldDefinition [" + modelClassName
                        + "]: it should have only one @FieldValue");
            }//from w w  w  .j a v  a 2  s.c o m

            if (fieldInfo.getter == null || fieldInfo.setter == null) {
                throw new Exception("Problem processing FieldDefinition [" + modelClassName
                        + "]: field marked as @FieldValue should have setter & getter");
            }

            fieldDefinition.put("value", fieldInfo.fieldElement.getSimpleName().toString());
        } else {
            annotation = GeneratorUtils.getAnnotation(elementUtils, fieldInfo.fieldElement,
                    FieldReadOnly.class.getName());

            if (annotation != null) {
                if (templateContext.containsKey("readOnly")) {
                    throw new Exception("Problem processing FieldDefinition [" + modelClassName
                            + "]: it should have only one @FieldReadOnly");
                }

                if (!fieldInfo.fieldElement.asType().getKind().equals(TypeKind.BOOLEAN)
                        && !fieldInfo.fieldElement.asType().toString().equals(Boolean.class.getName())) {
                    throw new Exception("Problem processing FieldDefinition [" + modelClassName
                            + "]: field marked as @FieldReadOnly must be boolean or Boolean");
                }

                if (fieldInfo.getter == null) {
                    throw new Exception("Problem processing FieldDefinition [" + modelClassName
                            + "]: field marked as @FieldReadOnly should have getter");
                }

                templateContext.put("readOnly", fieldInfo.getter);
            }

            annotation = GeneratorUtils.getAnnotation(elementUtils, fieldInfo.fieldElement,
                    FieldRequired.class.getName());

            if (annotation != null) {
                if (templateContext.containsKey("required")) {
                    throw new Exception("Problem processing FieldDefinition [" + modelClassName
                            + "]: it should have only one @FieldRequired");
                }

                if (!fieldInfo.fieldElement.asType().getKind().equals(TypeKind.BOOLEAN)
                        && !fieldInfo.fieldElement.asType().toString().equals(Boolean.class.getName())) {
                    throw new Exception("Problem processing FieldDefinition [" + modelClassName
                            + "]: field marked as @FieldRequired must be boolean or Boolean");
                }

                if (fieldInfo.getter == null) {
                    throw new Exception("Problem processing FieldDefinition [" + modelClassName
                            + "]: field marked as @FieldRequired should have getter");
                }

                templateContext.put("required", fieldInfo.getter);
            }

            if (fieldDefinitionAnnotation.labelMode().equals(LabelMode.OVERRIDE)) {

                annotation = GeneratorUtils.getAnnotation(elementUtils, fieldInfo.fieldElement,
                        FieldLabel.class.getName());

                if (annotation != null) {
                    if (templateContext.containsKey("label")) {
                        throw new Exception("Problem processing FieldDefinition [" + modelClassName
                                + "]: it should have only one @FieldLabel");
                    }

                    if (!fieldInfo.fieldElement.asType().toString().equals(String.class.getName())) {
                        throw new Exception("Problem processing FieldDefinition [" + modelClassName
                                + "]: field marked as @FieldLabel must be a String");
                    }

                    if (fieldInfo.getter == null) {
                        throw new Exception("Problem processing FieldDefinition [" + modelClassName
                                + "]: field marked as @FieldLabel should have getter");
                    }
                    templateContext.put("label", fieldInfo.getter);
                }
            }
        }
    }

    StringBuffer source = writeTemplate("templates/FieldDefinitionModifier.ftl", templateContext);

    fieldDefinition.put("sourceCode", source.toString());

    context.getFieldDefinitions().add(fieldDefinition);
}

From source file:com.rgeldmacher.leash.LeashAnnotationProcessor.java

private boolean typeIsPrimitive(TypeMirror type) {
    TypeKind kind = type.getKind();
    return kind == TypeKind.BOOLEAN || kind == TypeKind.BYTE || kind == TypeKind.CHAR || kind == TypeKind.DOUBLE
            || kind == TypeKind.FLOAT || kind == TypeKind.INT || kind == TypeKind.LONG
            || kind == TypeKind.SHORT;
}

From source file:com.rgeldmacher.leash.LeashAnnotationProcessor.java

private String getPrimitiveDefault(TypeMirror typeMirror) {
    TypeKind kind = typeMirror.getKind();
    if (kind == TypeKind.BOOLEAN) {
        return "false";
    } else if (kind == TypeKind.BYTE) {
        return "0";
    } else if (kind == TypeKind.CHAR) {
        return "'\\u0000'";
    } else if (kind == TypeKind.DOUBLE) {
        return "0.0d";
    } else if (kind == TypeKind.FLOAT) {
        return "0.0f";
    } else if (kind == TypeKind.INT) {
        return "0";
    } else if (kind == TypeKind.LONG) {
        return "0L";
    } else if (kind == TypeKind.SHORT) {
        return "0";
    }/*www.  ja  v  a2 s  .c o m*/

    return "";
}

From source file:com.googlecode.androidannotations.helper.ValidatorHelper.java

public void returnTypeIsVoidOrBoolean(ExecutableElement executableElement, IsValid valid) {
    TypeMirror returnType = executableElement.getReturnType();

    TypeKind returnKind = returnType.getKind();

    if (returnKind != TypeKind.BOOLEAN && returnKind != TypeKind.VOID
            && !returnType.toString().equals("java.lang.Boolean")) {
        valid.invalidate();//from   w  ww .  ja v a2s  .c  om
        annotationHelper.printAnnotationError(executableElement,
                "%s can only be used on a method with a boolean or a void return type");
    }
}

From source file:org.androidannotations.helper.ValidatorHelper.java

public void returnTypeIsVoidOrBoolean(ExecutableElement executableElement, IsValid valid) {
    TypeMirror returnType = executableElement.getReturnType();

    TypeKind returnKind = returnType.getKind();

    if (returnKind != TypeKind.BOOLEAN && returnKind != TypeKind.VOID
            && !returnType.toString().equals(CanonicalNameConstants.BOOLEAN)) {
        valid.invalidate();/* w  ww .j  a v a  2s .  c o m*/
        annotationHelper.printAnnotationError(executableElement,
                "%s can only be used on a method with a boolean or a void return type");
    }
}

From source file:org.jsweet.transpiler.util.Util.java

/**
 * Returns the literal for a given type inital value.
 *///  w  ww .j a v  a2 s .  c  om
public static String getTypeInitialValue(Type type) {
    if (type == null) {
        return "null";
    }
    if (isNumber(type)) {
        return "0";
    } else if (type.getKind() == TypeKind.BOOLEAN) {
        return "false";
    } else if (type.getKind() == TypeKind.VOID) {
        return null;
    } else {
        return "null";
    }
}

From source file:org.kie.workbench.common.forms.adf.processors.FormDefinitionsProcessor.java

private boolean isBooleanGetter(final ExecutableElement method) {
    String name = method.getSimpleName().toString();

    if (!method.getReturnType().getKind().equals(TypeKind.BOOLEAN)) {
        return false;
    }//  w w w .j a  va 2 s. com

    int parameterCount = method.getParameters().size();
    if (parameterCount != 0) {
        return false;
    }
    return (name.length() > 2 && name.startsWith("is"));
}