Search in sources :

Example 1 with Types

use of javax.lang.model.util.Types in project RoboBinding by RoboBinding.

the class TypeMirrorWrapperTest method unsupportedTypeMirrors.

@DataPoints("unsupportedTypeMirrors")
public static TypeMirror[] unsupportedTypeMirrors() {
    Types types = compilation.getTypes();
    NullType nullType = types.getNullType();
    NoType noneType = types.getNoType(TypeKind.NONE);
    return new TypeMirror[] { nullType, noneType };
}
Also used : Types(javax.lang.model.util.Types) NoType(javax.lang.model.type.NoType) TypeMirror(javax.lang.model.type.TypeMirror) NullType(javax.lang.model.type.NullType) FromDataPoints(org.junit.experimental.theories.FromDataPoints) DataPoints(org.junit.experimental.theories.DataPoints)

Example 2 with Types

use of javax.lang.model.util.Types in project realm-java by realm.

the class RealmProxyClassGenerator method emitPrimitiveType.

/**
     * Primitives and boxed types
     */
private void emitPrimitiveType(JavaWriter writer, final VariableElement field, final String fieldName, String fieldTypeCanonicalName) throws IOException {
    final String realmType = Constants.JAVA_TO_REALM_TYPES.get(fieldTypeCanonicalName);
    // Getter
    writer.emitAnnotation("SuppressWarnings", "\"cast\"").beginMethod(fieldTypeCanonicalName, metadata.getInternalGetter(fieldName), EnumSet.of(Modifier.PUBLIC)).emitStatement("proxyState.getRealm$realm().checkIfValid()");
    // For String and bytes[], null value will be returned by JNI code. Try to save one JNI call here.
    if (metadata.isNullable(field) && !Utils.isString(field) && !Utils.isByteArray(field)) {
        writer.beginControlFlow("if (proxyState.getRow$realm().isNull(%s))", fieldIndexVariableReference(field)).emitStatement("return null").endControlFlow();
    }
    // For Boxed types, this should be the corresponding primitive types. Others remain the same.
    String castingBackType;
    if (Utils.isBoxedType(fieldTypeCanonicalName)) {
        Types typeUtils = processingEnvironment.getTypeUtils();
        castingBackType = typeUtils.unboxedType(field.asType()).toString();
    } else {
        castingBackType = fieldTypeCanonicalName;
    }
    writer.emitStatement("return (%s) proxyState.getRow$realm().get%s(%s)", castingBackType, realmType, fieldIndexVariableReference(field));
    writer.endMethod().emitEmptyLine();
    // Setter
    writer.beginMethod("void", metadata.getInternalSetter(fieldName), EnumSet.of(Modifier.PUBLIC), fieldTypeCanonicalName, "value");
    emitCodeForUnderConstruction(writer, metadata.isPrimaryKey(field), new CodeEmitter() {

        @Override
        public void emit(JavaWriter writer) throws IOException {
            // set value as default value
            writer.emitStatement("final Row row = proxyState.getRow$realm()");
            if (metadata.isNullable(field)) {
                writer.beginControlFlow("if (value == null)").emitStatement("row.getTable().setNull(%s, row.getIndex(), true)", fieldIndexVariableReference(field)).emitStatement("return").endControlFlow();
            } else if (!metadata.isNullable(field) && !Utils.isPrimitiveType(field)) {
                writer.beginControlFlow("if (value == null)").emitStatement(Constants.STATEMENT_EXCEPTION_ILLEGAL_NULL_VALUE, fieldName).endControlFlow();
            }
            writer.emitStatement("row.getTable().set%s(%s, row.getIndex(), value, true)", realmType, fieldIndexVariableReference(field));
            writer.emitStatement("return");
        }
    });
    writer.emitStatement("proxyState.getRealm$realm().checkIfValid()");
    // Compared with getter, null value won't trigger more native calls in setter which is relatively cheaper.
    if (metadata.isPrimaryKey(field)) {
        // Primary key is not allowed to be changed after object created.
        writer.emitStatement(Constants.STATEMENT_EXCEPTION_PRIMARY_KEY_CANNOT_BE_CHANGED, fieldName);
    } else {
        if (metadata.isNullable(field)) {
            writer.beginControlFlow("if (value == null)").emitStatement("proxyState.getRow$realm().setNull(%s)", fieldIndexVariableReference(field)).emitStatement("return").endControlFlow();
        } else if (!metadata.isNullable(field) && !Utils.isPrimitiveType(field)) {
            // Same reason, throw IAE earlier.
            writer.beginControlFlow("if (value == null)").emitStatement(Constants.STATEMENT_EXCEPTION_ILLEGAL_NULL_VALUE, fieldName).endControlFlow();
        }
        writer.emitStatement("proxyState.getRow$realm().set%s(%s, value)", realmType, fieldIndexVariableReference(field));
    }
    writer.endMethod();
}
Also used : Types(javax.lang.model.util.Types) JavaWriter(com.squareup.javawriter.JavaWriter) IOException(java.io.IOException)

