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

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

Introduction

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

Prototype

Name getQualifiedName();

Source Link

Document

Returns the fully qualified name of this type element.

Usage

From source file:uniol.apt.compiler.AbstractServiceProcessor.java

private boolean isValidClass(Element ele) {
    if (ele.getKind() != ElementKind.CLASS) {
        error(ele, "Non-Class %s annotated with %s.", ele.getSimpleName().toString(),
                this.annotationClass.getCanonicalName());
    }/*  ww  w .  j a  va2  s  .c om*/

    TypeElement classEle = (TypeElement) ele;

    if (!classEle.getModifiers().contains(Modifier.PUBLIC)) {
        error(classEle, "Class %s is not public.", classEle.getQualifiedName().toString());
        return false;
    }

    if (classEle.getModifiers().contains(Modifier.ABSTRACT)) {
        error(classEle, "Class %s is abstract.", classEle.getQualifiedName().toString());
        return false;
    }

    TypeMirror expected = this.types.erasure(this.elements.getTypeElement(this.interfaceName).asType());
    boolean found = false;
    for (TypeMirror actual : classEle.getInterfaces()) {
        if (this.types.isAssignable(actual, expected)) {
            found = true;
            break;
        }
    }
    if (!found) {
        error(classEle, "Class %s doesn't implement interface %s.", classEle.getQualifiedName().toString(),
                this.interfaceName);
        return false;
    }

    if (!allowGenerics && !classEle.getTypeParameters().isEmpty()) {
        error(classEle, "Class %s is generic.", classEle.getQualifiedName().toString());
        return false;
    }

    for (Element enclosed : classEle.getEnclosedElements()) {
        if (enclosed.getKind() == ElementKind.CONSTRUCTOR) {
            ExecutableElement constructorEle = (ExecutableElement) enclosed;
            if (constructorEle.getParameters().size() == 0
                    && constructorEle.getModifiers().contains(Modifier.PUBLIC)) {
                return true;
            }
        }
    }

    error(classEle, String.format("Class %s needs an public no-arg constructor",
            classEle.getQualifiedName().toString()));
    return false;
}

From source file:org.commonjava.vertx.vabr.anno.proc.AbstractTemplateInfo.java

protected AbstractTemplateInfo(final Element elem, final Handles handles, final int priority,
        final Method method, final String path, final String defPath, final String[] routeVersions,
        final boolean fork) {
    this.priority = priority;
    this.httpMethod = method;
    this.fork = fork;
    this.httpPath = AnnotationUtils.pathOf(handles, path, defPath);
    this.routePathFragment = isEmpty(path) ? defPath : path;
    this.handlerPathFragment = isEmpty(handles.prefix()) ? handles.value() : handles.prefix();

    // it only applies to methods...
    final ExecutableElement eelem = (ExecutableElement) elem;

    methodname = eelem.getSimpleName().toString();

    final TypeElement cls = (TypeElement) eelem.getEnclosingElement();

    Element parent = cls.getEnclosingElement();
    while (parent.getKind() != ElementKind.PACKAGE) {
        parent = parent.getEnclosingElement();
    }// ww  w.  j  ava2  s.c o  m

    final PackageElement pkg = (PackageElement) parent;

    qualifiedClassname = cls.getQualifiedName().toString();

    classname = cls.getSimpleName().toString();

    packagename = pkg.getQualifiedName().toString();

    this.handlerKey = AnnotationUtils.getHandlerKey(handles, qualifiedClassname);

    this.versions = new ArrayList<>();
    if (routeVersions != null && routeVersions.length > 0) {
        for (final String rv : routeVersions) {
            this.versions.add(rv);
        }
    } else {
        final String[] handlerVersions = handles.versions();
        if (handlerVersions != null) {
            for (final String rv : handlerVersions) {
                this.versions.add(rv);
            }
        }
    }
}

From source file:net.pkhsolutions.ceres.common.builder.processor.BuildableAP.java

private void createConstructorBuilder(TypeElement type) {
    ExecutableElement constructor = getBuilderConstructor(type);
    VelocityContext vc = createAndInitializeVelocityContext(type);
    vc.put("properties", createPropertyList(constructor.getParameters()));
    createSourceFile(type.getQualifiedName() + "Builder", constructorBuilderTemplate, vc);
}

From source file:com.contentful.vault.compiler.Processor.java

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    for (Injection injection : findAndParseTargets(roundEnv)) {
        try {//  w w w  .jav  a 2s . c  o m
            injection.brewJava().writeTo(filer);
        } catch (Exception e) {
            TypeElement element = injection.originatingElement;
            error(element, "Failed writing injection for \"%s\", message: %s", element.getQualifiedName(),
                    e.getMessage());
        }
    }
    return true;
}

From source file:org.androidtransfuse.adapter.element.ASTElementFactory.java

