Java Code Examples for javax.lang.model.type.TypeKind#isPrimitive()

The following examples show how to use javax.lang.model.type.TypeKind#isPrimitive() . 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: TypeBindingFilter.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
void filter( Set<TypeElement> set ) {
    super.filter(set);
    if ( set.size() == 0 ){
        return;
    }
    TypeKind kind = getType().getKind();
    if ( kind == TypeKind.DECLARED ){
        filterDeclaredTypes(set);
    }
    else if ( kind.isPrimitive()  ){
        WebBeansModelProviderImpl.LOGGER.fine("Variable element " +
                mySimpleName+ " " +
                "couldn't have type as eligible for inection becuase its " +
                "type is primitive. It is unproxyable bean types"); // NOI18N
        set.clear();
    }
    else if ( kind == TypeKind.ARRAY ){
        WebBeansModelProviderImpl.LOGGER.fine("Variable element " +
                mySimpleName+ " " +
                "couldn't have type as eligible for inection becuase its " +
                "type has array type. It is unproxyable bean types");// NOI18N
        set.clear();
    }
}
 
Example 2
Source File: TestTypeKind.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
static int testIsPrimitive() {
    int failures = 0;
    // The eight primitive types
    Set<TypeKind> primitives = EnumSet.of(BOOLEAN,  // 1
                                          BYTE,     // 2
                                          CHAR,     // 3
                                          DOUBLE,   // 4
                                          FLOAT,    // 5
                                          INT,      // 6
                                          LONG,     // 7
                                          SHORT);   // 8

    for(TypeKind tk : TypeKind.values()) {
        boolean primitiveness;
        if ((primitiveness=tk.isPrimitive()) != primitives.contains(tk) ) {
            failures++;
            System.err.println("Unexpected isPrimitive value " + primitiveness +
                               "for " + tk);
        }
    }
    return failures;
}
 
Example 3
Source File: ColumnValidationReader.java    From droitatedDB with Apache License 2.0 6 votes vote down vote up
private String getValidatorClass(AnnotationMirror mirror, Element element) {
    Map<? extends ExecutableElement, ? extends AnnotationValue> elementValues = mirror.getElementValues();
    for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : elementValues.entrySet()) {
        if (entry.getKey().getSimpleName().toString().equals(VALIDATOR_VALUE_METHOD_NAME)) {
            AnnotationValue value = entry.getValue();
            TypeElement acceptedType = value.accept(new ValidatorAllowedTypeRetrievalVisitor(column, messager), null);
            TypeElement actualType = column.accept(new ColumnDeclaredTypeRetrievalVisitor(), null);
            TypeKind columnTypeKind = column.asType().getKind();

            if (columnTypeKind.isPrimitive() && compareTypeWithPrimitive(acceptedType, columnTypeKind)) {
                return value.getValue().toString();
            } else if (!columnTypeKind.isPrimitive() && compareTypeWithDeclared(acceptedType, actualType)) {
                return value.getValue().toString();
            } else {
                messager.printMessage(Diagnostic.Kind.ERROR, "The validator annotation " + element.toString() + " is not allowed on this type of column. Accepted is " + acceptedType.toString(), column);
                return null;

            }
        }
    }
    messager.printMessage(Diagnostic.Kind.ERROR, "No value set for Validator annotation", mirror.getAnnotationType().asElement());
    return null;
}
 
Example 4
Source File: TestTypeKind.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
static int testIsPrimitive() {
    int failures = 0;
    // The eight primitive types
    Set<TypeKind> primitives = EnumSet.of(BOOLEAN,  // 1
                                          BYTE,     // 2
                                          CHAR,     // 3
                                          DOUBLE,   // 4
                                          FLOAT,    // 5
                                          INT,      // 6
                                          LONG,     // 7
                                          SHORT);   // 8

    for(TypeKind tk : TypeKind.values()) {
        boolean primitiveness;
        if ((primitiveness=tk.isPrimitive()) != primitives.contains(tk) ) {
            failures++;
            System.err.println("Unexpected isPrimitive value " + primitiveness +
                               "for " + tk);
        }
    }
    return failures;
}
 