Example 3 with Types

use of javax.lang.model.util.Types in project react4j by react4j.

the class ReactProcessor method determineRenderMethod.

private void determineRenderMethod(@Nonnull final TypeElement typeElement, @Nonnull final ComponentDescriptor descriptor) {
    /*
     * Get the render method that has been overridden by the typeElement, a parent class, or by a default
     * method method implemented by the typeElement or a parent class.
     */
    final ExecutableElement renderMethod = getComponentRenderMethod();
    final Elements elementUtils = processingEnv.getElementUtils();
    final Types typeUtils = processingEnv.getTypeUtils();
    final TypeElement componentType = elementUtils.getTypeElement(Constants.COMPONENT_CLASSNAME);
    final MethodDescriptor overriddenRenderMethod = // Get all methods on type parent classes, and default methods from interfaces
    ProcessorUtil.getMethods(typeElement, processingEnv.getTypeUtils()).stream().filter(m -> elementUtils.overrides(m, renderMethod, typeElement)).filter(m -> m.getEnclosingElement() != componentType).map(m -> new MethodDescriptor(m, (ExecutableType) typeUtils.asMemberOf(descriptor.getDeclaredType(), m))).findAny().orElse(null);
    if (null == overriddenRenderMethod) {
        throw new ReactProcessorException("The react component does not override any render methods.", typeElement);
    }
}
Also used : Arrays(java.util.Arrays) PackageElement(javax.lang.model.element.PackageElement) AbstractProcessor(javax.annotation.processing.AbstractProcessor) Modifier(javax.lang.model.element.Modifier) VariableElement(javax.lang.model.element.VariableElement) HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) Elements(javax.lang.model.util.Elements) ArrayList(java.util.ArrayList) SupportedSourceVersion(javax.annotation.processing.SupportedSourceVersion) Kind(javax.tools.Diagnostic.Kind) Map(java.util.Map) DeclaredType(javax.lang.model.type.DeclaredType) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) PrintWriter(java.io.PrintWriter) ElementKind(javax.lang.model.element.ElementKind) ExecutableType(javax.lang.model.type.ExecutableType) StringWriter(java.io.StringWriter) Collection(java.util.Collection) ExecutableElement(javax.lang.model.element.ExecutableElement) Set(java.util.Set) IOException(java.io.IOException) Element(javax.lang.model.element.Element) TypeSpec(com.squareup.javapoet.TypeSpec) Processor(javax.annotation.processing.Processor) Types(javax.lang.model.util.Types) Collectors(java.util.stream.Collectors) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeKind(javax.lang.model.type.TypeKind) JavaFile(com.squareup.javapoet.JavaFile) SourceVersion(javax.lang.model.SourceVersion) List(java.util.List) TypeMirror(javax.lang.model.type.TypeMirror) RoundEnvironment(javax.annotation.processing.RoundEnvironment) AutoService(com.google.auto.service.AutoService) ExecutableType(javax.lang.model.type.ExecutableType) SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) Types(javax.lang.model.util.Types) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Elements(javax.lang.model.util.Elements)

Example 4 with Types

use of javax.lang.model.util.Types in project checker-framework by typetools.

the class StructuralEqualityComparer method subtypeAndCompare.

/**
 * A temporary solution until we handle CaptureConversion, subtypeAndCompare handles cases in
 * which we encounter a captured type being compared against a non-captured type. The captured
 * type may have type arguments that are subtypes of the other type it is being compared to. In
 * these cases, we will convert the bounds via this method to the other type and then continue
 * on with the equality comparison. If neither of the type args can be converted to the other
 * than we just compare the effective annotations on the two types for equality.
 */