private PackageClass buildPackageClass(TypeElement typeElement) {

    PackageElement packageElement = elements.getPackageOf(typeElement);

    String pkg = packageElement.getQualifiedName().toString();
    String qualifiedName = typeElement.getQualifiedName().toString();
    String name;//from   w ww .j  a  v a2 s .c  o  m
    if (!pkg.isEmpty() && pkg.length() < qualifiedName.length()) {
        name = qualifiedName.substring(pkg.length() + 1);
    } else {
        name = qualifiedName;
    }

    return new PackageClass(pkg, name);
}

From source file:com.github.hackersun.processor.factory.FactoryAnnotatedClass.java

/**
 * @throws ProcessingException if id() from annotation is null
 *///from w  w w  .j  a v a  2  s .c om
public FactoryAnnotatedClass(TypeElement classElement) throws ProcessingException {
    this.annotatedClassElement = classElement;
    Factory annotation = classElement.getAnnotation(Factory.class);
    id = annotation.id();

    if (StringUtils.isEmpty(id)) {
        throw new ProcessingException(classElement,
                "id() in @%s for class %s is null or empty! that's not allowed", Factory.class.getSimpleName(),
                classElement.getQualifiedName().toString());
    }

    // Get the full QualifiedTypeName
    try {
        Class<?> clazz = annotation.type();
        qualifiedGroupClassName = clazz.getCanonicalName();
        simpleFactoryGroupName = clazz.getSimpleName();
    } catch (MirroredTypeException mte) {
        DeclaredType classTypeMirror = (DeclaredType) mte.getTypeMirror();
        TypeElement classTypeElement = (TypeElement) classTypeMirror.asElement();
        qualifiedGroupClassName = classTypeElement.getQualifiedName().toString();
        simpleFactoryGroupName = classTypeElement.getSimpleName().toString();
    }
}

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

