Example usage for javax.lang.model.type TypeKind DECLARED

List of usage examples for javax.lang.model.type TypeKind DECLARED

Introduction

In this page you can find the example usage for javax.lang.model.type TypeKind DECLARED.

Prototype

TypeKind DECLARED

To view the source code for javax.lang.model.type TypeKind DECLARED.

Click Source Link

Document

A class or interface type.

Usage

From source file:Main.java

private static TypeKind getKindOfType(Class<?> type) {
    if (!type.isPrimitive()) {
        return TypeKind.DECLARED;
    }//from   ww  w. jav  a2 s  .c om
    return TypeKind.valueOf(type.getName().toUpperCase());
}

From source file:org.lambdamatic.mongodb.apt.template.ElementUtils.java

/**
 * Checks if the given {@link Element} is, implements or extends the given target type.
 * /*from www .  java2  s.  c o m*/
 * @param type the element to analyze
 * @param targetType the type to check
 * @return <code>true</code> if the given {@link Element} corresponds to a type that implements
 *         {@link List}, <code>false</code> otherwise.
 */
public static boolean isAssignable(final DeclaredType type, final Class<?> targetType) {
    if (type instanceof NoType) {
        return false;
    }
    if (type.asElement().toString().equals(targetType.getName())) {
        return true;
    }
    final TypeElement element = (TypeElement) type.asElement();
    final boolean implementation = element.getInterfaces().stream()
            .filter(interfaceMirror -> interfaceMirror.getKind() == TypeKind.DECLARED)
            .map(interfaceMirror -> (DeclaredType) interfaceMirror)
            .map(declaredInterface -> declaredInterface.asElement())
            .anyMatch(declaredElement -> declaredElement.toString().equals(targetType.getName()));
    if (implementation) {
        return true;
    }
    if (element.getSuperclass().getKind() == TypeKind.DECLARED) {
        return isAssignable((DeclaredType) (element.getSuperclass()), targetType);
    }
    return false;
}

From source file:info.archinnov.achilles.internals.parser.FunctionParamParser.java

public FunctionParamSignature parseParam(GlobalParsingContext context, AnnotationTree annotationTree,
        TypeName parentType, String methodName, String paramName) {
    final TypeMirror currentTypeMirror = annotationTree.getCurrentType();
    final TypeName sourceType = TypeName.get(currentTypeMirror).box();
    boolean isUDT = currentTypeMirror.getKind() == TypeKind.DECLARED
            && aptUtils.getAnnotationOnClass(currentTypeMirror, UDT.class).isPresent();

    if (containsAnnotation(annotationTree, JSON.class)) {
        return new FunctionParamSignature(paramName, sourceType, STRING, "text");
    } else if (containsAnnotation(annotationTree, Computed.class)) {
        throw new AchillesBeanMappingException(
                format("Cannot have @Computed annotation on param '%s' of method '%s' on class '%s'", paramName,
                        methodName));/*from  w  w w.j ava  2 s  .  co m*/
    } else if (aptUtils.isAssignableFrom(Tuple1.class, currentTypeMirror)) {
        return parseTuple1(context, annotationTree, parentType, methodName, paramName);
    } else if (aptUtils.isAssignableFrom(Tuple2.class, currentTypeMirror)) {
        return parseTuple2(context, annotationTree, parentType, methodName, paramName);
    } else if (aptUtils.isAssignableFrom(Tuple3.class, currentTypeMirror)) {
        return parseTuple3(context, annotationTree, parentType, methodName, paramName);
    } else if (aptUtils.isAssignableFrom(Tuple4.class, currentTypeMirror)) {
        return parseTuple4(context, annotationTree, parentType, methodName, paramName);
    } else if (aptUtils.isAssignableFrom(Tuple5.class, currentTypeMirror)) {
        return parseTuple5(context, annotationTree, parentType, methodName, paramName);
    } else if (aptUtils.isAssignableFrom(Tuple6.class, currentTypeMirror)) {
        return parseTuple6(context, annotationTree, parentType, methodName, paramName);
    } else if (aptUtils.isAssignableFrom(Tuple7.class, currentTypeMirror)) {
        return parseTuple7(context, annotationTree, parentType, methodName, paramName);
    } else if (aptUtils.isAssignableFrom(Tuple8.class, currentTypeMirror)) {
        return parseTuple8(context, annotationTree, parentType, methodName, paramName);
    } else if (aptUtils.isAssignableFrom(Tuple9.class, currentTypeMirror)) {
        return parseTuple9(context, annotationTree, parentType, methodName, paramName);
    } else if (aptUtils.isAssignableFrom(Tuple10.class, currentTypeMirror)) {
        return parseTuple10(context, annotationTree, parentType, methodName, paramName);
    } else if (aptUtils.isAssignableFrom(List.class, currentTypeMirror)) {
        return parseList(context, annotationTree, parentType, methodName, paramName);
    } else if (aptUtils.isAssignableFrom(Set.class, currentTypeMirror)) {
        return parseSet(context, annotationTree, parentType, methodName, paramName);
    } else if (aptUtils.isAssignableFrom(Map.class, currentTypeMirror)) {
        return parseMap(context, annotationTree, parentType, methodName, paramName);
    } else if (aptUtils.isAssignableFrom(java.util.Optional.class, currentTypeMirror)) {
        return parseOptional(context, annotationTree, parentType, methodName, paramName);
    } else if (isUDT) {
        return parseUDT(context, annotationTree, paramName);
    } else {
        return parseSimpleType(context, annotationTree, parentType, methodName, paramName);
    }
}

