Search in sources :

Example 1 with GeneratedClassHolder

use of org.androidannotations.holder.GeneratedClassHolder in project androidannotations by androidannotations.

the class ModelProcessor method generateElements.

@SuppressWarnings({ "unchecked", "rawtypes" })
private boolean generateElements(AnnotationElements validatedModel, ProcessHolder processHolder) throws Exception {
    boolean isElementRemaining = false;
    for (GeneratingAnnotationHandler generatingAnnotationHandler : environment.getGeneratingHandlers()) {
        if (!generatingAnnotationHandler.isEnabled()) {
            continue;
        }
        String annotationName = generatingAnnotationHandler.getTarget();
        Set<? extends Element> annotatedElements = validatedModel.getRootAnnotatedElements(annotationName);
        if (!annotatedElements.isEmpty()) {
            LOGGER.debug("Processing root elements {}: {}", generatingAnnotationHandler.getClass().getSimpleName(), annotatedElements);
        }
        for (Element annotatedElement : annotatedElements) {
            /*
				 * We do not generate code for abstract classes, because the
				 * generated classes are final anyway (we do not want anyone to
				 * extend them).
				 */
            if (!isAbstractClass(annotatedElement)) {
                if (processHolder.getGeneratedClassHolder(annotatedElement) == null) {
                    TypeElement typeElement = (TypeElement) annotatedElement;
                    Element enclosingElement = annotatedElement.getEnclosingElement();
                    if (typeElement.getNestingKind() == NestingKind.MEMBER && processHolder.getGeneratedClassHolder(enclosingElement) == null) {
                        isElementRemaining = true;
                    } else {
                        GeneratedClassHolder generatedClassHolder = generatingAnnotationHandler.createGeneratedClassHolder(environment, typeElement);
                        processHolder.put(annotatedElement, generatedClassHolder);
                        generatingAnnotationHandler.process(annotatedElement, generatedClassHolder);
                    }
                }
            } else {
                LOGGER.trace("Skip element {} because it's abstract", annotatedElement);
            }
        }
    /*
			 * We currently do not take into account class annotations from
			 * ancestors. We should careful design the priority rules first.
			 */
    }
    return isElementRemaining;
}
Also used : GeneratedClassHolder(org.androidannotations.holder.GeneratedClassHolder) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) GeneratingAnnotationHandler(org.androidannotations.handler.GeneratingAnnotationHandler)

Example 2 with GeneratedClassHolder

use of org.androidannotations.holder.GeneratedClassHolder in project androidannotations by androidannotations.

the class PrefHandler method assignValue.

@Override
public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, EComponentHolder holder, Element element, Element param) {
    TypeMirror fieldTypeMirror = param.asType();
    AbstractJClass prefClass = getJClass(fieldTypeMirror.toString());
    String elementTypeName = fieldTypeMirror.toString();
    int index = elementTypeName.lastIndexOf(".");
    if (index != -1) {
        elementTypeName = elementTypeName.substring(index + 1);
    }
    Set<? extends Element> sharedPrefElements = getEnvironment().getValidatedElements().getRootAnnotatedElements(SharedPref.class.getName());
    for (Element sharedPrefElement : sharedPrefElements) {
        GeneratedClassHolder sharedPrefHolder = getEnvironment().getGeneratedClassHolder(sharedPrefElement);
        String sharedPrefName = sharedPrefHolder.getGeneratedClass().name();
        if (elementTypeName.equals(sharedPrefName)) {
            prefClass = sharedPrefHolder.getGeneratedClass();
            break;
        }
    }
    targetBlock.add(fieldRef.assign(JExpr._new(prefClass).arg(holder.getContextRef())));
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) GeneratedClassHolder(org.androidannotations.holder.GeneratedClassHolder) Element(javax.lang.model.element.Element) AbstractJClass(com.helger.jcodemodel.AbstractJClass) SharedPref(org.androidannotations.annotations.sharedpreferences.SharedPref)

Example 3 with GeneratedClassHolder

use of org.androidannotations.holder.GeneratedClassHolder in project androidannotations by androidannotations.

the class ModelProcessor method process.

