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

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

Introduction

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

Prototype

TypeMirror asType();

Source Link

Document

Returns the type defined by this element.

Usage

From source file:com.wesleyhome.dao.processor.CodeGenerationHelper.java

/**
 * @param model// w  w  w .  j av a  2  s.  co  m
 * @param method
 * @param element
 */
public static void addMethodParameter(final JCodeModel model, final JMethod method, final Element element) {
    TypeMirror fieldTypeMirror = element.asType();
    String fieldTypeString = fieldTypeMirror.toString();
    JClass fieldType = model.ref(fieldTypeString);
    addMethodParameters(method, fieldType, element.getSimpleName().toString());
}

From source file:com.dspot.declex.override.holder.FragmentActionHolder.java

public static void createInformationForActionHolder(Element element, AndroidAnnotationsEnvironment env) {

    final String clsName = element.asType().toString();
    final int index = clsName.lastIndexOf('.');
    final String pkg = clsName.substring(0, index);
    final String fragmentName = clsName.substring(index + 1);
    final String actionName = pkg + "." + fragmentName + "ActionHolder";

    ActionInfo actionInfo = new ActionInfo(actionName);
    actionInfo.isTimeConsuming = false;//from  w  w  w.j a v a2s . c  o m

    //This will avoid generation for parent classes, not used in the project
    actionInfo.generated = false;

    actionInfo.setReferences(JavaDocUtils.referenceFromClassName(clsName));
    Actions.getInstance().addAction(fragmentName, actionName, actionInfo);

    actionInfo.addMethod(CONTAINER_NAME, actionName);

    //FragmentTransaction can change the package
    actionInfo.addMethod(TRANSACTION_NAME, "android.app.FragmentTransaction");

    actionInfo.addMethod(REPLACE_NAME, actionName);

    actionInfo.addMethod(ADD_NAME, actionName);

    actionInfo.addMethod(INIT_NAME, env.getCodeModel().VOID.fullName());
    actionInfo.addMethod(INIT_NAME, env.getCodeModel().VOID.fullName(),
            Arrays.asList(new ActionMethodParam("tag", env.getClasses().STRING)));

    actionInfo.addMethod(BUILD_NAME, env.getCodeModel().VOID.fullName(),
            Arrays.asList(new ActionMethodParam("Started", env.getJClass(Runnable.class))));

    actionInfo.addMethod(EXECUTE_NAME, env.getCodeModel().VOID.fullName());

    actionInfo.addMethod(ADD_TO_BACK_STACK_NAME, actionName);

    actionInfo.addMethod(BUILDER_NAME, actionName + ".FragmentBuilder" + generationSuffix());

}

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

/**
 * Determines whether or not the Element is a concrete type.
 * If the element is a generic type or contains generic type
 * arguments, this method will return false.
 *
 * @param element the element to check.// w  w w. j  av  a 2  s . c  o  m
 * @return true if the element is not generic and
 * contains no generic type arguments, false otherwise.
 */
public static boolean isConcreteType(@NotNull Element element) {
    return isConcreteType(element.asType());
}

From source file:org.drombler.acp.core.docking.impl.DockingAnnotationProcessor.java

private void configureDocking(AbstractDockingType docking, Element element, String areaId, String icon,
        DockingState dockingState) {/*from w  ww  .  ja  va  2 s .com*/
    docking.setId(element.asType().toString());
    docking.setAreaId(StringUtils.stripToNull(areaId));
    docking.setIcon(StringUtils.stripToNull(icon));
    docking.setState(DOCKING_STATES.get(dockingState));
    docking.setDockableClass(element.asType().toString());
}

From source file:me.oriley.shiv.holders.ViewBindingHolder.java