Example 5
Source File: TestTypeKind.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
static int testIsPrimitive() {
    int failures = 0;
    // The eight primitive types
    Set<TypeKind> primitives = EnumSet.of(BOOLEAN,  // 1
                                          BYTE,     // 2
                                          CHAR,     // 3
                                          DOUBLE,   // 4
                                          FLOAT,    // 5
                                          INT,      // 6
                                          LONG,     // 7
                                          SHORT);   // 8

    for(TypeKind tk : TypeKind.values()) {
        boolean primitiveness;
        if ((primitiveness=tk.isPrimitive()) != primitives.contains(tk) ) {
            failures++;
            System.err.println("Unexpected isPrimitive value " + primitiveness +
                               "for " + tk);
        }
    }
    return failures;
}
 
Example 6
Source File: SourceModeler.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private Collection<TypeMirror> getBoxedPrimitives( 
        CompilationController controller)
{
    TypeKind[] values = TypeKind.values();
    Collection<TypeMirror> result = new ArrayList<TypeMirror>( values.length);
    for (TypeKind typeKind : values) {
        if ( typeKind.isPrimitive() ){
            PrimitiveType primitiveType = 
                controller.getTypes().getPrimitiveType(typeKind);
            TypeElement boxedClass = controller.getTypes().
                boxedClass(primitiveType);
            result.add( boxedClass.asType() );
        }
    }
    return result;
}
 
Example 7
Source File: TestTypeKind.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
static int testIsPrimitive() {
    int failures = 0;
    // The eight primitive types
    Set<TypeKind> primitives = EnumSet.of(BOOLEAN,  // 1
                                          BYTE,     // 2
                                          CHAR,     // 3
                                          DOUBLE,   // 4
                                          FLOAT,    // 5
                                          INT,      // 6
                                          LONG,     // 7
                                          SHORT);   // 8

    for(TypeKind tk : TypeKind.values()) {
        boolean primitiveness;
        if ((primitiveness=tk.isPrimitive()) != primitives.contains(tk) ) {
            failures++;
            System.err.println("Unexpected isPrimitive value " + primitiveness +
                               "for " + tk);
        }
    }
    return failures;
}
 
Example 8
Source File: TestTypeKind.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
static int testIsPrimitive() {
    int failures = 0;
    // The eight primitive types
    Set<TypeKind> primitives = EnumSet.of(BOOLEAN,  // 1
                                          BYTE,     // 2
                                          CHAR,     // 3
                                          DOUBLE,   // 4
                                          FLOAT,    // 5
                                          INT,      // 6
                                          LONG,     // 7
                                          SHORT);   // 8

    for(TypeKind tk : TypeKind.values()) {
        boolean primitiveness;
        if ((primitiveness=tk.isPrimitive()) != primitives.contains(tk) ) {
            failures++;
            System.err.println("Unexpected isPrimitive value " + primitiveness +
                               "for " + tk);
        }
    }
    return failures;
}
 
Example 9
Source File: ELTypeUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the return type of the given {@code method}.
 * @param method
 * @return
 */
public static TypeMirror getReturnType(CompilationContext info, final ExecutableElement method, ELElement elElement, List<Node> rootToNode) {
    TypeKind returnTypeKind = method.getReturnType().getKind();
    if (returnTypeKind.isPrimitive()) {
        return info.info().getTypes().getPrimitiveType(returnTypeKind);
    } else if (returnTypeKind == TypeKind.VOID) {
        return info.info().getTypes().getNoType(returnTypeKind);
    } else if (returnTypeKind == TypeKind.TYPEVAR && rootToNode != null && elElement != null) {
        return getReturnTypeForGenericClass(info, method, elElement, rootToNode);
    } else {
        return method.getReturnType();
    }
}
 
Example 10
Source File: TreeBackedTypesTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void testIsSameTypePrimitiveType() throws IOException {
  initCompiler();

  for (TypeKind typeKind : TypeKind.values()) {
    if (typeKind.isPrimitive()) {
      PrimitiveType primitiveType = types.getPrimitiveType(typeKind);
      PrimitiveType primitiveType2 = types.getPrimitiveType(typeKind);

      assertSameType(primitiveType, primitiveType2);
    }
  }
}
 
