Java Code Examples for javax.lang.model.type.TypeKind#equals()
The following examples show how to use
javax.lang.model.type.TypeKind#equals() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HashCodeMethod.java From firebase-android-sdk with Apache License 2.0 | 5 votes |
private static CodeBlock hashExpression(ExecutableElement method) { TypeKind returnKind = method.getReturnType().getKind(); if (returnKind.isPrimitive()) { if (returnKind.equals(TypeKind.FLOAT)) { return CodeBlock.builder() .add("(Float.floatToIntBits($L))", method.getSimpleName()) .build(); } if (returnKind.equals(TypeKind.DOUBLE)) { return CodeBlock.builder() .add( "((int) ((Double.doubleToLongBits($1L) >>> 32) ^ Double.doubleToLongBits($1L)))", method.getSimpleName()) .build(); } if (returnKind.equals(TypeKind.BOOLEAN)) { return CodeBlock.builder().add("($L ? 1231 : 1237)", method.getSimpleName()).build(); } if (returnKind.equals(TypeKind.LONG)) { return CodeBlock.builder() .add("((int)($1L ^ ($1L >>> 32)))", method.getSimpleName()) .build(); } return CodeBlock.builder().add("((int)$L)", method.getSimpleName()).build(); } if (returnKind.equals(TypeKind.ARRAY)) { return CodeBlock.builder() .add("$T.hashCode($L)", Arrays.class, method.getSimpleName()) .build(); } return CodeBlock.builder().add("$L.hashCode()", method.getSimpleName()).build(); }
Example 2
Source File: ProducesMethodValidator.java From dagger2-sample with Apache License 2.0 | 5 votes |
private void validateKeyType(ValidationReport.Builder<? extends Element> reportBuilder, TypeMirror type) { TypeKind kind = type.getKind(); if (!(kind.isPrimitive() || kind.equals(DECLARED) || kind.equals(ARRAY))) { reportBuilder.addItem(PRODUCES_METHOD_RETURN_TYPE, reportBuilder.getSubject()); } }
Example 3
Source File: ProvidesMethodValidator.java From dagger2-sample with Apache License 2.0 | 5 votes |
private void validateKeyType(ValidationReport.Builder<? extends Element> reportBuilder, TypeMirror type) { TypeKind kind = type.getKind(); if (!(kind.isPrimitive() || kind.equals(DECLARED) || kind.equals(ARRAY))) { reportBuilder.addItem(PROVIDES_METHOD_RETURN_TYPE, reportBuilder.getSubject()); } }
Example 4
Source File: EqualsMethod.java From firebase-android-sdk with Apache License 2.0 | 4 votes |
static MethodSpec generate(TypeElement annotation) { MethodSpec.Builder equalsBuilder = MethodSpec.methodBuilder("equals") .addModifiers(Modifier.PUBLIC) .addAnnotation(Override.class) .returns(boolean.class) .addParameter(Object.class, "other"); equalsBuilder.addCode( CodeBlock.builder() .add("if (this == other) return true;\n") .add("if (!(other instanceof $T)) return false;\n", annotation) .add("$1T that = ($1T) other;\n\n", annotation) .build()); List<CodeBlock> equalExpressions = new ArrayList<>(); for (ExecutableElement method : ElementFilter.methodsIn(annotation.getEnclosedElements())) { TypeKind resultKind = method.getReturnType().getKind(); if (resultKind.isPrimitive()) { if (resultKind.equals(TypeKind.FLOAT)) { equalExpressions.add( CodeBlock.builder() .add( "(Float.floatToIntBits($1L) == Float.floatToIntBits(that.$1L()))", method.getSimpleName().toString()) .build()); continue; } if (resultKind.equals(TypeKind.DOUBLE)) { equalExpressions.add( CodeBlock.builder() .add( "(Double.doubleToLongBits($1L) == Double.doubleToLongBits(that.$1L()))", method.getSimpleName().toString()) .build()); continue; } equalExpressions.add( CodeBlock.builder() .add("($1L == that.$1L())", method.getSimpleName().toString()) .build()); } else { if (resultKind.equals(TypeKind.ARRAY)) { equalExpressions.add( CodeBlock.builder() .add( "($1T.equals($2L, that.$2L()))", Arrays.class, method.getSimpleName().toString()) .build()); } equalExpressions.add( CodeBlock.builder() .add("($1L.equals(that.$1L()))", method.getSimpleName().toString()) .build()); } } if (equalExpressions.isEmpty()) { equalsBuilder.addCode("return true;\n"); } else { equalsBuilder.addCode("return $L;\n", CodeBlock.join(equalExpressions, "\n && ")); } return equalsBuilder.build(); }
Example 5
Source File: TypeUtils.java From ngAndroid with Apache License 2.0 | 4 votes |
private boolean either(TypeKind left, TypeKind right, TypeKind compare){ return left.equals(compare) || right.equals(compare); }
Example 6
Source File: ProducesMethodValidator.java From dagger2-sample with Apache License 2.0 | 4 votes |
@Override public ValidationReport<ExecutableElement> validate(ExecutableElement producesMethodElement) { ValidationReport.Builder<ExecutableElement> builder = ValidationReport.Builder.about(producesMethodElement); Produces producesAnnotation = producesMethodElement.getAnnotation(Produces.class); checkArgument(producesAnnotation != null); Element enclosingElement = producesMethodElement.getEnclosingElement(); if (!isAnnotationPresent(enclosingElement, ProducerModule.class)) { builder.addItem(formatModuleErrorMessage(BINDING_METHOD_NOT_IN_MODULE), producesMethodElement); } if (!producesMethodElement.getTypeParameters().isEmpty()) { builder.addItem(formatErrorMessage(BINDING_METHOD_TYPE_PARAMETER), producesMethodElement); } Set<Modifier> modifiers = producesMethodElement.getModifiers(); if (modifiers.contains(PRIVATE)) { builder.addItem(formatErrorMessage(BINDING_METHOD_PRIVATE), producesMethodElement); } if (modifiers.contains(STATIC)) { // TODO(gak): why not? builder.addItem(formatErrorMessage(BINDING_METHOD_STATIC), producesMethodElement); } if (modifiers.contains(ABSTRACT)) { builder.addItem(formatErrorMessage(BINDING_METHOD_ABSTRACT), producesMethodElement); } TypeMirror returnType = producesMethodElement.getReturnType(); TypeKind returnTypeKind = returnType.getKind(); if (returnTypeKind.equals(VOID)) { builder.addItem(formatErrorMessage(BINDING_METHOD_MUST_RETURN_A_VALUE), producesMethodElement); } // check mapkey is right if (!producesAnnotation.type().equals(Produces.Type.MAP) && (getMapKeys(producesMethodElement) != null && !getMapKeys(producesMethodElement).isEmpty())) { builder.addItem(formatErrorMessage(BINDING_METHOD_NOT_MAP_HAS_MAP_KEY), producesMethodElement); } ProvidesMethodValidator.validateMethodQualifiers(builder, producesMethodElement); switch (producesAnnotation.type()) { case UNIQUE: // fall through case SET: validateSingleReturnType(builder, returnType); break; case MAP: validateSingleReturnType(builder, returnType); ImmutableSet<? extends AnnotationMirror> annotationMirrors = getMapKeys(producesMethodElement); switch (annotationMirrors.size()) { case 0: builder.addItem(formatErrorMessage(BINDING_METHOD_WITH_NO_MAP_KEY), producesMethodElement); break; case 1: break; default: builder.addItem(formatErrorMessage(BINDING_METHOD_WITH_MULTIPLE_MAP_KEY), producesMethodElement); break; } break; case SET_VALUES: if (returnTypeKind.equals(DECLARED) && MoreTypes.isTypeOf(ListenableFuture.class, returnType)) { DeclaredType declaredReturnType = MoreTypes.asDeclared(returnType); if (!declaredReturnType.getTypeArguments().isEmpty()) { validateSetType(builder, Iterables.getOnlyElement( declaredReturnType.getTypeArguments())); } } else { validateSetType(builder, returnType); } break; default: throw new AssertionError(); } return builder.build(); }
Example 7
Source File: ProvidesMethodValidator.java From dagger2-sample with Apache License 2.0 | 4 votes |
@Override public ValidationReport<ExecutableElement> validate(ExecutableElement providesMethodElement) { ValidationReport.Builder<ExecutableElement> builder = ValidationReport.Builder.about(providesMethodElement); Provides providesAnnotation = providesMethodElement.getAnnotation(Provides.class); checkArgument(providesAnnotation != null); Element enclosingElement = providesMethodElement.getEnclosingElement(); if (!isAnnotationPresent(enclosingElement, Module.class)) { builder.addItem(formatModuleErrorMessage(BINDING_METHOD_NOT_IN_MODULE), providesMethodElement); } if (!providesMethodElement.getTypeParameters().isEmpty()) { builder.addItem(formatErrorMessage(BINDING_METHOD_TYPE_PARAMETER), providesMethodElement); } Set<Modifier> modifiers = providesMethodElement.getModifiers(); if (modifiers.contains(PRIVATE)) { builder.addItem(formatErrorMessage(BINDING_METHOD_PRIVATE), providesMethodElement); } if (modifiers.contains(STATIC)) { // TODO(gak): why not? builder.addItem(formatErrorMessage(BINDING_METHOD_STATIC), providesMethodElement); } if (modifiers.contains(ABSTRACT)) { builder.addItem(formatErrorMessage(BINDING_METHOD_ABSTRACT), providesMethodElement); } TypeMirror returnType = providesMethodElement.getReturnType(); TypeKind returnTypeKind = returnType.getKind(); if (returnTypeKind.equals(VOID)) { builder.addItem(formatErrorMessage(BINDING_METHOD_MUST_RETURN_A_VALUE), providesMethodElement); } // check mapkey is right if (!providesAnnotation.type().equals(Provides.Type.MAP) && (getMapKeys(providesMethodElement) != null && getMapKeys(providesMethodElement).size() > 0)) { builder.addItem(formatErrorMessage(BINDING_METHOD_NOT_MAP_HAS_MAP_KEY), providesMethodElement); } validateMethodQualifiers(builder, providesMethodElement); switch (providesAnnotation.type()) { case UNIQUE: // fall through case SET: validateKeyType(builder, returnType); break; case MAP: validateKeyType(builder, returnType); ImmutableSet<? extends AnnotationMirror> annotationMirrors = getMapKeys(providesMethodElement); switch (annotationMirrors.size()) { case 0: builder.addItem(formatErrorMessage(BINDING_METHOD_WITH_NO_MAP_KEY), providesMethodElement); break; case 1: break; default: builder.addItem(formatErrorMessage(BINDING_METHOD_WITH_MULTIPLE_MAP_KEY), providesMethodElement); break; } break; case SET_VALUES: if (!returnTypeKind.equals(DECLARED)) { builder.addItem(PROVIDES_METHOD_SET_VALUES_RETURN_SET, providesMethodElement); } else { DeclaredType declaredReturnType = (DeclaredType) returnType; // TODO(gak): should we allow "covariant return" for set values? if (!declaredReturnType.asElement().equals(getSetElement())) { builder.addItem(PROVIDES_METHOD_SET_VALUES_RETURN_SET, providesMethodElement); } else if (declaredReturnType.getTypeArguments().isEmpty()) { builder.addItem(formatErrorMessage(BINDING_METHOD_SET_VALUES_RAW_SET), providesMethodElement); } else { validateKeyType(builder, Iterables.getOnlyElement(declaredReturnType.getTypeArguments())); } } break; default: throw new AssertionError(); } return builder.build(); }