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

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

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

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

/**
 * Returns all ignored fields for given {@code typeElement}. This time support only {@link JsonIgnoreProperties} and {@link JsonIgnore}
 * annotations. But in the following article <a href="http://www.baeldung.com/jackson-ignore-properties-on-serialization">Jackson Ignore
 * Properties on Marshalling</a>, more ways to ignore properties can be found, but there're not supported this time.
 *
 * @param typeElement type element// w  w w.j av a2  s  .  c  o m
 * @return not {@code null} list of ignored fields
 */
@NotNull
private static Set<String> getIgnoredFields(TypeElement typeElement) {
    if (typeElement == null)
        return Collections.emptySet();

    Set<String> res = new HashSet<>();
    JsonIgnoreProperties annotation = typeElement.getAnnotation(JsonIgnoreProperties.class);

    if (annotation != null && ArrayUtils.isNotEmpty(annotation.value()))
        Collections.addAll(res, annotation.value());

    JsonIgnore ann;

    for (Element element : typeElement.getEnclosedElements())
        if ((ann = element.getAnnotation(JsonIgnore.class)) != null && ann.value())
            res.add(element.toString());

    return res;
}

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./* w w w. j ava 2 s .c o m*/
 * 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:cop.raml.utils.ImportScanner.java

/**
 * Scan all root elements from the context {@link RoundEnvironment#getRootElements()} which is taken from {@link
 * ThreadLocalContext#getRoundEnv()}. All elements are stored from the previous method invoked will be cleared. All operation of import scanner
 * will be proceeded only with this elements set.
 * This method should be invoked at the beginning of the whole process once.
 *///from  ww  w .j  a va  2s.  c om
public void scanRootElements() {
    rootElements.clear();

    for (Element element : ThreadLocalContext.getRoundEnv().getRootElements())
        if (element instanceof TypeElement && !Config.ramlSkipClassName(element.toString()))
            rootElements.put(element.toString(), (TypeElement) element);
}

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 ava  2 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:org.shredzone.commons.taglib.processor.TaglibProcessor.java

/**
 * Processes a {@link Tag} annotation./* ww  w.j  a va 2 s.  com*/
 *
 * @param element
 *            Program element with that tag
 */
private void processTag(Element element) {
    Tag tagAnno = element.getAnnotation(Tag.class);
    String className = element.toString();
    String tagName = computeTagName(tagAnno.name(), className);

    // Try to evaluate the class name of the tag type
    String tagTypeClass = null;
    try {
        Class<? extends JspTag> tagType = tagAnno.type();
        tagTypeClass = tagType.getName();
    } catch (MirroredTypeException ex) {
        // This is a hack, see http://forums.sun.com/thread.jspa?threadID=791053
        tagTypeClass = ex.getTypeMirror().toString();
    }

    if (!PROXY_MAP.containsKey(tagTypeClass)) {
        throw new ProcessorException("No proxy for tag type " + tagTypeClass);
    }

    TagBean tag = new TagBean(tagName, className, tagAnno.bodycontent(), tagTypeClass);
    tag.setProxyClassName(className + "Proxy");

    if (StringUtils.hasText(tagAnno.bean())) {
        tag.setBeanName(tagAnno.bean());
    } else {
        tag.setBeanName(StringUtils.uncapitalize(StringUtils.unqualify(className)));
    }

    tag.setTryCatchFinally(tagAnno.tryCatchFinally());

    taglib.addTag(tag);
}

From source file:org.shredzone.commons.taglib.processor.TaglibProcessor.java

/**
 * Processes a {@link TagParameter} annotation.
 *
 * @param element//from ww w  .j  a  v  a  2s . c  om
 *            Program element with that tag
 */
private void processTagParameter(Element element) {
    TagParameter tagAnno = element.getAnnotation(TagParameter.class);
    String methodName = element.toString();
    String className = element.getEnclosingElement().toString();

    TagBean tag = taglib.getTagForClass(className);
    if (tag == null) {
        throw new ProcessorException("Missing @Tag on class: " + className);
    }

    Matcher m = METHOD_PATTERN.matcher(methodName);
    if (!m.matches()) {
        throw new ProcessorException("@TagParameter must be used on a setter method: " + methodName);
    }

    String attrName = StringUtils.uncapitalize(m.group(1));
    String attrType = m.group(2);

    if (attrType.indexOf(',') >= 0) {
        throw new ProcessorException("@TagParameter setter only allows one parameter: " + methodName);
    }

    AttributeBean attr = new AttributeBean(attrName, attrType, tagAnno.required(), tagAnno.rtexprvalue());
    tag.addAttribute(attr);
}

From source file:org.shredzone.commons.taglib.processor.TaglibProcessor.java

/**
 * Processes a {@link TagInfo} annotation.
 *
 * @param element/*from  w w  w . ja  v  a  2  s  .  com*/
 *            Program element with that tag
 */