/**
 * Go through a Jackson-annotated constructor, and produce {@link TransferClass}es representing
 * what we found./*from   ww  w .ja  v  a 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()));
    }
}

From source file:com.contentful.vault.compiler.Processor.java

private void parseContentType(TypeElement element, Map<TypeElement, ModelInjection> models) {
    String id = element.getAnnotation(ContentType.class).value();
    if (id.isEmpty()) {
        error(element, "@%s id may not be empty. (%s)", ContentType.class.getSimpleName(),
                element.getQualifiedName());
        return;//from   ww w .  ja  va  2  s.co m
    }

    if (!isSubtypeOfType(element.asType(), Resource.class.getName())) {
        error(element, "Classes annotated with @%s must extend \"" + Resource.class.getName() + "\". (%s)",
                ContentType.class.getSimpleName(), element.getQualifiedName());
        return;
    }

    Set<FieldMeta> fields = new LinkedHashSet<>();
    Set<String> memberIds = new LinkedHashSet<>();
    for (Element enclosedElement : element.getEnclosedElements()) {
        Field field = enclosedElement.getAnnotation(Field.class);
        if (field == null) {
            continue;
        }

        String fieldId = field.value();
        if (fieldId.isEmpty()) {
            fieldId = enclosedElement.getSimpleName().toString();
        }

        Set<Modifier> modifiers = enclosedElement.getModifiers();
        if (modifiers.contains(Modifier.STATIC)) {
            error(element, "@%s elements must not be static. (%s.%s)", Field.class.getSimpleName(),
                    element.getQualifiedName(), enclosedElement.getSimpleName());
            return;
        }
        if (modifiers.contains(Modifier.PRIVATE)) {
            error(element, "@%s elements must not be private. (%s.%s)", Field.class.getSimpleName(),
                    element.getQualifiedName(), enclosedElement.getSimpleName());
            return;
        }

        if (!memberIds.add(fieldId)) {
            error(element, "@%s for the same id (\"%s\") was used multiple times in the same class. (%s)",
                    Field.class.getSimpleName(), fieldId, element.getQualifiedName());
            return;
        }

        FieldMeta.Builder fieldBuilder = FieldMeta.builder();
        if (isList(enclosedElement)) {
            DeclaredType declaredType = (DeclaredType) enclosedElement.asType();
            List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
            if (typeArguments.size() == 0) {
                error(element, "Array fields must have a type parameter specified. (%s.%s)",
                        element.getQualifiedName(), enclosedElement.getSimpleName());
                return;
            }

            TypeMirror arrayType = typeArguments.get(0);
            if (!isValidListType(arrayType)) {
                error(element, "Invalid list type \"%s\" specified. (%s.%s)", arrayType.toString(),
                        element.getQualifiedName(), enclosedElement.getSimpleName());
                return;
            }

            String sqliteType = null;
            if (String.class.getName().equals(arrayType.toString())) {
                sqliteType = SqliteUtils.typeForClass(List.class.getName());
            }

            fieldBuilder.setSqliteType(sqliteType).setArrayType(arrayType.toString());
        } else {
            TypeMirror enclosedType = enclosedElement.asType();
            String linkType = getLinkType(enclosedType);
            String sqliteType = null;
            if (linkType == null) {
                sqliteType = SqliteUtils.typeForClass(enclosedType.toString());
                if (sqliteType == null) {
                    error(element, "@%s specified for unsupported type (\"%s\"). (%s.%s)",
                            Field.class.getSimpleName(), enclosedType.toString(), element.getQualifiedName(),
                            enclosedElement.getSimpleName());
                    return;
                }
            }

            fieldBuilder.setSqliteType(sqliteType).setLinkType(linkType);
        }

        fields.add(fieldBuilder.setId(fieldId).setName(enclosedElement.getSimpleName().toString())
                .setType(enclosedElement.asType()).build());
    }

    if (fields.size() == 0) {
        error(element, "Model must contain at least one @%s element. (%s)", Field.class.getSimpleName(),
                element.getQualifiedName());
        return;
    }

    ClassName injectionClassName = getInjectionClassName(element, SUFFIX_MODEL);
    String tableName = "entry_" + SqliteUtils.hashForId(id);
    models.put(element, new ModelInjection(id, injectionClassName, element, tableName, fields));
}

From source file:com.thoratou.exact.processors.ExactProcessor.java

private void readAnnotations(RoundEnvironment roundEnv, HashMap<String, ArrayList<Item>> itemMap,
        HashMap<String, ArrayList<ExtensionItem>> extItemMap) throws Exception {
    //retrieve all classes with @ExactNode annotation
    for (TypeElement typeElement : ElementFilter.typesIn(roundEnv.getElementsAnnotatedWith(ExactNode.class))) {
        Name qualifiedName = typeElement.getQualifiedName();
        String className = qualifiedName.toString();
        logger.info("Exact class : " + className);
        classMap.put(className, typeElement);

        for (ExecutableElement methodElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) {
            {//from  ww w  .  j  av  a 2s.  co  m
                ExactPath annotation = methodElement.getAnnotation(ExactPath.class);
                if (annotation != null) {
                    String methodName = methodElement.getSimpleName().toString();
                    String returnType = methodElement.getReturnType().toString();
                    String xPathString = annotation.value();

                    logger.info(
                            "Exact method : " + methodName + " , " + annotation.value() + " , " + returnType);

                    XPathParser parser = new XPathParser(xPathString);
                    XPathPathExpr xPathPathExpr = parser.parse();

                    logger.info("XPath value = " + xPathPathExpr.toString());

                    Item item = new Item();
                    item.setxPathPathExpr(xPathPathExpr);
                    item.setMethodName(methodName);
                    item.setReturnType(returnType);

                    if (itemMap.containsKey(className)) {
                        ArrayList<Item> items = itemMap.get(className);
                        items.add(item);
                    } else {
                        ArrayList<Item> items = new ArrayList<Item>();
                        items.add(item);
                        itemMap.put(className, items);
                    }

                    methodMap.put(new Pair<String, String>(className, methodName), methodElement);
                }
            }

            {
                ExactExtension annotation = methodElement.getAnnotation(ExactExtension.class);
                if (annotation != null) {
                    String methodName = methodElement.getSimpleName().toString();
                    String returnType = methodElement.getReturnType().toString();
                    String name = annotation.name();
                    String element = annotation.element();
                    String filter = annotation.filter();

                    logger.info("Exact extension : " + methodName + " , " + returnType + " , " + name + " , "
                            + element + " , " + filter);

                    XPathParser elementParser = new XPathParser(element);
                    XPathPathExpr elementXPathPathExpr = elementParser.parse();
                    logger.info("XPath element = " + elementXPathPathExpr.toString());

                    XPathParser filterParser = new XPathParser(filter);
                    XPathPathExpr filterXPathPathExpr = filterParser.parse();
                    logger.info("XPath filter = " + filterXPathPathExpr.toString());

                    ExtensionItem item = new ExtensionItem();
                    item.setName(name);
                    item.setElement(elementXPathPathExpr);
                    item.setFilter(filterXPathPathExpr);
                    item.setMethodName(methodName);
                    item.setReturnType(returnType);

                    if (extItemMap.containsKey(className)) {
                        ArrayList<ExtensionItem> items = extItemMap.get(className);
                        items.add(item);
                    } else {
                        ArrayList<ExtensionItem> items = new ArrayList<ExtensionItem>();
                        items.add(item);
                        extItemMap.put(className, items);
                    }
                }
            }
        }
    }
}

From source file:com.webcohesion.enunciate.modules.java_json_client.JavaJSONClientModule.java

/**
 * Whether to use the server-side declaration for this declaration.
 *
 * @param declaration The declaration.// w  w w . j av a 2 s  . co m
 * @param matcher     The matcher.
 * @return Whether to use the server-side declaration for this declaration.
 */
protected boolean useServerSide(TypeElement declaration, AntPatternMatcher matcher) {
    boolean useServerSide = false;

    for (String pattern : getServerSideTypesToUse()) {
        if (matcher.match(pattern, declaration.getQualifiedName().toString())) {
            useServerSide = true;
            break;
        }
    }
    return useServerSide;
}