Example 11
Source File: ReturnTypeChanged.java    From revapi with Apache License 2.0 5 votes vote down vote up
private static boolean isPrimitiveOrVoid(TypeMirror type) {
    TypeKind kind = type.getKind();
    switch (kind) {
        case VOID:
            return true;
        case ARRAY:
            return isPrimitiveOrVoid(((ArrayType) type).getComponentType());
        default:
            return kind.isPrimitive();
    }
}
 
Example 12
Source File: Imports.java    From immutables with Apache License 2.0 5 votes vote down vote up
private void collectBuiltins(Map<String, TypeMirror> collected) {
  for (TypeKind kind : TypeKind.values()) {
    if (kind.isPrimitive()) {
      TypeElement boxedClass = types.boxedClass(types.getPrimitiveType(kind));
      collected.put(boxedClass.getSimpleName().toString(), boxedClass.asType());
    }
  }

  TypeElement typeElement = elements.getTypeElement(String.class.getCanonicalName());
  collected.put(typeElement.getSimpleName().toString(), typeElement.asType());

  typeElement = elements.getTypeElement(Templates.Invokable.class.getCanonicalName());
  collected.put(typeElement.getSimpleName().toString(), typeElement.asType());
}
 
Example 13
Source File: APHotSpotSignature.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static TypeMirror lookupType(ProcessingEnvironment env, String binaryName) {
    if (binaryName.length() == 1) {
        TypeKind kind = fromPrimitiveOrVoidTypeChar(binaryName.charAt(0));
        if (kind.isPrimitive()) {
            return env.getTypeUtils().getPrimitiveType(kind);
        } else if (kind == TypeKind.VOID) {
            return env.getTypeUtils().getNoType(kind);
        }
    }

    String canonicalName = binaryName;
    if (canonicalName.startsWith("L") && canonicalName.endsWith(";")) {
        canonicalName = canonicalName.substring(1, canonicalName.length() - 1);
    }
    env.getMessager().printMessage(Kind.ERROR, canonicalName);

    int arrayDims = 0;
    while (canonicalName.startsWith("[")) {
        canonicalName = canonicalName.substring(1, canonicalName.length());
        arrayDims++;
    }

    canonicalName = canonicalName.replaceAll("/", ".");
    TypeElement typeElement = env.getElementUtils().getTypeElement(canonicalName);
    if (typeElement == null) {
        throw new RuntimeException(String.format("Type with name %s not found.", canonicalName));
    }
    TypeMirror mirror = typeElement.asType();
    for (int i = 0; i < arrayDims; i++) {
        mirror = env.getTypeUtils().getArrayType(mirror);
    }
    return mirror;
}
 
Example 14
Source File: ProcessorUtils.java    From jackdaw with Apache License 2.0 5 votes vote down vote up
public static TypeElement getWrappedType(final TypeMirror mirror) {
    final ProcessingEnvironment env = ProcessorContextHolder.getProcessingEnvironment();
    final Types typeUtils = env.getTypeUtils();

    final TypeKind kind = mirror.getKind();
    final boolean primitive = kind.isPrimitive();

    if (primitive) {
        return typeUtils.boxedClass((PrimitiveType) mirror);
    }
    return (TypeElement) typeUtils.asElement(mirror);
}
 
Example 15
Source File: ProducesMethodValidator.java    From dagger2-sample with Apache License 2.0 5 votes vote down vote up
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 16
Source File: HashCodeMethod.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
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 17
Source File: TurbineTypesFactoryTest.java    From turbine with Apache License 2.0 5 votes vote down vote up
@Test
public void primitiveTypes() {
  for (TypeKind kind : TypeKind.values()) {
    if (kind.isPrimitive()) {
      PrimitiveType type = turbineTypes.getPrimitiveType(kind);
      assertThat(type.getKind()).isEqualTo(kind);
    } else {
      try {
        turbineTypes.getPrimitiveType(kind);
        fail();
      } catch (IllegalArgumentException expected) {
      }
    }
  }
}
 
