Search in sources :

Example 21 with TypeMirror

use of javax.lang.model.type.TypeMirror in project immutables by immutables.

the class AccessorAttributesCollector method processGenerationCandidateMethod.

private void processGenerationCandidateMethod(ExecutableElement attributeMethodCandidate, TypeElement originalType) {
    Name name = attributeMethodCandidate.getSimpleName();
    if (CheckMirror.isPresent(attributeMethodCandidate)) {
        if (!attributeMethodCandidate.getParameters().isEmpty() || attributeMethodCandidate.getModifiers().contains(Modifier.PRIVATE) || attributeMethodCandidate.getModifiers().contains(Modifier.ABSTRACT) || attributeMethodCandidate.getModifiers().contains(Modifier.STATIC) || attributeMethodCandidate.getModifiers().contains(Modifier.NATIVE)) {
            report(attributeMethodCandidate).error("Method '%s' annotated with @%s must be non-private parameter-less method", name, CheckMirror.simpleName());
        } else if (attributeMethodCandidate.getReturnType().getKind() == TypeKind.VOID) {
            type.addNormalizeMethod(name.toString(), false);
        } else if (returnsNormalizedAbstractValueType(attributeMethodCandidate)) {
            type.addNormalizeMethod(name.toString(), true);
        } else {
            report(attributeMethodCandidate).error("Method '%s' annotated with @%s must return void or normalized instance of abstract value type", name, CheckMirror.simpleName());
        }
        return;
    }
    boolean useDefaultAsDefault = type.constitution.style().defaultAsDefault();
    if (isDiscoveredAttribute(attributeMethodCandidate, useDefaultAsDefault)) {
        TypeMirror returnType = resolveReturnType(attributeMethodCandidate);
        ValueAttribute attribute = new ValueAttribute();
        attribute.reporter = reporter;
        attribute.returnType = returnType;
        attribute.names = deriveNames(name.toString());
        attribute.element = attributeMethodCandidate;
        attribute.containingType = type;
        boolean isFinal = isFinal(attributeMethodCandidate);
        boolean isAbstract = isAbstract(attributeMethodCandidate);
        boolean defaultAnnotationPresent = DefaultMirror.isPresent(attributeMethodCandidate);
        boolean derivedAnnotationPresent = DerivedMirror.isPresent(attributeMethodCandidate);
        if (isAbstract) {
            attribute.isGenerateAbstract = true;
            if (attributeMethodCandidate.getDefaultValue() != null) {
                attribute.isGenerateDefault = true;
            }
            if (defaultAnnotationPresent || derivedAnnotationPresent) {
                if (defaultAnnotationPresent) {
                    if (attribute.isGenerateDefault) {
                        report(attributeMethodCandidate).annotationNamed(DefaultMirror.simpleName()).warning("@Value.Default annotation is superflous for default annotation attribute");
                    } else {
                        report(attributeMethodCandidate).annotationNamed(DefaultMirror.simpleName()).error("@Value.Default attribute should have initializer body", name);
                    }
                }
                if (derivedAnnotationPresent) {
                    if (attribute.isGenerateDefault) {
                        report(attributeMethodCandidate).annotationNamed(DerivedMirror.simpleName()).error("@Value.Derived cannot be used with default annotation attribute");
                    } else {
                        report(attributeMethodCandidate).annotationNamed(DerivedMirror.simpleName()).error("@Value.Derived attribute should have initializer body", name);
                    }
                }
            }
        } else if (defaultAnnotationPresent && derivedAnnotationPresent) {
            report(attributeMethodCandidate).annotationNamed(DerivedMirror.simpleName()).error("Attribute '%s' cannot be both @Value.Default and @Value.Derived", name);
            attribute.isGenerateDefault = true;
        } else if ((defaultAnnotationPresent || derivedAnnotationPresent) && isFinal) {
            report(attributeMethodCandidate).error("Annotated attribute '%s' will be overriden and cannot be final", name);
        } else if (defaultAnnotationPresent) {
            attribute.isGenerateDefault = true;
            if (useDefaultAsDefault && attribute.isInterfaceDefaultMethod()) {
                report(attributeMethodCandidate).annotationNamed(DefaultMirror.simpleName()).warning("@Value.Default annotation is superflous for default annotation attribute" + " when 'defaultAsDefault' style is enabled");
            }
        } else if (derivedAnnotationPresent) {
            attribute.isGenerateDerived = true;
        } else if (useDefaultAsDefault) {
            attribute.isGenerateDefault = attribute.isInterfaceDefaultMethod();
        }
        if (LazyMirror.isPresent(attributeMethodCandidate)) {
            if (isAbstract || isFinal) {
                report(attributeMethodCandidate).error("@Value.Lazy attribute '%s' must be non abstract and non-final", name);
            } else if (defaultAnnotationPresent || derivedAnnotationPresent) {
                report(attributeMethodCandidate).error("@Value.Lazy attribute '%s' cannot be @Value.Derived or @Value.Default", name);
            } else {
                attribute.isGenerateLazy = true;
                attribute.isGenerateDefault = false;
            }
        }
        attributes.add(attribute);
        // Compute this eagerly here, for no strong reason
        if (attribute.isGenerateDefault) {
            type.defaultAttributesCount++;
        }
        if (attribute.isGenerateDerived) {
            type.derivedAttributesCount++;
        }
        if (attributeMethodCandidate.getEnclosingElement().equals(originalType)) {
            hasNonInheritedAttributes = true;
        }
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) Name(javax.lang.model.element.Name)

Example 22 with TypeMirror

use of javax.lang.model.type.TypeMirror in project immutables by immutables.

the class AccessorAttributesCollector method resolveReturnType.

static TypeMirror resolveReturnType(ProcessingEnvironment processing, ExecutableElement method, TypeElement typeElement) {
    method = CachingElements.getDelegate(method);
    TypeMirror returnType = method.getReturnType();
    // our target class could not define formal type parameters also.
    if (returnType.getKind() == TypeKind.TYPEVAR) {
        return asInheritedMemberReturnType(processing, typeElement, method);
    } else if (returnType.getKind() == TypeKind.DECLARED || returnType.getKind() == TypeKind.ERROR) {
        if (!((DeclaredType) returnType).getTypeArguments().isEmpty()) {
            return asInheritedMemberReturnType(processing, typeElement, method);
        }
    }
    return returnType;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror)

Example 23 with TypeMirror

use of javax.lang.model.type.TypeMirror in project immutables by immutables.

the class TypeStringProvider method appendTypeArguments.

private void appendTypeArguments(TypeMirror type, DeclaredType declaredType) {
    List<? extends TypeMirror> arguments = declaredType.getTypeArguments();
    if (!arguments.isEmpty()) {
        buffer.append('<');
        boolean notFirst = false;
        for (TypeMirror argument : arguments) {
            typeAnnotationHandle(argument);
            if (notFirst) {
                buffer.append(',').append(' ');
            }
            notFirst = true;
            int mark = buffer.length();
            caseType(argument);
            cutTypeArgument(type, mark);
        }
        buffer.append('>');
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror)

Example 24 with TypeMirror

use of javax.lang.model.type.TypeMirror in project immutables by immutables.

the class ValueType method getNonAttributeAbstractMethodSignatures.

public Set<String> getNonAttributeAbstractMethodSignatures() {
    if (element.getKind().isClass() || element.getKind().isInterface()) {
        Set<String> signatures = new LinkedHashSet<>();
        List<? extends Element> members = constitution.protoclass().environment().processing().getElementUtils().getAllMembers(CachingElements.getDelegate((TypeElement) element));
        for (ExecutableElement m : ElementFilter.methodsIn(members)) {
            if (!m.getParameters().isEmpty() || m.getSimpleName().contentEquals(AccessorAttributesCollector.HASH_CODE_METHOD) || m.getSimpleName().contentEquals(AccessorAttributesCollector.TO_STRING_METHOD)) {
                if (m.getModifiers().contains(Modifier.ABSTRACT)) {
                    TypeMirror returnType = m.getReturnType();
                    if (!AccessorAttributesCollector.isEclipseImplementation(m)) {
                        returnType = AccessorAttributesCollector.asInheritedMemberReturnType(constitution.protoclass().processing(), CachingElements.getDelegate((TypeElement) element), m);
                    }
                    signatures.add(toSignature(m, returnType));
                }
            }
        }
        return signatures;
    }
    return Collections.emptySet();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 25 with TypeMirror

use of javax.lang.model.type.TypeMirror in project hibernate-orm by hibernate.

the class JPAMetaModelEntityProcessor method modelGenerationNeedsToBeDeferred.

private boolean modelGenerationNeedsToBeDeferred(Collection<MetaEntity> entities, MetaEntity containedEntity) {
    ContainsAttributeTypeVisitor visitor = new ContainsAttributeTypeVisitor(containedEntity.getTypeElement(), context);
    for (MetaEntity entity : entities) {
        if (entity.equals(containedEntity)) {
            continue;
        }
        for (Element subElement : ElementFilter.fieldsIn(entity.getTypeElement().getEnclosedElements())) {
            TypeMirror mirror = subElement.asType();
            if (!TypeKind.DECLARED.equals(mirror.getKind())) {
                continue;
            }
            boolean contains = mirror.accept(visitor, subElement);
            if (contains) {
                return true;
            }
        }
        for (Element subElement : ElementFilter.methodsIn(entity.getTypeElement().getEnclosedElements())) {
            TypeMirror mirror = subElement.asType();
            if (!TypeKind.DECLARED.equals(mirror.getKind())) {
                continue;
            }
            boolean contains = mirror.accept(visitor, subElement);
            if (contains) {
                return true;
            }
        }
    }
    return false;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) MetaEntity(org.hibernate.jpamodelgen.model.MetaEntity) AnnotationMetaEntity(org.hibernate.jpamodelgen.annotation.AnnotationMetaEntity) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element)

Aggregations

TypeMirror (javax.lang.model.type.TypeMirror)475 TypeElement (javax.lang.model.element.TypeElement)197 ExecutableElement (javax.lang.model.element.ExecutableElement)105 Test (org.junit.Test)90 VariableElement (javax.lang.model.element.VariableElement)88 DeclaredType (javax.lang.model.type.DeclaredType)76 Element (javax.lang.model.element.Element)61 ArrayList (java.util.ArrayList)36 Expression (com.google.devtools.j2objc.ast.Expression)32 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)28 Elements (javax.lang.model.util.Elements)28 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)24 AbstractJClass (com.helger.jcodemodel.AbstractJClass)24 List (java.util.List)23 Types (javax.lang.model.util.Types)22 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)21 TypeParameterElement (javax.lang.model.element.TypeParameterElement)21 ArrayType (javax.lang.model.type.ArrayType)21 FunctionElement (com.google.devtools.j2objc.types.FunctionElement)20 IJExpression (com.helger.jcodemodel.IJExpression)20