private void processTagInfo(Element element) {
    TagInfo tagAnno = element.getAnnotation(TagInfo.class);

    if (element.getKind().equals(ElementKind.PACKAGE)) {
        taglib.setInfo(tagAnno.value());
        return;
    }

    String className = element.toString();

    TagBean tag = taglib.getTagForClass(className);
    if (tag == null) {
        throw new ProcessorException("Missing @Tag on class: " + className);
    }

    tag.setInfo(tagAnno.value());
}

From source file:com.dspot.declex.action.Actions.java

private void createInformationForMethods(Element typeElement, ActionInfo actionInfo,
        List<String> methodsHandled) {

    if (methodsHandled == null) {
        methodsHandled = new LinkedList<>();
    }/*from  ww w.  j a  v a2s. co m*/

    for (Element elem : typeElement.getEnclosedElements()) {

        if (elem.getKind() == ElementKind.METHOD) {
            if (methodsHandled.contains(elem.toString()))
                continue;

            final ExecutableElement element = (ExecutableElement) elem;

            List<ActionMethodParam> params = new LinkedList<>();
            for (VariableElement param : element.getParameters()) {

                List<Annotation> annotations = new LinkedList<>();
                for (Class<? extends Annotation> annotation : ACTION_ANNOTATION) {
                    Annotation containedAnnotation = param.getAnnotation(annotation);
                    if (containedAnnotation != null) {
                        annotations.add(containedAnnotation);
                    }
                }

                final AbstractJClass paramType = codeModelHelper.elementTypeToJClass(param);

                ActionMethodParam actionMethodParam = new ActionMethodParam(param.getSimpleName().toString(),
                        paramType, annotations);
                params.add(actionMethodParam);
            }

            List<Annotation> annotations = new LinkedList<>();
            for (Class<? extends Annotation> annotation : ACTION_ANNOTATION) {
                Annotation containedAnnotation = element.getAnnotation(annotation);
                if (containedAnnotation != null) {
                    annotations.add(containedAnnotation);
                }
            }

            String javaDoc = env.getProcessingEnvironment().getElementUtils().getDocComment(element);

            final String clazz = element.getReturnType().toString();

            actionInfo.addMethod(element.getSimpleName().toString(), clazz, javaDoc, params, annotations);

            methodsHandled.add(element.toString());
        }
    }

    List<? extends TypeMirror> superTypes = env.getProcessingEnvironment().getTypeUtils()
            .directSupertypes(typeElement.asType());
    for (TypeMirror type : superTypes) {
        TypeElement superElement = env.getProcessingEnvironment().getElementUtils()
                .getTypeElement(type.toString());
        if (superElement == null)
            continue;
        if (superElement.getKind().equals(ElementKind.INTERFACE))
            continue;
        if (superElement.asType().toString().equals(Object.class.getCanonicalName()))
            continue;
        createInformationForMethods(superElement, actionInfo, methodsHandled);
    }

}

From source file:org.shredzone.commons.taglib.processor.TaglibProcessor.java

/**
 * Processes a {@link BeanFactoryReference} annotation.
 *
 * @param element//from ww w  .j a  v  a2 s  . c o  m
 *            Program element with that tag
 */
private void processBeanFactoryReference(Element element) {
    BeanFactoryReference tagAnno = element.getAnnotation(BeanFactoryReference.class);

    if (element.getKind().equals(ElementKind.PACKAGE)) {
        if (taglib.getBeanFactoryReference() != null) {
            throw new ProcessorException("Package @BeanFactoryReference already defined");
        }

        taglib.setBeanFactoryReference(tagAnno.value());
        return;
    }

    String className = element.toString();
    TagBean tag = taglib.getTagForClass(className);
    if (tag == null) {
        throw new ProcessorException("Missing @Tag on class: " + className);
    }

    tag.setBeanFactoryReference(tagAnno.value());
}

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

/**
 * Go through a Jackson-annotated constructor, and produce {@link TransferClass}es representing
 * what we found.//from w  ww  .java 2  s  .c om
 */
private void processJsonPropertyAnnotations(final RoundEnvironment roundEnv) {
    final Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(JsonProperty.class);
    for (final Element e : elements) {
        if (e.getEnclosingElement() == null) {
            continue;
        }
        final Element parentElement = e.getEnclosingElement().getEnclosingElement();
        if (parentElement == null) {
            continue;
        }
        if (!(parentElement instanceof TypeElement)) {
            continue;
        }
        final TypeElement parent = (TypeElement) parentElement;
        final String parentJavaDoc = processingEnv.getElementUtils().getDocComment(parent);
        final String parentName = parent.getQualifiedName().toString();

        final TransferClass klass = getOrCreateTransferClass(parentName, parentJavaDoc);

        klass.add(e.toString(), makeTypeDescriptor(e.asType()));
    }
}