Example 18
Source File: TurbineTypeMirrorTest.java    From turbine with Apache License 2.0 5 votes vote down vote up
@Test
public void primitiveTypes() {
  for (TypeKind kind : TypeKind.values()) {
    if (!kind.isPrimitive()) {
      continue;
    }
    TurbineConstantTypeKind turbineKind = TurbineConstantTypeKind.valueOf(kind.name());
    TypeMirror type = factory.asTypeMirror(PrimTy.create(turbineKind, ImmutableList.of()));
    assertThat(type.getKind()).isEqualTo(kind);
  }
}
 
Example 19
Source File: ExpectedTypeResolver.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static boolean isPrimitiveType(TypeKind k) {
    return k.isPrimitive();
}
 
Example 20
Source File: TypeUtils.java    From ngAndroid with Apache License 2.0 4 votes vote down vote up
/**
 * rules for this method are found here
 *
 * http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2
 *
 * If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).
 * Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:
 * If either operand is of type double, the other is converted to double.
 * Otherwise, if either operand is of type float, the other is converted to float.
 * Otherwise, if either operand is of type long, the other is converted to long.
 * Otherwise, both operands are converted to type int.
 *
 * test this hardcore..
 *
 * @param leftMirror
 * @param rightMirror
 * @param operator
 * @return
 */
public TypeMirror getOperatorKind(TypeMirror leftMirror, TypeMirror rightMirror , TokenType.BinaryOperator operator){

    if(leftMirror == null || rightMirror == null)
        return null;

    TypeKind leftKind = leftMirror.getKind();
    TypeKind rightKind = rightMirror.getKind();

    if(isString(leftMirror))
        return leftMirror;
    if(isString(rightMirror))
        return rightMirror;

    if(either(leftKind, rightKind, TypeKind.BOOLEAN) ) {
        if(operator != null) {
            messageUtils.error(Option.<Element>absent(), "Cannot perform perform the operation '%s' with a boolean type. (%s %s %s)", operator.toString(), leftMirror, operator.toString(), rightMirror);
        }
        return null;
    }

    if(leftKind.isPrimitive() && rightKind.isPrimitive()) {
        if (typeUtils.isSameType(leftMirror, rightMirror))
            return typeUtils.getPrimitiveType(leftKind);

        if (either(leftKind, rightKind, TypeKind.DOUBLE))
            return typeUtils.getPrimitiveType(TypeKind.DOUBLE);

        if (either(leftKind, rightKind, TypeKind.FLOAT))
            return typeUtils.getPrimitiveType(TypeKind.FLOAT);

        if (either(leftKind, rightKind, TypeKind.LONG))
            return typeUtils.getPrimitiveType(TypeKind.LONG);

        if (
            either(leftKind, rightKind, TypeKind.INT) ||
            either(leftKind, rightKind, TypeKind.CHAR) ||
            either(leftKind, rightKind, TypeKind.SHORT) ||
            either(leftKind, rightKind, TypeKind.BYTE)
        ) {
            return typeUtils.getPrimitiveType(TypeKind.INT);
        }
    }
    TypeMirror intMirror = typeUtils.getPrimitiveType(TypeKind.INT);
    if(both(assignable(intMirror, leftMirror), assignable(intMirror, rightMirror)))
        return intMirror;

    TypeMirror longMirror = typeUtils.getPrimitiveType(TypeKind.LONG);
    if(both(assignable(longMirror, leftMirror), assignable(longMirror, rightMirror)))
        return longMirror;

    TypeMirror floatMirror = typeUtils.getPrimitiveType(TypeKind.FLOAT);
    if(both(assignable(floatMirror, leftMirror), assignable(floatMirror, rightMirror)))
        return floatMirror;

    TypeMirror doubleMirror = typeUtils.getPrimitiveType(TypeKind.DOUBLE);
    if(both(assignable(doubleMirror, leftMirror), assignable(doubleMirror, rightMirror)))
        return doubleMirror;

    return null;
}