Example usage for javax.lang.model.element TypeElement toString

List of usage examples for javax.lang.model.element TypeElement toString

Introduction

In this page you can find the example usage for javax.lang.model.element TypeElement toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:Main.java

public static boolean checkIfIsSerializable(ProcessingEnvironment processingEnvironment, String type) {
    if (type.equals("java.lang.String") || type.equals("java.lang.Boolean")
            || type.equals("java.lang.Character")) {
        //it is serializable but we handle it different way
        return false;
    }//  w ww .  j av  a 2 s . c om

    TypeElement serializableElement = processingEnvironment.getElementUtils()
            .getTypeElement("java.io.Serializable");
    TypeElement elementType;

    elementType = processingEnvironment.getElementUtils().getTypeElement(type);

    if (elementType != null) {
        for (TypeMirror typeMirror : elementType.getInterfaces()) {
            if (typeMirror.toString().equals(serializableElement.toString())) {
                return true;
            }
        }
    }
    return false;
}

From source file:Main.java

public static boolean checkIfIsParcelable(ProcessingEnvironment processingEnvironment, String type) {
    TypeElement parcelableElement = processingEnvironment.getElementUtils()
            .getTypeElement("android.os.Parcelable");
    TypeElement elementType;/*from w w w.  ja v a  2  s.  c o m*/
    if (type.contains("<") && type.contains(">")) {
        elementType = processingEnvironment.getElementUtils()
                .getTypeElement(type.substring(type.indexOf("<") + 1, type.indexOf(">")));
    } else {
        elementType = processingEnvironment.getElementUtils()
                .getTypeElement(type.replace("[]", "").replace(">", ""));
    }
    if (elementType != null) {
        for (TypeMirror typeMirror : elementType.getInterfaces()) {
            if (typeMirror.toString().equals(parcelableElement.toString())) {
                return true;
            }
        }
    }
    return false;
}

From source file:cop.raml.utils.example.JsonExample.java

/**
 * Retrieves example for given {@code obj} which is not simple element, e.g. class with variables and method.
 * To build example of this object, read recursively all it's variables (each of them could be not simple element as well), build example and put
 * it into a map. Only not {@code null} example a placed into the map.
 * Additionally we consider {@link JsonIgnoreProperties} and {@link JsonIgnore} annotations.
 *
 * @param obj     not {@code null} complex element
 * @param visited not {@code null} visited object (use it to avoid cycle references)
 * @return {@code null} or not empty map of element name tp element example structure
 *///  w  w  w.  j a  v a  2  s  .  c om
private Map<String, Object> getTypeExample(@NotNull TypeElement obj, @NotNull Set<String> visited) {
    String name = obj.toString();

    if (visited.contains(name))
        return null;

    visited.add(name);

    try {
        Set<String> ignored = getIgnoredFields(obj);
        Map<String, Object> map = new LinkedHashMap<>();
        String elementName;
        Object res;

        for (Element element : obj.getEnclosedElements()) {
            if (element.getKind() != ElementKind.FIELD)
                continue;
            if (ElementUtils.isStatic(element))
                continue;
            if (ignored.contains(elementName = element.getSimpleName().toString()))
                continue;
            if ((res = getElementExample(element, visited)) != null)
                map.put(elementName, res);
        }

        return map;
    } finally {
        visited.remove(name);
    }
}

From source file:com.spotify.docgenerator.JacksonJerseyAnnotationProcessor.java

private void processRESTEndpointAnnotations(final Set<? extends TypeElement> annotations,
        final RoundEnvironment roundEnv) {
    for (String methodAnnotation : METHOD_ANNOTATIONS) {
        for (TypeElement foundAnnotations : annotations) {
            if (foundAnnotations.toString().equals(methodAnnotation)) {
                processFoundRestAnnotations(foundAnnotations, roundEnv);
            }//from   w  ww.  java 2  s .  c o  m
        }
    }
}

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./*www.  j a v  a  2s. c om*/
 *
 * @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.utils.ImportScanner.java

public TypeElement getElement(String className) {
    if (StringUtils.isBlank(className))
        return null;
    if (imports.containsKey(this.className) && imports.get(this.className).containsKey(className))
        return imports.get(this.className).get(className);
    if (rootElements.containsKey(className))
        return rootElements.get(className);

    TypeElement element;

    if ((element = getOneRootElementWithName(className)) != null)
        return element;
    if ((element = ThreadLocalContext.getElement(className)) != null) {
        rootElements.putIfAbsent(element.toString(), element);
        return element;
    }/*w ww. j  a va  2  s .  co  m*/

    className = className.startsWith("java.lang.") ? className : String.format("java.lang.%s", className);

    if ((element = ThreadLocalContext.getElement(className)) != null) {
        rootElements.putIfAbsent(element.toString(), element);
        return element;
    }

    Matcher matcher = NAME_COLLECTION.matcher(className);

    if (!matcher.matches())
        return null;

    className = matcher.group("className");
    return "any".equals(className) ? null : getElement(matcher.group("className"));
}