From source file:fr.xebia.extras.selma.codegen.MapperClassGenerator.java

private List<MethodWrapper> validateTypes() {

    List<MethodWrapper> res = new ArrayList<MethodWrapper>();

    for (ExecutableElement mapperMethod : mapperMethods) {

        MethodWrapper methodWrapper = new MethodWrapper(mapperMethod, declaredType, context);
        res.add(methodWrapper);/*from   www  . j  a  va  2 s. c om*/

        mapper.buildEnumForMethod(methodWrapper);

        InOutType inOutType = methodWrapper.inOutType();
        if (inOutType.differs()) {
            MappingBuilder builder = MappingBuilder.getBuilderFor(context, inOutType);

            if ((inOutType.in().getKind() != TypeKind.DECLARED
                    || inOutType.out().getKind() != TypeKind.DECLARED) && builder == null) {
                context.error(mapperMethod,
                        "In type : %s and Out type : %s differs and this kind of conversion is not supported here",
                        inOutType.in(), inOutType.out());
            } else {
                context.mappingMethod(methodWrapper.inOutType(), methodWrapper.getSimpleName());
            }
        }

    }
    return res;
}

From source file:org.mule.devkit.apt.model.AnnotationProcessorIdentifiable.java

@Override
public boolean isXmlType() {
    if (asType().getKind() == TypeKind.DECLARED) {

        DeclaredType declaredType = (DeclaredType) asType();
        XmlType xmlType = declaredType.asElement().getAnnotation(XmlType.class);

        if (xmlType != null) {
            return true;
        }/*from  w  w  w .  j a v a  2  s . com*/
    }

    return false;
}

From source file:org.netbeans.jpa.modeler.properties.convert.ConvertPanel.java