@SuppressWarnings({ "unchecked", "rawtypes" })
public ProcessResult process(AnnotationElements validatedModel) throws Exception {
    ProcessHolder processHolder = new ProcessHolder(environment.getProcessingEnvironment());
    environment.setProcessHolder(processHolder);
    LOGGER.info("Processing root elements");
    /*
		 * We generate top classes then inner classes, then inner classes of
		 * inner classes, etc... until there is no more classes to generate.
		 */
    while (generateElements(validatedModel, processHolder)) {
        // CHECKSTYLE:OFF
        ;
    // CHECKSTYLE:ON
    }
    LOGGER.info("Processing enclosed elements");
    for (AnnotationHandler annotationHandler : environment.getDecoratingHandlers()) {
        if (!annotationHandler.isEnabled()) {
            continue;
        }
        String annotationName = annotationHandler.getTarget();
        /*
			 * For ancestors, the annotationHandler manipulates the annotated
			 * elements, but uses the holder for the root element
			 */
        Set<AnnotatedAndRootElements> ancestorAnnotatedElements = validatedModel.getAncestorAnnotatedElements(annotationName);
        if (!ancestorAnnotatedElements.isEmpty()) {
            LOGGER.debug("Processing enclosed elements with {}: {}", annotationHandler.getClass().getSimpleName(), ancestorAnnotatedElements);
        }
        for (AnnotatedAndRootElements elements : ancestorAnnotatedElements) {
            GeneratedClassHolder holder = processHolder.getGeneratedClassHolder(elements.rootTypeElement);
            /*
				 * Annotations coming from ancestors may be applied to root
				 * elements that are not validated, and therefore not available.
				 */
            if (holder != null) {
                processThrowing(annotationHandler, elements.annotatedElement, holder);
            }
        }
        Set<? extends Element> rootAnnotatedElements = validatedModel.getRootAnnotatedElements(annotationName);
        for (Element annotatedElement : rootAnnotatedElements) {
            Element enclosingElement;
            if (annotatedElement instanceof TypeElement) {
                enclosingElement = annotatedElement;
            } else {
                enclosingElement = annotatedElement.getEnclosingElement();
                /*
					 * we are processing a method parameter
					 */
                if (enclosingElement instanceof ExecutableElement) {
                    enclosingElement = enclosingElement.getEnclosingElement();
                }
            }
            /*
				 * We do not generate code for elements belonging to abstract
				 * classes, because the generated classes are final anyway
				 */
            if (!isAbstractClass(enclosingElement)) {
                GeneratedClassHolder holder = processHolder.getGeneratedClassHolder(enclosingElement);
                /*
					 * The holder can be null if the annotated holder class is
					 * already invalidated.
					 */
                if (holder != null) {
                    processThrowing(annotationHandler, annotatedElement, holder);
                }
            } else {
                LOGGER.trace("Skip element {} because enclosing element {} is abstract", annotatedElement, enclosingElement);
            }
        }
    }
    return new //
    ProcessResult(//
    processHolder.codeModel(), processHolder.getOriginatingElements());
}
Also used : GeneratedClassHolder(org.androidannotations.holder.GeneratedClassHolder) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) AnnotationHandler(org.androidannotations.handler.AnnotationHandler) GeneratingAnnotationHandler(org.androidannotations.handler.GeneratingAnnotationHandler) AnnotatedAndRootElements(org.androidannotations.internal.model.AnnotationElements.AnnotatedAndRootElements)

Aggregations

Element (javax.lang.model.element.Element)3 GeneratedClassHolder (org.androidannotations.holder.GeneratedClassHolder)3 ExecutableElement (javax.lang.model.element.ExecutableElement)2 TypeElement (javax.lang.model.element.TypeElement)2 GeneratingAnnotationHandler (org.androidannotations.handler.GeneratingAnnotationHandler)2 AbstractJClass (com.helger.jcodemodel.AbstractJClass)1 TypeMirror (javax.lang.model.type.TypeMirror)1 SharedPref (org.androidannotations.annotations.sharedpreferences.SharedPref)1 AnnotationHandler (org.androidannotations.handler.AnnotationHandler)1 AnnotatedAndRootElements (org.androidannotations.internal.model.AnnotationElements.AnnotatedAndRootElements)1