Search in sources :

Example 11 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project immutables by immutables.

the class FactoryMethodAttributesCollector method collect.

void collect() {
    ExecutableElement factoryMethodElement = (ExecutableElement) protoclass.sourceElement();
    Parameterizable element = (Parameterizable) (factoryMethodElement.getKind() == ElementKind.CONSTRUCTOR ? factoryMethodElement.getEnclosingElement() : type.element);
    for (VariableElement parameter : factoryMethodElement.getParameters()) {
        TypeMirror returnType = parameter.asType();
        ValueAttribute attribute = new ValueAttribute();
        attribute.isGenerateAbstract = true;
        attribute.reporter = reporter;
        attribute.returnType = returnType;
        attribute.element = parameter;
        String parameterName = parameter.getSimpleName().toString();
        attribute.names = styles.forAccessorWithRaw(parameterName, parameterName);
        attribute.containingType = type;
        attributes.add(attribute);
    }
    Instantiator encodingInstantiator = protoclass.encodingInstantiator();
    @Nullable InstantiationCreator instantiationCreator = encodingInstantiator.creatorFor(element);
    for (ValueAttribute attribute : attributes) {
        attribute.initAndValidate(instantiationCreator);
    }
    if (instantiationCreator != null) {
        type.additionalImports(instantiationCreator.imports);
    }
    type.attributes.addAll(attributes);
    type.throwing = extractThrowsClause(factoryMethodElement);
}
Also used : InstantiationCreator(org.immutables.value.processor.encode.Instantiator.InstantiationCreator) TypeMirror(javax.lang.model.type.TypeMirror) ExecutableElement(javax.lang.model.element.ExecutableElement) Parameterizable(javax.lang.model.element.Parameterizable) Instantiator(org.immutables.value.processor.encode.Instantiator) VariableElement(javax.lang.model.element.VariableElement) Nullable(javax.annotation.Nullable)

Example 12 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project immutables by immutables.

the class FromSupertypesModel method isEligibleFromType.

private boolean isEligibleFromType(TypeElement typeElement, ValueAttribute attr) {
    if (!typeElement.getTypeParameters().isEmpty()) {
        return false;
    }
    @Nullable ExecutableElement accessor = findMethod(typeElement, attr.names.get);
    if (accessor == null) {
        // it (null) should never happen in theory
        return false;
    }
    boolean sameReturnType = accessor.getReturnType().toString().equals(attr.returnType.toString());
    if (sameReturnType) {
        return true;
    }
    reporter.warning("Generated 'Builder.from' method will not copy from attribute '%s'" + " because it has different return type in supertype" + " (And we cannot handle generic specialization or covarian overrides yet)." + " Sometimes it is possible to avoid this by providing abstract override method in this value object", attr.name());
    return false;
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) Nullable(javax.annotation.Nullable)

Example 13 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project immutables by immutables.

the class AccessorAttributesCollector method collectGeneratedCandidateMethods.

private void collectGeneratedCandidateMethods(TypeElement type) {
    TypeElement originalType = CachingElements.getDelegate(type);
    List<? extends Element> accessorsInSourceOrder;
    if (originalType.getKind() == ElementKind.ANNOTATION_TYPE) {
        accessorsInSourceOrder = SourceOrdering.getEnclosedElements(originalType);
    } else {
        AccessorProvider provider = SourceOrdering.getAllAccessorsProvider(processing.getElementUtils(), processing.getTypeUtils(), originalType);
        accessorsInSourceOrder = provider.get();
        accessorMapping = provider.accessorMapping();
    }
    for (ExecutableElement element : ElementFilter.methodsIn(accessorsInSourceOrder)) {
        if (isElegibleAccessorMethod(element)) {
            processGenerationCandidateMethod(element, originalType);
        }
    }
    // inform use during checking for warnings.
    for (Element element : processing.getElementUtils().getAllMembers(originalType)) {
        if (element.getKind() == ElementKind.METHOD) {
            switch(element.getSimpleName().toString()) {
                case HASH_CODE_METHOD:
                case TO_STRING_METHOD:
                case EQUALS_METHOD:
                    processUtilityCandidateMethod((ExecutableElement) element, originalType);
                    break;
                default:
            }
        }
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) AccessorProvider(org.immutables.generator.SourceOrdering.AccessorProvider)

Example 14 with ExecutableElement

use of javax.lang.model.element.ExecutableElement 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 15 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project hibernate-orm by hibernate.

the class XmlMetaEntity method getType.

/**
	 * Returns the entity type for a property.
	 *
	 * @param propertyName The property name
	 * @param explicitTargetEntity The explicitly specified target entity type or {@code null}.
	 * @param expectedElementKind Determines property vs field access type
	 *
	 * @return The entity type for this property  or {@code null} if the property with the name and the matching access
	 *         type does not exist.
	 */
private String getType(String propertyName, String explicitTargetEntity, ElementKind expectedElementKind) {
    for (Element elem : element.getEnclosedElements()) {
        if (!expectedElementKind.equals(elem.getKind())) {
            continue;
        }
        TypeMirror mirror;
        String name = elem.getSimpleName().toString();
        if (ElementKind.METHOD.equals(elem.getKind())) {
            name = StringUtil.getPropertyName(name);
            mirror = ((ExecutableElement) elem).getReturnType();
        } else {
            mirror = elem.asType();
        }
        if (name == null || !name.equals(propertyName)) {
            continue;
        }
        if (explicitTargetEntity != null) {
            // TODO should there be a check of the target entity class and if it is loadable?
            return explicitTargetEntity;
        }
        switch(mirror.getKind()) {
            case INT:
                {
                    return "java.lang.Integer";
                }
            case LONG:
                {
                    return "java.lang.Long";
                }
            case BOOLEAN:
                {
                    return "java.lang.Boolean";
                }
            case BYTE:
                {
                    return "java.lang.Byte";
                }
            case SHORT:
                {
                    return "java.lang.Short";
                }
            case CHAR:
                {
                    return "java.lang.Char";
                }
            case FLOAT:
                {
                    return "java.lang.Float";
                }
            case DOUBLE:
                {
                    return "java.lang.Double";
                }
            case DECLARED:
                {
                    return mirror.toString();
                }
            case TYPEVAR:
                {
                    return mirror.toString();
                }
            default:
                {
                }
        }
    }
    context.logMessage(Diagnostic.Kind.WARNING, "Unable to determine type for property " + propertyName + " of class " + getQualifiedName() + " using access type " + accessTypeInfo.getDefaultAccessType());
    return null;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element)

Aggregations

ExecutableElement (javax.lang.model.element.ExecutableElement)341 TypeElement (javax.lang.model.element.TypeElement)154 TypeMirror (javax.lang.model.type.TypeMirror)96 VariableElement (javax.lang.model.element.VariableElement)85 Element (javax.lang.model.element.Element)69 Test (org.junit.Test)41 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)32 DeclaredType (javax.lang.model.type.DeclaredType)30 ArrayList (java.util.ArrayList)26 JBlock (com.helger.jcodemodel.JBlock)25 JInvocation (com.helger.jcodemodel.JInvocation)20 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)18 JVar (com.helger.jcodemodel.JVar)18 Map (java.util.Map)18 IJExpression (com.helger.jcodemodel.IJExpression)16 HashSet (java.util.HashSet)16 ElementValidation (org.androidannotations.ElementValidation)15 Block (com.google.devtools.j2objc.ast.Block)13 JMethod (com.helger.jcodemodel.JMethod)13 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)11