boolean subtypeAndCompare(final AnnotatedTypeMirror type1, final AnnotatedTypeMirror type2, final VisitHistory visited) {
    final Types types = type1.atypeFactory.types;
    final AnnotatedTypeMirror t1;
    final AnnotatedTypeMirror t2;
    if (type1.getKind() == TypeKind.NULL && type2.getKind() == TypeKind.NULL) {
        return areEqual(type1, type2);
    }
    if (type1.getKind() == TypeKind.NULL || type2.getKind() == TypeKind.NULL) {
        t1 = type1;
        t2 = type2;
    } else if (types.isSubtype(type2.getUnderlyingType(), type1.getUnderlyingType())) {
        t1 = type1;
        t2 = AnnotatedTypes.asSuper(type1.atypeFactory, type2, type1);
    } else if (types.isSubtype(type1.getUnderlyingType(), type2.getUnderlyingType())) {
        t1 = AnnotatedTypes.asSuper(type1.atypeFactory, type1, type2);
        t2 = type2;
    } else {
        t1 = null;
        t2 = null;
    }
    if (t1 == null || t2 == null) {
        final QualifierHierarchy qualifierHierarchy = type1.atypeFactory.getQualifierHierarchy();
        if (currentTop == null) {
            return AnnotationUtils.areSame(AnnotatedTypes.findEffectiveAnnotations(qualifierHierarchy, type1), AnnotatedTypes.findEffectiveAnnotations(qualifierHierarchy, type2));
        } else {
            return AnnotationUtils.areSame(AnnotatedTypes.findEffectiveAnnotationInHierarchy(qualifierHierarchy, type1, currentTop), AnnotatedTypes.findEffectiveAnnotationInHierarchy(qualifierHierarchy, type2, currentTop));
        }
    }
    return areEqual(t1, t2, visited);
}
Also used : Types(javax.lang.model.util.Types) AnnotatedTypes(org.checkerframework.framework.util.AnnotatedTypes)

Example 5 with Types

use of javax.lang.model.util.Types in project checker-framework by typetools.

the class FlowExpressionParseUtil method parseHelper.

private static FlowExpressions.Receiver parseHelper(String expression, FlowExpressionContext context, TreePath path) throws FlowExpressionParseException {
    expression = expression.trim();
    ProcessingEnvironment env = context.checkerContext.getProcessingEnvironment();
    Types types = env.getTypeUtils();
    if (isNullLiteral(expression, context)) {
        return parseNullLiteral(expression, types);
    } else if (isIntLiteral(expression, context)) {
        return parseIntLiteral(expression, types);
    } else if (isLongLiteral(expression, context)) {
        return parseLongLiteral(expression, types);
    } else if (isStringLiteral(expression, context)) {
        return parseStringLiteral(expression, types, env.getElementUtils());
    } else if (isThisLiteral(expression, context)) {
        return parseThis(expression, context);
    } else if (isSuperLiteral(expression, context)) {
        return parseSuper(expression, types, context);
    } else if (isIdentifier(expression, context)) {
        return parseIdentifier(expression, env, path, context);
    } else if (isParameter(expression, context)) {
        return parseParameter(expression, context);
    } else if (isArray(expression, context)) {
        return parseArray(expression, context, path);
    } else if (isMethod(expression, context)) {
        return parseMethod(expression, context, path, env);
    } else if (isMemberSelect(expression, context)) {
        return parseMemberSelect(expression, env, context, path);
    } else if (isParentheses(expression, context)) {
        return parseParentheses(expression, context, path);
    } else {
        throw constructParserException(expression);
    }
}
Also used : Types(javax.lang.model.util.Types) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment)

Aggregations

Types (javax.lang.model.util.Types)113 TypeMirror (javax.lang.model.type.TypeMirror)77 TypeElement (javax.lang.model.element.TypeElement)52 Elements (javax.lang.model.util.Elements)48 ExecutableElement (javax.lang.model.element.ExecutableElement)34 Element (javax.lang.model.element.Element)30 SupportedAnnotationTypes (javax.annotation.processing.SupportedAnnotationTypes)27 DeclaredType (javax.lang.model.type.DeclaredType)26 ArrayList (java.util.ArrayList)25 Map (java.util.Map)24 VariableElement (javax.lang.model.element.VariableElement)24 List (java.util.List)21 AnnotationMirror (javax.lang.model.element.AnnotationMirror)20 TypeKind (javax.lang.model.type.TypeKind)18 Set (java.util.Set)16 Collection (java.util.Collection)15 HashMap (java.util.HashMap)13 ElementKind (javax.lang.model.element.ElementKind)13 Modifier (javax.lang.model.element.Modifier)13 IOException (java.io.IOException)12