Search in sources :

Example 11 with PrimitiveType

use of javax.lang.model.type.PrimitiveType in project checker-framework by typetools.

the class CFGTranslationPhaseOne method box.

/**
 * If the input node is an unboxed primitive type, insert a call to the appropriate valueOf
 * method, otherwise leave it alone.
 *
 * @param node in input node
 * @return a Node representing the boxed version of the input, which may simply be the input node
 */
protected Node box(Node node) {
    // For boxing conversion, see JLS 5.1.7
    if (TypesUtils.isPrimitive(node.getType())) {
        PrimitiveType primitive = types.getPrimitiveType(node.getType().getKind());
        TypeMirror boxedType = types.getDeclaredType(types.boxedClass(primitive));
        TypeElement boxedElement = (TypeElement) ((DeclaredType) boxedType).asElement();
        IdentifierTree classTree = treeBuilder.buildClassUse(boxedElement);
        handleArtificialTree(classTree);
        // No need to handle possible errors from evaluating a class literal here
        // since this is synthetic code that can't fail.
        ClassNameNode className = new ClassNameNode(classTree);
        className.setInSource(false);
        insertNodeAfter(className, node);
        MemberSelectTree valueOfSelect = treeBuilder.buildValueOfMethodAccess(classTree);
        handleArtificialTree(valueOfSelect);
        MethodAccessNode valueOfAccess = new MethodAccessNode(valueOfSelect, className);
        valueOfAccess.setInSource(false);
        insertNodeAfter(valueOfAccess, className);
        MethodInvocationTree valueOfCall = treeBuilder.buildMethodInvocation(valueOfSelect, (ExpressionTree) node.getTree());
        handleArtificialTree(valueOfCall);
        Node boxed = new MethodInvocationNode(valueOfCall, valueOfAccess, Collections.singletonList(node), getCurrentPath());
        boxed.setInSource(false);
        // Add Throwable to account for unchecked exceptions
        addToConvertedLookupMap(node.getTree(), boxed);
        insertNodeWithExceptionsAfter(boxed, uncheckedExceptionTypes, valueOfAccess);
        return boxed;
    } else {
        return node;
    }
}
Also used : MethodAccessNode(org.checkerframework.dataflow.cfg.node.MethodAccessNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) NumericalMultiplicationNode(org.checkerframework.dataflow.cfg.node.NumericalMultiplicationNode) ArrayCreationNode(org.checkerframework.dataflow.cfg.node.ArrayCreationNode) ValueLiteralNode(org.checkerframework.dataflow.cfg.node.ValueLiteralNode) StringConversionNode(org.checkerframework.dataflow.cfg.node.StringConversionNode) UnsignedRightShiftNode(org.checkerframework.dataflow.cfg.node.UnsignedRightShiftNode) LeftShiftNode(org.checkerframework.dataflow.cfg.node.LeftShiftNode) PrimitiveTypeNode(org.checkerframework.dataflow.cfg.node.PrimitiveTypeNode) FloatLiteralNode(org.checkerframework.dataflow.cfg.node.FloatLiteralNode) LessThanNode(org.checkerframework.dataflow.cfg.node.LessThanNode) BitwiseOrNode(org.checkerframework.dataflow.cfg.node.BitwiseOrNode) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode) NarrowingConversionNode(org.checkerframework.dataflow.cfg.node.NarrowingConversionNode) EqualToNode(org.checkerframework.dataflow.cfg.node.EqualToNode) NumericalPlusNode(org.checkerframework.dataflow.cfg.node.NumericalPlusNode) ConditionalAndNode(org.checkerframework.dataflow.cfg.node.ConditionalAndNode) VariableDeclarationNode(org.checkerframework.dataflow.cfg.node.VariableDeclarationNode) ClassDeclarationNode(org.checkerframework.dataflow.cfg.node.ClassDeclarationNode) IntegerDivisionNode(org.checkerframework.dataflow.cfg.node.IntegerDivisionNode) AssertionErrorNode(org.checkerframework.dataflow.cfg.node.AssertionErrorNode) InstanceOfNode(org.checkerframework.dataflow.cfg.node.InstanceOfNode) BooleanLiteralNode(org.checkerframework.dataflow.cfg.node.BooleanLiteralNode) ThisNode(org.checkerframework.dataflow.cfg.node.ThisNode) AssignmentNode(org.checkerframework.dataflow.cfg.node.AssignmentNode) NullLiteralNode(org.checkerframework.dataflow.cfg.node.NullLiteralNode) ArrayTypeNode(org.checkerframework.dataflow.cfg.node.ArrayTypeNode) LambdaResultExpressionNode(org.checkerframework.dataflow.cfg.node.LambdaResultExpressionNode) IntegerRemainderNode(org.checkerframework.dataflow.cfg.node.IntegerRemainderNode) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) ConditionalOrNode(org.checkerframework.dataflow.cfg.node.ConditionalOrNode) NotEqualNode(org.checkerframework.dataflow.cfg.node.NotEqualNode) BitwiseXorNode(org.checkerframework.dataflow.cfg.node.BitwiseXorNode) ArrayAccessNode(org.checkerframework.dataflow.cfg.node.ArrayAccessNode) ExplicitThisNode(org.checkerframework.dataflow.cfg.node.ExplicitThisNode) StringConcatenateNode(org.checkerframework.dataflow.cfg.node.StringConcatenateNode) NullChkNode(org.checkerframework.dataflow.cfg.node.NullChkNode) CharacterLiteralNode(org.checkerframework.dataflow.cfg.node.CharacterLiteralNode) FloatingDivisionNode(org.checkerframework.dataflow.cfg.node.FloatingDivisionNode) FunctionalInterfaceNode(org.checkerframework.dataflow.cfg.node.FunctionalInterfaceNode) StringConcatenateAssignmentNode(org.checkerframework.dataflow.cfg.node.StringConcatenateAssignmentNode) TypeCastNode(org.checkerframework.dataflow.cfg.node.TypeCastNode) MethodAccessNode(org.checkerframework.dataflow.cfg.node.MethodAccessNode) WideningConversionNode(org.checkerframework.dataflow.cfg.node.WideningConversionNode) LongLiteralNode(org.checkerframework.dataflow.cfg.node.LongLiteralNode) MarkerNode(org.checkerframework.dataflow.cfg.node.MarkerNode) ImplicitThisNode(org.checkerframework.dataflow.cfg.node.ImplicitThisNode) FloatingRemainderNode(org.checkerframework.dataflow.cfg.node.FloatingRemainderNode) ClassNameNode(org.checkerframework.dataflow.cfg.node.ClassNameNode) ObjectCreationNode(org.checkerframework.dataflow.cfg.node.ObjectCreationNode) PackageNameNode(org.checkerframework.dataflow.cfg.node.PackageNameNode) DoubleLiteralNode(org.checkerframework.dataflow.cfg.node.DoubleLiteralNode) SuperNode(org.checkerframework.dataflow.cfg.node.SuperNode) IntegerLiteralNode(org.checkerframework.dataflow.cfg.node.IntegerLiteralNode) SignedRightShiftNode(org.checkerframework.dataflow.cfg.node.SignedRightShiftNode) ThrowNode(org.checkerframework.dataflow.cfg.node.ThrowNode) GreaterThanOrEqualNode(org.checkerframework.dataflow.cfg.node.GreaterThanOrEqualNode) StringLiteralNode(org.checkerframework.dataflow.cfg.node.StringLiteralNode) TernaryExpressionNode(org.checkerframework.dataflow.cfg.node.TernaryExpressionNode) BitwiseAndNode(org.checkerframework.dataflow.cfg.node.BitwiseAndNode) ParameterizedTypeNode(org.checkerframework.dataflow.cfg.node.ParameterizedTypeNode) CaseNode(org.checkerframework.dataflow.cfg.node.CaseNode) SwitchExpressionNode(org.checkerframework.dataflow.cfg.node.SwitchExpressionNode) NumericalAdditionNode(org.checkerframework.dataflow.cfg.node.NumericalAdditionNode) NumericalSubtractionNode(org.checkerframework.dataflow.cfg.node.NumericalSubtractionNode) BitwiseComplementNode(org.checkerframework.dataflow.cfg.node.BitwiseComplementNode) ConditionalNotNode(org.checkerframework.dataflow.cfg.node.ConditionalNotNode) NumericalMinusNode(org.checkerframework.dataflow.cfg.node.NumericalMinusNode) ReturnNode(org.checkerframework.dataflow.cfg.node.ReturnNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) GreaterThanNode(org.checkerframework.dataflow.cfg.node.GreaterThanNode) LessThanOrEqualNode(org.checkerframework.dataflow.cfg.node.LessThanOrEqualNode) SynchronizedNode(org.checkerframework.dataflow.cfg.node.SynchronizedNode) Node(org.checkerframework.dataflow.cfg.node.Node) IdentifierTree(com.sun.source.tree.IdentifierTree) ClassNameNode(org.checkerframework.dataflow.cfg.node.ClassNameNode) PrimitiveType(javax.lang.model.type.PrimitiveType)