From source file:com.mastfrog.parameters.processor.Processor.java

private void checkConstructor(TypeElement el, GeneratedParamsClass inf) {
    Elements elements = processingEnv.getElementUtils();
    TypeElement pageParamsType = elements
            .getTypeElement("org.apache.wicket.request.mapper.parameter.PageParameters");
    TypeElement customParamsType = elements.getTypeElement(inf.qualifiedName());
    boolean found = false;
    boolean foundArgument = false;
    ExecutableElement con = null;
    outer: for (Element sub : el.getEnclosedElements()) {
        switch (sub.getKind()) {
        case CONSTRUCTOR:
            for (AnnotationMirror mir : sub.getAnnotationMirrors()) {
                DeclaredType type = mir.getAnnotationType();
                switch (type.toString()) {
                case "javax.inject.Inject":
                case "com.google.inject.Inject":
                    ExecutableElement constructor = (ExecutableElement) sub;
                    con = constructor;/*from  w  w w. ja va 2 s  .  co  m*/
                    for (VariableElement va : constructor.getParameters()) {
                        TypeMirror varType = va.asType();
                        if (pageParamsType != null && varType.toString().equals(pageParamsType.toString())) {
                            foundArgument = true;
                            break;
                        }
                        if (customParamsType != null
                                && varType.toString().equals(customParamsType.toString())) {
                            foundArgument = true;
                            break;
                        } else if (customParamsType == null && inf.qualifiedName().equals(varType.toString())) { //first compilation - type not generated yet
                            foundArgument = true;
                            break;
                        }
                    }
                    found = true;
                    break outer;
                default:
                    //do nothing
                }
            }
        }
    }
    if (!found) {
        processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "Usually a constructor "
                + "annotated with @Inject that takes a " + inf.className + " is desired", el);
    } else if (found && !foundArgument) {
        processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING,
                "Usually a constructor taking " + "an argument of " + inf.className + " is desired", con);
    }
}

From source file:org.kie.workbench.common.forms.adf.processors.FormDefinitionGenerator.java

private List<FormDefinitionFieldData> extractParentFormFields(TypeElement element, FieldPolicy policy,
        I18nSettings i18nSettings, Map<String, String> defaultParams) throws Exception {

    if (element.toString().equals(Object.class.getName())) {
        return new ArrayList<>();
    }/*  ww w . j a v  a2  s.c om*/

    TypeElement parentElement = getParent(element);

    List<FormDefinitionFieldData> result = extractParentFormFields(parentElement, policy, i18nSettings,
            defaultParams);

    result.addAll(extracFormFields(element, policy, i18nSettings, defaultParams));

    return result;
}

From source file:org.kie.workbench.common.forms.adf.processors.FormDefinitionsProcessor.java

private List<Map<String, String>> extractParentFormFields(TypeElement element, FieldPolicy policy,
        I18nSettings i18nSettings) throws Exception {

    if (element.toString().equals(Object.class.getName())) {
        return new ArrayList<>();
    }/*  w  ww.  j av  a2s. c o  m*/

    TypeElement parentElement = getParent(element);

    List<Map<String, String>> result = extractParentFormFields(parentElement, policy, i18nSettings);
    result.addAll(extracFormFields(element, policy, i18nSettings));

    return result;
}

From source file:org.kie.workbench.common.stunner.core.processors.MainProcessor.java

private TypeElement getDefinitionInheritedType(TypeElement classElement) {
    final Elements elementUtils = processingEnv.getElementUtils();
    classElement = getParent(classElement);
    while (!classElement.toString().equals(Object.class.getName())) {
        List<VariableElement> variableElements = ElementFilter.fieldsIn(classElement.getEnclosedElements());
        for (VariableElement variableElement : variableElements) {
            for (String annotation : DEFINITION_ANNOTATIONS) {
                if (GeneratorUtils.getAnnotation(elementUtils, variableElement, annotation) != null) {
                    return classElement;
                }//from  www  .jav a 2 s. co  m
            }
        }
        classElement = getParent(classElement);
    }
    return null;
}