Example usage for javax.lang.model.type TypeMirror toString

List of usage examples for javax.lang.model.type TypeMirror toString

Introduction

In this page you can find the example usage for javax.lang.model.type TypeMirror toString.

Prototype

String toString();

Source Link

Document

Returns an informative string representation of this type.

Usage

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);//ww  w  . j  a v  a  2 s.co  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: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  w  w w. ja v a 2s.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:cop.raml.utils.example.JsonExample.java

/**
 * Retrieves example for given {@code element} only if given {@code element} is primitive or declared, when example can be auto generated.
 * First, look at element's javadoc and read {@link Macro#EXAMPLE}. Use it if it exists.
 * Second, look at element's javadoc and read {@link TagLink} macro, if it exists, then read linked element's javadoc and use {@link
 * Macro#EXAMPLE} if it exists./*from  w  w w  .  j ava 2  s  .c om*/
 * Third, generate random values using {@link #getPrimitiveExample(TypeKind, Random)} and {@link #getDeclaredExample(String, String)}. Otherwise
 * returns {@code null}.
 *
 * @param element element object
 * @param random  not {@code null} random generator
 * @return variable example (could be any types or {@code null})
 */
private static Object getAutoGeneratedElementExample(@NotNull Element element, @NotNull Random random)
        throws Exception {
    String doc;

    if (Macro.EXAMPLE.exists(doc = ThreadLocalContext.getDocComment(element)))
        return Macro.EXAMPLE.get(doc);
    if (Macro.EXAMPLE.exists(doc = ThreadLocalContext.getDocComment(TagLink.create(doc))))
        return Macro.EXAMPLE.get(doc);

    TypeMirror type = ElementUtils.getType(element);
    Object res = getPrimitiveExample(type.getKind(), random);
    String varName = element instanceof VariableElement ? element.toString() : null;

    return res == null ? getDeclaredExample(type.toString(), varName) : res;
}

From source file:com.vimeo.stag.processor.utils.TypeUtils.java

@NotNull
public static String getClassNameFromTypeMirror(@NotNull TypeMirror typeMirror) {
    String classAndPackage = typeMirror.toString();

    // This is done to avoid the generic template from being included in the file name
    // to be generated (since it will be an invalid file name)
    int idx = classAndPackage.indexOf("<");
    if (idx > 0) {
        classAndPackage = classAndPackage.substring(0, idx);
    }// w  ww.  ja va 2 s  .com

    return classAndPackage;
}

From source file:com.vimeo.stag.processor.utils.TypeUtils.java

/**
 * Retrieves the outer type of a parameterized class.
 * e.g. an ArrayList{@literal <T>} would be returned as
 * just ArrayList. If an interface is passed in, i.e. a
 * List, the underlying implementation will be returned,
 * i.e. ArrayList.// w w w  .  ja  v  a  2  s .  c  o m
 *
 * @param type the type to get the outer class from/
 * @return the outer class of the type passed in, or the
 * type itself if it is not parameterized.
 */
@NotNull
public static String getOuterClassType(@NotNull TypeMirror type) {
    if (type instanceof DeclaredType) {
        return ((DeclaredType) type).asElement().toString();
    } else {
        return type.toString();
    }
}

From source file:com.vimeo.stag.processor.utils.TypeUtils.java

/**
 * Retrieves the outer type of a parameterized class.
 * e.g. an ArrayList{@literal <T>} would be returned as
 * just ArrayList. If an interface is passed in, i.e. a
 * List, the underlying implementation will be returned,
 * i.e. ArrayList.//  ww w. j a va2 s.c  o  m
 *
 * @param type the type to get the outer class from/
 * @return the outer class of the type passed in, or the
 * type itself if it is not parameterized.
 */
@NotNull
public static String getSimpleOuterClassType(@NotNull TypeMirror type) {
    if (type instanceof DeclaredType) {
        return ((DeclaredType) type).asElement().getSimpleName().toString();
    } else {
        return type.toString();
    }
}

From source file:com.vimeo.stag.processor.utils.TypeUtils.java

/**
 * Retrieves a Map of the inherited concrete member variables of an Element. This takes all the
 * member variables that were inherited from the generic parent class and evaluates what their concrete
 * type will be based on the concrete inherited type. For instance, take the following code example:
 * <pre><code>/* w  w w .  jav a  2 s .  c om*/
 * {@literal Factory<T>} {
 *
 *  {@literal @UseStag}
 *   public T data;
 *
 * }
 *
 * VideoFactory extends {@literal Factory<Video>}{
 *
 *   // other variables in here
 *
 * }
 * </code></pre>
 * In this example, VideoFactory has a public member variable T that is of type Video.
 * Since the Factory class has the UseStag annotation, we cannot just generate
 * parsing code for the Factory class, since it is generic and we need concrete types.
 * Instead when we generate the adapter for VideoFactory, we crawl the inheritance
 * hierarchy gathering the member variables. When we get to VideoFactory, we see it
 * has one member variable, T. We then look at the inherited type, Factory{@literal <Video>},
 * and compare it to the original type, Factory{@literal <T>}, and then infer the type
 * of T to be Video.
 *
 * @param concreteInherited the type inherited for the class you are using, in the example,
 *                          this would be Factory{@literal <Video>}
 * @param genericInherited  the raw type inherited for the class you are using, in the example,
 *                          this would be Factory{@literal <T>}
 * @param members           the member variable map of the field (Element) to their concrete
 *                          type (TypeMirror). This should be retrieved by calling getConcreteMembers
 *                          on the inherited class.
 * @return returns a LinkedHashMap of the member variables mapped to their concrete types for the concrete
 * inherited class. (to maintain the ordering)
 */
@NotNull
public static LinkedHashMap<FieldAccessor, TypeMirror> getConcreteMembers(@NotNull TypeMirror concreteInherited,
        @NotNull TypeElement genericInherited, @NotNull Map<FieldAccessor, TypeMirror> members) {

    DebugLog.log(TAG, "Inherited concrete type: " + concreteInherited.toString());
    DebugLog.log(TAG, "Inherited generic type: " + genericInherited.asType().toString());
    List<? extends TypeMirror> concreteTypes = getParameterizedTypes(concreteInherited);
    List<? extends TypeMirror> inheritedTypes = getParameterizedTypes(genericInherited);

    LinkedHashMap<FieldAccessor, TypeMirror> map = new LinkedHashMap<>();

    for (Entry<FieldAccessor, TypeMirror> member : members.entrySet()) {

        DebugLog.log(TAG, "\t\tEvaluating member - " + member.getValue().toString());

        if (isConcreteType(member.getValue())) {

            DebugLog.log(TAG, "\t\t\tConcrete Type: " + member.getValue().toString());
            map.put(member.getKey(), member.getValue());

        } else {

            if (isParameterizedType(member.getValue())) {

                // HashMap<String, T> ...
                TypeMirror resolvedType = resolveTypeVars(member.getValue(), inheritedTypes, concreteTypes);
                map.put(member.getKey(), resolvedType);

                DebugLog.log(TAG, "\t\t\tGeneric Parameterized Type - " + member.getValue().toString()
                        + " resolved to - " + resolvedType.toString());
            } else {

                int index = inheritedTypes.indexOf(member.getKey().asType());
                TypeMirror concreteType = concreteTypes.get(index);
                map.put(member.getKey(), concreteType);

                DebugLog.log(TAG, "\t\t\tGeneric Type - " + member.getValue().toString() + " resolved to - "
                        + concreteType.toString());
            }
        }
    }
    return map;
}

From source file:org.jraf.android.prefs.compiler.PrefType.java

public boolean isCompatible(TypeMirror type) {
    return getFullName().equals(type.toString());
}

From source file:org.androidtransfuse.analysis.BroadcastReceiverAnalysis.java

private String buildReceiverType(TypeMirror type) {
    if (type != null) {
        return type.toString();
    } else {/* w  w  w  . j  a  v  a  2s.  c  o  m*/
        return android.content.BroadcastReceiver.class.getName();
    }
}

From source file:org.leandreck.endpoints.processor.model.EndpointNodeFactory.java

private List<MethodNode> defineMethods(final TypeElement typeElement, final DeclaredType containingType) {
    final TypeMirror superclass = typeElement.getSuperclass();
    final List<MethodNode> superclassMethods;
    if (DECLARED.equals(superclass.getKind()) && !"java.lang.Object".equals(superclass.toString())) {
        superclassMethods = defineMethods((TypeElement) ((DeclaredType) superclass).asElement(),
                containingType);/*www . j a v  a 2s. c  om*/
    } else {
        superclassMethods = new ArrayList<>(20);
    }

    //Implemented Interfaces:
    typeElement.getInterfaces().stream()
            .flatMap(
                    it -> defineMethods((TypeElement) ((DeclaredType) it).asElement(), containingType).stream())
            .forEach(superclassMethods::add);

    //Own enclosed Methods
    ElementFilter.methodsIn(typeElement.getEnclosedElements()).stream()
            .map(methodElement -> methodNodeFactory.createMethodNode(methodElement, containingType))
            .filter(method -> !method.isIgnored()).forEach(superclassMethods::add);
    return superclassMethods;
}