Example 12 with PrimitiveType

use of javax.lang.model.type.PrimitiveType in project j2objc by google.

the class JdtTypes method unboxedType.

@Override
public PrimitiveType unboxedType(TypeMirror t) {
    if (t.getKind() == TypeKind.DECLARED) {
        TypeElement e = (TypeElement) ((DeclaredType) t).asElement();
        PrimitiveType result = unboxedTypes.get(e);
        if (result != null) {
            return result;
        }
    }
    throw new IllegalArgumentException("No unboxing convertion for: " + t);
}
Also used : TypeElement(javax.lang.model.element.TypeElement) PrimitiveType(javax.lang.model.type.PrimitiveType)

Example 13 with PrimitiveType

use of javax.lang.model.type.PrimitiveType in project buck by facebook.

the class TreeBackedTypesTest method testIsNotSameTypePrimitiveType.

@Test
public void testIsNotSameTypePrimitiveType() throws IOException {
    initCompiler();
    PrimitiveType intType = types.getPrimitiveType(TypeKind.INT);
    PrimitiveType longType = types.getPrimitiveType(TypeKind.LONG);
    assertNotSameType(intType, longType);
}
Also used : PrimitiveType(javax.lang.model.type.PrimitiveType) Test(org.junit.Test)

Example 14 with PrimitiveType

