package com.carrotcreative.recyclercore.compiler;

import com.carrotcreative.recyclercore.annotations.RCModel;
import com.squareup.javapoet.ClassName;

import javax.lang.model.element.TypeElement;
import javax.lang.model.type.MirroredTypeException;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Types;

/**
 * Created by kaushiksaurabh on 9/20/16.
 */

class ModelDetails
{
    private final ClassName mClassName;
    private final String mCanonicalName;
    private final String mControllerCanonicalName;

    private ModelDetails(ClassName className, String cannonicalName, String controllerCanonicalName)
    {
        mClassName = className;
        mCanonicalName = cannonicalName;
        mControllerCanonicalName = controllerCanonicalName;
    }

    public ClassName getClassName()
    {
        return mClassName;
    }

    public String getCanonicalName()
    {
        return mCanonicalName;
    }

    public String getControllerCanonicalName()
    {
        return mControllerCanonicalName;
    }

    static ModelDetails fromTypeElement(TypeElement typeElement, Types typesUtil)
    {
        ClassName clsName = ClassName.get(typeElement);
        String cannonName = ClassUtil.getCanonicalName(clsName);
        String controllerCanonicalName = null;
        try
        {
            RCModel annotation = typeElement.getAnnotation(RCModel.class);
            annotation.controller();
        }
        catch(MirroredTypeException ex)
        {
            TypeMirror typeMirror = ex.getTypeMirror();
            TypeElement tElement = (TypeElement) typesUtil.asElement(typeMirror);
            ClassName controllerCls = ClassName.get(tElement);
            controllerCanonicalName = ClassUtil.getCanonicalName(controllerCls);
        }

        return new ModelDetails(clsName, cannonName, controllerCanonicalName);
    }
}

@@ -1025,24 +1025,1153 @@
 ame;%0A    %7D%0A%0A
+    /**%0A     * When the annotation value is a Class, accessing the class value is not straightforward.%0A     * During the pre processing state, Javac does not load classes that are in the source.%0A     * So when we call annotation.controller() it fails with MirroredTypeException.%0A     * There are two possible workarounds for this.%0A     * %3Cp%3E%0A     * Method 1. use getAnnotationMirrors : When getAnnotationMirrors is used it returns a set of%0A     * AnnotationMirror instances, one for each annotation.%0A     * Then locate the correct AnnotationMirror for the required Annotation.%0A     * Then extract the Annotation Value fron the mirror.%0A     * %3Cp%3E%0A     * Method 2. Catch the MirroredTypeException. The exception contains the TypeMirror of the%0A     * class we need.%0A     * We use the second method.%0A     *%0A     * ref : https://blog.retep.org/2009/02/13/getting-class-values-from-annotations-in-an-annotationprocessor/%0A     *%0A     * @param typeElement Represents a class or Interface program element%0A     * @param typesUtil   Provides utility methods for operating on types.%0A     * @return new instance of ModelDetails class%0A     */%0A
     static M

Add comment for MirroredTypeException