static void importAttributeConverter(String classHandle, AtomicBoolean validated, ModelerFile modelerFile) {
    if (StringUtils.isBlank(classHandle)) {
        validated.set(true);/*from  ww  w.j a  v a  2 s .  c  o  m*/
        return;
    }
    FileObject pkg = SourceGroupSupport.findSourceGroupForFile(modelerFile.getFileObject()).getRootFolder();
    try {
        JavaSource javaSource = JavaSource.create(ClasspathInfo.create(pkg));
        javaSource.runUserActionTask(controller -> {
            try {
                controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
                TypeElement jc = controller.getElements().getTypeElement(classHandle);
                EntityMappings entityMappings = (EntityMappings) modelerFile.getDefinitionElement();
                Optional<Converter> converter = entityMappings.findConverter(classHandle);
                if (jc != null) {
                    DeclaredType attributeConverterType = null;
                    if (!jc.getInterfaces().isEmpty()) { //fetch interface info
                        for (TypeMirror interfaceType : jc.getInterfaces()) {
                            if (interfaceType.getKind() == TypeKind.DECLARED && AttributeConverter.class
                                    .getName().equals(((DeclaredType) interfaceType).asElement().toString())) {
                                attributeConverterType = (DeclaredType) interfaceType;
                            }
                        }
                    }
                    if (attributeConverterType != null
                            && attributeConverterType.getTypeArguments().size() == 2) {
                        TypeMirror attributeType = attributeConverterType.getTypeArguments().get(0);
                        TypeMirror dbFieldType = attributeConverterType.getTypeArguments().get(1);
                        if (!entityMappings.addConverter(classHandle, attributeType.toString(),
                                dbFieldType.toString())) {
                            message("MSG_ATTRIBUTE_CONVERTER_TYPE_CONFLICT", classHandle);
                        } else {
                            if (!converter.isPresent()) {
                                message("MSG_ATTRIBUTE_CONVERTER_TYPE_REGISTERED", classHandle,
                                        attributeType.toString(), dbFieldType.toString());
                            }
                            validated.set(true);
                        }
                    } else {
                        message("MSG_ATTRIBUTE_CONVERTER_NOT_IMPLEMENTED", classHandle);
                    }
                } else {
                    if (converter.isPresent()) {
                        validated.set(true);
                    } else {
                        message("MSG_ARTIFACT_NOT_FOUND", classHandle, pkg.getPath());
                    }
                }
            } catch (IOException t) {
                ExceptionUtils.printStackTrace(t);
            }
        }, true);
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
}

From source file:io.github.jeddict.jpa.modeler.properties.convert.ConvertPanel.java

static void importAttributeConverter(String classHandle, AtomicBoolean validated, ModelerFile modelerFile) {
    if (StringUtils.isBlank(classHandle)) {
        validated.set(true);//from   w  w w  . ja va  2 s.c o m
        return;
    }
    FileObject pkg = findSourceGroupForFile(modelerFile.getFileObject()).getRootFolder();
    try {
        JavaSource javaSource = JavaSource.create(ClasspathInfo.create(pkg));
        javaSource.runUserActionTask(controller -> {
            try {
                controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
                TypeElement jc = controller.getElements().getTypeElement(classHandle);
                EntityMappings entityMappings = (EntityMappings) modelerFile.getDefinitionElement();
                Optional<Converter> converter = entityMappings.findConverter(classHandle);
                if (jc != null) {
                    DeclaredType attributeConverterType = null;
                    if (!jc.getInterfaces().isEmpty()) { //fetch interface info
                        for (TypeMirror interfaceType : jc.getInterfaces()) {
                            if (interfaceType.getKind() == TypeKind.DECLARED && AttributeConverter.class
                                    .getName().equals(((DeclaredType) interfaceType).asElement().toString())) {
                                attributeConverterType = (DeclaredType) interfaceType;
                            }
                        }
                    }
                    if (attributeConverterType != null
                            && attributeConverterType.getTypeArguments().size() == 2) {
                        TypeMirror attributeType = attributeConverterType.getTypeArguments().get(0);
                        TypeMirror dbFieldType = attributeConverterType.getTypeArguments().get(1);
                        if (!entityMappings.addConverter(classHandle, attributeType.toString(),
                                dbFieldType.toString())) {
                            message("MSG_ATTRIBUTE_CONVERTER_TYPE_CONFLICT", classHandle);
                        } else {
                            if (!converter.isPresent()) {
                                message("MSG_ATTRIBUTE_CONVERTER_TYPE_REGISTERED", classHandle,
                                        attributeType.toString(), dbFieldType.toString());
                            }
                            validated.set(true);
                        }
                    } else {
                        message("MSG_ATTRIBUTE_CONVERTER_NOT_IMPLEMENTED", classHandle);
                    }
                } else {
                    if (converter.isPresent()) {
                        validated.set(true);
                    } else {
                        message("MSG_ARTIFACT_NOT_FOUND", classHandle, pkg.getPath());
                    }
                }
            } catch (IOException t) {
                ExceptionUtils.printStackTrace(t);
            }
        }, true);
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
}

From source file:cop.raml.utils.ImportScanner.java

/**
 * Find class root element for given {@code element} and retrieve all imports are used in the java file. All imported elements will be stored in
 * the internal map.// w  w w.j av a2 s .  com
 *
 * @param element some element
 */
public void setCurrentElement(@NotNull Element element) {
    element = getClassRootElement(element);
    TypeKind kind = element.asType().getKind();

    if (kind != TypeKind.DECLARED && kind != TypeKind.ERROR)
        return;

    className = element.toString();

    if (imports.containsKey(className))
        return;

    for (String className : SorcererJavacUtils.getImports(element)) {
        if (className.endsWith(".*"))
            rootElements.entrySet().stream()
                    .filter(entry -> entry.getKey().startsWith(className.substring(0, className.length() - 1)))
                    .forEach(entry -> addImport(this.className, entry.getKey(), entry.getValue()));
        else if (rootElements.containsKey(className))
            addImport(this.className, className, rootElements.get(className));
        else {
            TypeElement typeElement = ThreadLocalContext.getElement(className);

            if (typeElement == null)
                continue;
            if (Config.ramlSkipClassName(typeElement.toString()))
                continue;

            // TODO values current project package and include include only related elements

            addImport(this.className, className, typeElement);
            rootElements.putIfAbsent(typeElement.toString(), typeElement);
        }
    }
}

From source file:cop.raml.mocks.MockUtils.java

private static TypeElementMock createEnumElement(@NotNull Class<?> cls) {
    TypeElementMock element = new TypeElementMock(cls.getName(), ElementKind.ENUM);
    element.setType(new TypeMirrorMock(element, TypeKind.DECLARED));
    element.addEnclosedElement(new TypeElementMock("values()", ElementKind.METHOD));

    for (Object obj : cls.getEnumConstants())
        element.addEnclosedElement(new VariableElementMock(obj.toString(), ElementKind.ENUM_CONSTANT));

    return setAnnotations(element, cls);
}

From source file:org.androidannotations.helper.IntentBuilder.java

private JMethod addPutExtraMethod(TypeMirror elementType, String parameterName, JFieldVar extraKeyField) {
    boolean castToSerializable = false;
    boolean castToParcelable = false;
    if (elementType.getKind() == TypeKind.DECLARED) {
        Elements elementUtils = holder.processingEnvironment().getElementUtils();
        TypeMirror parcelableType = elementUtils.getTypeElement(PARCELABLE).asType();
        if (!typeUtils.isSubtype(elementType, parcelableType)) {
            TypeMirror stringType = elementUtils.getTypeElement(STRING).asType();
            if (!typeUtils.isSubtype(elementType, stringType)) {
                castToSerializable = true;
            }//  w ww.j ava 2  s .  c om
        } else {
            TypeMirror serializableType = elementUtils.getTypeElement(SERIALIZABLE).asType();
            if (typeUtils.isSubtype(elementType, serializableType)) {
                castToParcelable = true;
            }
        }
    }

    JMethod method = holder.getIntentBuilderClass().method(PUBLIC, holder.getIntentBuilderClass(),
            parameterName);
    JClass parameterClass = codeModelHelper.typeMirrorToJClass(elementType, holder);
    JVar extraParameterVar = method.param(parameterClass, parameterName);
    JBlock body = method.body();
    JInvocation invocation = body.invoke(holder.getIntentField(), "putExtra").arg(extraKeyField);
    if (castToSerializable) {
        invocation.arg(cast(holder.classes().SERIALIZABLE, extraParameterVar));
    } else if (castToParcelable) {
        invocation.arg(cast(holder.classes().PARCELABLE, extraParameterVar));
    } else {
        invocation.arg(extraParameterVar);
    }
    body._return(_this());
    return method;
}