use of javax.lang.model.type.PrimitiveType in project RoboBinding by RoboBinding.

the class TypeMirrorWrapperTest method supportedTypeMirrors.

@DataPoints("supportedTypeMirrors")
public static TypeMirrorToWrapped[] supportedTypeMirrors() {
    Types types = compilation.getTypes();
    NoType voidType = types.getNoType(TypeKind.VOID);
    PrimitiveType primitiveType = types.getPrimitiveType(TypeKind.INT);
    Elements elements = compilation.getElements();
    DeclaredType declaredType = (DeclaredType) elements.getTypeElement(Object.class.getName()).asType();
    ArrayType arrayType = types.getArrayType(declaredType);
    return new TypeMirrorToWrapped[] { a(voidType).itsWrapped(WrappedVoidType.class), a(primitiveType).itsWrapped(WrappedPrimitiveType.class), a(declaredType).itsWrapped(WrappedDeclaredType.class), a(arrayType).itsWrapped(WrappedArrayType.class) };
}
Also used : ArrayType(javax.lang.model.type.ArrayType) Types(javax.lang.model.util.Types) NoType(javax.lang.model.type.NoType) PrimitiveType(javax.lang.model.type.PrimitiveType) Elements(javax.lang.model.util.Elements) DeclaredType(javax.lang.model.type.DeclaredType) FromDataPoints(org.junit.experimental.theories.FromDataPoints) DataPoints(org.junit.experimental.theories.DataPoints)

Example 15 with PrimitiveType

use of javax.lang.model.type.PrimitiveType in project auto by google.

the class MoreTypesTest method testIsTypeOf.

@Test
public void testIsTypeOf() {
    Types types = compilationRule.getTypes();
    PrimitiveType intType = types.getPrimitiveType(TypeKind.INT);
    TypeMirror integerType = types.boxedClass(intType).asType();
    WildcardType wildcardType = types.getWildcardType(null, null);
    expect.that(MoreTypes.isTypeOf(int.class, intType)).isTrue();
    expect.that(MoreTypes.isTypeOf(Integer.class, integerType)).isTrue();
    expect.that(MoreTypes.isTypeOf(Integer.class, intType)).isFalse();
    expect.that(MoreTypes.isTypeOf(int.class, integerType)).isFalse();
    expect.that(MoreTypes.isTypeOf(Integer.class, FAKE_ERROR_TYPE)).isFalse();
    assertThrows(IllegalArgumentException.class, () -> MoreTypes.isTypeOf(Integer.class, wildcardType));
}
Also used : Types(javax.lang.model.util.Types) WildcardType(javax.lang.model.type.WildcardType) TypeMirror(javax.lang.model.type.TypeMirror) PrimitiveType(javax.lang.model.type.PrimitiveType) Test(org.junit.Test)

Aggregations

PrimitiveType (javax.lang.model.type.PrimitiveType)23 TypeMirror (javax.lang.model.type.TypeMirror)11 TypeElement (javax.lang.model.element.TypeElement)9 DeclaredType (javax.lang.model.type.DeclaredType)6 ArrayType (javax.lang.model.type.ArrayType)5 Test (org.junit.Test)5 WildcardType (javax.lang.model.type.WildcardType)4 Types (javax.lang.model.util.Types)4 Map (java.util.Map)3 TypeKind (javax.lang.model.type.TypeKind)3 Set (java.util.Set)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 Elements (javax.lang.model.util.Elements)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 CastExpression (com.google.devtools.j2objc.ast.CastExpression)1 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)1 Expression (com.google.devtools.j2objc.ast.Expression)1 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)1 MethodInvocation (com.google.devtools.j2objc.ast.MethodInvocation)1