@NonNull
private CodeBlock generateProtectedBindViewsMethod() throws ShivException {
    CodeBlock.Builder builder = CodeBlock.builder()
            .add("$T $N = ($T) $N;\n", mHostType, FIELD_HOST, mHostType, OBJECT)
            .add("int size = $N.getChildCount();\n", VIEW_GROUP).add("int $N = 0;\n", BOUND)
            .beginControlFlow("for (int i = 0; i < size; i++)")
            .add("$T $N = $N.getChildAt(i);\n", View.class, VIEW, VIEW_GROUP)
            .beginControlFlow("if ($N instanceof $T)", VIEW, ViewGroup.class)
            .add("$N += $N($N, ($T) $N);\n", BOUND, BIND_VIEWS, OBJECT, ViewGroup.class, VIEW).endControlFlow()
            .beginControlFlow("switch ($N.getId())", VIEW);

    for (Element element : mElements) {
        builder.add("case $L:\n", element.getAnnotation(BindView.class).value())
                .add("    $N.$N = ($T) $N;\n", FIELD_HOST, element.getSimpleName(), element.asType(), VIEW)
                .add("    $N++;\n", BOUND).add("    break;\n");
    }/*w w  w . j  a  v a2  s  .  co m*/

    builder.endControlFlow().beginControlFlow("if ($N >= $N)", BOUND, VIEW_COUNT).add("break;\n")
            .endControlFlow().endControlFlow().add("return $N;\n", BOUND);

    return builder.build();
}

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  .  ja va 2  s  .  co m
 *
 * @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:com.wesleyhome.dao.processor.method.IncludeQueryMethodGenerator.java

private JClass getParameterType(final JCodeModel model, final JClass fieldType,
        final EntityInformationMap entityInfoMap, final MappingType mappingType) {
    JClass parameterType = null;//from   ww  w. ja  v  a 2  s .  c  o m
    String fullName = fieldType.fullName();
    EntityInfo foreignKeyInfo = entityInfoMap.get(fullName);
    List<Element> foreignKeyIdElms = foreignKeyInfo == null ? null : foreignKeyInfo.getIdElements();
    if (foreignKeyIdElms == null || foreignKeyIdElms.size() > 1) {
        parameterType = fieldType;
    } else {
        switch (mappingType) {
        case UNIQUE_OBJECT:
        case OBJECT:
            parameterType = fieldType;
            break;
        case ONE_TO_ONE:
        case MANY_TO_ONE:
        default:
            Element element = foreignKeyIdElms.get(0);
            TypeMirror fieldTypeMirror = element.asType();
            String fieldTypeString = fieldTypeMirror.toString();
            parameterType = model.ref(fieldTypeString);
            break;
        }
    }
    return parameterType;
}

From source file:com.webcohesion.enunciate.modules.jackson.JacksonModule.java

/**
 * Whether the specified declaration is throwable.
 *
 * @param declaration The declaration to determine whether it is throwable.
 * @return Whether the specified declaration is throwable.
 *///from w w  w.j  a  v  a 2 s .  co m
protected boolean isThrowable(Element declaration) {
    return declaration.getKind() == ElementKind.CLASS
            && ((DecoratedTypeMirror) declaration.asType()).isInstanceOf(Throwable.class);
}

From source file:com.rgeldmacher.leash.LeashAnnotationProcessor.java

private boolean typeIsActivityOrFragment(Element type) {
    return type != null && (types.isAssignable(type.asType(),
            elements.getTypeElement("android.app.Activity").asType())
            || types.isAssignable(type.asType(), elements.getTypeElement("android.app.Fragment").asType())
            || types.isAssignable(type.asType(),
                    elements.getTypeElement("android.support.v4.app.FragmentActivity").asType())
            || types.isAssignable(type.asType(),
                    elements.getTypeElement("android.support.v4.app.Fragment").asType()));
}

From source file:com.rgeldmacher.leash.LeashAnnotationProcessor.java

private MethodSpec createClearMethodSpec(TypeElement classWithAnnotations, Set<Element> annotatedFields,
        ClassName retainedFragmentType, MethodSpec getRetainedFragmentMethodSpec) {
    MethodSpec.Builder builder = MethodSpec.methodBuilder("clear").addModifiers(Modifier.PUBLIC,
            Modifier.STATIC);/*  w  w w .ja v a2 s .  co m*/

    addGetRetainedFragmentSnippet(builder, classWithAnnotations, retainedFragmentType,
            getRetainedFragmentMethodSpec);

    builder.beginControlFlow("if (retainedFragment != null)")
            .addStatement("retainedFragment.hasBeenRetained = false");
    for (Element field : annotatedFields) {
        if (typeIsPrimitive(field.asType())) {
            builder.addStatement("retainedFragment.$L = $L", field.getSimpleName().toString(),
                    getPrimitiveDefault(field.asType()));
        } else {
            builder.addStatement("retainedFragment.$L = null", field.getSimpleName().toString());
        }
    }

    builder.endControlFlow();

    return builder.build();
}