Search in sources :

Example 6 with JDefinedClass

use of com.helger.jcodemodel.JDefinedClass in project adt4j by sviperll.

the class FinalValueClassModel method buildFactoryClass.

private JDefinedClass buildFactoryClass(Map<String, JMethod> constructorMethods) throws JClassAlreadyExistsException {
    JDefinedClass factoryClass = environment.buildValueClassInnerClass(JMod.PRIVATE | JMod.STATIC, environment.valueClassName() + "Factory", EClassType.CLASS);
    for (JTypeVar visitorTypeParameter : environment.getValueTypeParameters()) {
        JTypeVar typeParameter = factoryClass.generify(visitorTypeParameter.name());
        typeParameter.boundLike(visitorTypeParameter);
    }
    AbstractJClass usedValueClassType = environment.wrappedValueClassType(factoryClass.typeParams());
    VisitorDefinition.VisitorUsage usedVisitor = environment.visitor(usedValueClassType, usedValueClassType, types._RuntimeException);
    factoryClass._implements(usedVisitor.getVisitorType());
    for (MethodUsage interfaceMethod : usedVisitor.methods()) {
        JMethod factoryMethod = factoryClass.method(interfaceMethod.mods().getValue() & ~JMod.ABSTRACT, usedValueClassType, interfaceMethod.name());
        for (JTypeVar visitorMethodTypeParameter : interfaceMethod.typeParams()) {
            JTypeVar typeParameter = factoryMethod.generify(visitorMethodTypeParameter.name());
            typeParameter.boundLike(visitorMethodTypeParameter);
        }
        MethodUsage usedInterfaceMethod = interfaceMethod.narrow(factoryMethod.typeParams());
        Source.annotateNonnull(factoryMethod);
        factoryMethod.annotate(Override.class);
        List<AbstractJClass> typeArguments = new ArrayList<>();
        typeArguments.addAll(factoryClass.typeParamList());
        typeArguments.addAll(factoryMethod.typeParamList());
        JMethod constructorMethod = constructorMethods.get(interfaceMethod.name());
        JInvocation staticInvoke = environment.invokeValueClassStaticMethod(constructorMethod, typeArguments.toArray(new AbstractJClass[typeArguments.size()]));
        for (VariableDeclaration param : usedInterfaceMethod.params()) {
            JVar argument = factoryMethod.param(param.mods().getValue(), param.type().declarable(), param.name());
            staticInvoke.arg(argument);
        }
        VariableDeclaration param = usedInterfaceMethod.varParam();
        if (param != null) {
            JVar argument = factoryMethod.varParam(param.mods().getValue(), param.type().elementType().declarable(), param.name());
            staticInvoke.arg(argument);
        }
        factoryMethod.body()._return(staticInvoke);
    }
    return factoryClass;
}
Also used : VisitorDefinition(com.github.sviperll.adt4j.model.config.VisitorDefinition) JDefinedClass(com.helger.jcodemodel.JDefinedClass) JTypeVar(com.helger.jcodemodel.JTypeVar) ArrayList(java.util.ArrayList) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) VariableDeclaration(com.github.sviperll.adt4j.model.config.VariableDeclaration) MethodUsage(com.github.sviperll.adt4j.model.config.VisitorDefinition.MethodUsage) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Example 7 with JDefinedClass

use of com.helger.jcodemodel.JDefinedClass in project androidannotations by androidannotations.

the class EActivityHolder method setOnRetainNonConfigurationInstance.

private void setOnRetainNonConfigurationInstance() throws JClassAlreadyExistsException {
    AnnotationHelper annotationHelper = new AnnotationHelper(getEnvironment());
    TypeElement fragmentActivityTypeElement = getFragmentActivity(annotationHelper);
    TypeElement typeElement = annotationHelper.typeElementFromQualifiedName(generatedClass._extends().fullName());
    String onRetainNonConfigurationInstanceName = "onRetainNonConfigurationInstance";
    if (fragmentActivityTypeElement != null && annotationHelper.isSubtype(typeElement.asType(), fragmentActivityTypeElement.asType())) {
        onRetainNonConfigurationInstanceName = "onRetainCustomNonConfigurationInstance";
    }
    NonConfigurationHolder ncHolder = getNonConfigurationHolder();
    JDefinedClass ncHolderClass = ncHolder.getGeneratedClass();
    JMethod onRetainNonConfigurationInstanceMethod = generatedClass.method(PUBLIC, ncHolderClass, onRetainNonConfigurationInstanceName);
    onRetainNonConfigurationInstanceMethod.annotate(Override.class);
    JBlock methodBody = onRetainNonConfigurationInstanceMethod.body();
    onRetainNonConfigurationInstance = methodBody.decl(ncHolderClass, "nonConfigurationInstanceState_", _new(ncHolderClass));
    IJExpression superCall = _super().invoke(onRetainNonConfigurationInstanceMethod);
    methodBody.assign(onRetainNonConfigurationInstance.ref(ncHolder.getSuperNonConfigurationInstanceField()), superCall);
    onRetainNonConfigurationInstanceBindBlock = methodBody.blockSimple();
    methodBody._return(onRetainNonConfigurationInstance);
}
Also used : JDefinedClass(com.helger.jcodemodel.JDefinedClass) AnnotationHelper(org.androidannotations.helper.AnnotationHelper) TypeElement(javax.lang.model.element.TypeElement) JBlock(com.helger.jcodemodel.JBlock) IJExpression(com.helger.jcodemodel.IJExpression) JMethod(com.helger.jcodemodel.JMethod)

Example 8 with JDefinedClass

use of com.helger.jcodemodel.JDefinedClass in project androidannotations by androidannotations.

the class APTCodeModelHelper method createDelegatingAnonymousRunnableClass.

public JDefinedClass createDelegatingAnonymousRunnableClass(JBlock previousBody) {
    JCodeModel codeModel = environment.getCodeModel();
    JDefinedClass anonymousRunnableClass = codeModel.anonymousClass(Runnable.class);
    JMethod runMethod = anonymousRunnableClass.method(JMod.PUBLIC, codeModel.VOID, "run");
    runMethod.annotate(Override.class);
    runMethod.body().add(previousBody);
    return anonymousRunnableClass;
}
Also used : JDefinedClass(com.helger.jcodemodel.JDefinedClass) JMethod(com.helger.jcodemodel.JMethod) JCodeModel(com.helger.jcodemodel.JCodeModel)

Example 9 with JDefinedClass

use of com.helger.jcodemodel.JDefinedClass in project androidannotations by androidannotations.

the class EComponentWithViewSupportHolder method createTextWatcherHolder.

private TextWatcherHolder createTextWatcherHolder(JFieldRef idRef, TypeMirror viewParameterType) {
    JDefinedClass onTextChangeListenerClass = getCodeModel().anonymousClass(getClasses().TEXT_WATCHER);
    AbstractJClass viewClass = getClasses().TEXT_VIEW;
    if (viewParameterType != null) {
        viewClass = getJClass(viewParameterType.toString());
    }
    JBlock onViewChangedBody = getOnViewChangedBodyInjectionBlock().blockSimple();
    JVar viewVariable = onViewChangedBody.decl(FINAL, viewClass, "view", cast(viewClass, findViewById(idRef)));
    // 
    onViewChangedBody._if(viewVariable.ne(JExpr._null()))._then().invoke(viewVariable, "addTextChangedListener").arg(_new(onTextChangeListenerClass));
    return new TextWatcherHolder(this, viewVariable, onTextChangeListenerClass);
}
Also used : JDefinedClass(com.helger.jcodemodel.JDefinedClass) JBlock(com.helger.jcodemodel.JBlock) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JVar(com.helger.jcodemodel.JVar)

Example 10 with JDefinedClass

use of com.helger.jcodemodel.JDefinedClass in project androidannotations by androidannotations.

the class UiThreadHandler method process.

@Override
public void process(Element element, EComponentHolder holder) throws Exception {
    ExecutableElement executableElement = (ExecutableElement) element;
    JMethod delegatingMethod = codeModelHelper.overrideAnnotatedMethod(executableElement, holder);
    JBlock previousBody = codeModelHelper.removeBody(delegatingMethod);
    JDefinedClass anonymousRunnableClass = codeModelHelper.createDelegatingAnonymousRunnableClass(previousBody);
    UiThread annotation = element.getAnnotation(UiThread.class);
    long delay = annotation.delay();
    UiThread.Propagation propagation = annotation.propagation();
    if (delay == 0 && propagation == UiThread.Propagation.REUSE) {
        // Put in the check for the UI thread.
        addUIThreadCheck(delegatingMethod, previousBody, holder);
    }
    delegatingMethod.body().add(// 
    getJClass(UiThreadExecutor.class).staticInvoke(METHOD_RUN_TASK).arg(// 
    annotation.id()).arg(// 
    _new(anonymousRunnableClass)).arg(lit(delay)));
}
Also used : UiThreadExecutor(org.androidannotations.api.UiThreadExecutor) JDefinedClass(com.helger.jcodemodel.JDefinedClass) UiThread(org.androidannotations.annotations.UiThread) ExecutableElement(javax.lang.model.element.ExecutableElement) JBlock(com.helger.jcodemodel.JBlock) JMethod(com.helger.jcodemodel.JMethod)

Aggregations

JDefinedClass (com.helger.jcodemodel.JDefinedClass)31 JMethod (com.helger.jcodemodel.JMethod)20 AbstractJClass (com.helger.jcodemodel.AbstractJClass)19 JBlock (com.helger.jcodemodel.JBlock)15 JVar (com.helger.jcodemodel.JVar)15 JInvocation (com.helger.jcodemodel.JInvocation)9 IJExpression (com.helger.jcodemodel.IJExpression)5 JClassAlreadyExistsException (com.helger.jcodemodel.JClassAlreadyExistsException)5 JFieldVar (com.helger.jcodemodel.JFieldVar)5 JTypeVar (com.helger.jcodemodel.JTypeVar)5 VisitorDefinition (com.github.sviperll.adt4j.model.config.VisitorDefinition)4 GenerationProcess (com.github.sviperll.adt4j.model.util.GenerationProcess)3 JAnnotationUse (com.helger.jcodemodel.JAnnotationUse)3 JConditional (com.helger.jcodemodel.JConditional)3 JFieldRef (com.helger.jcodemodel.JFieldRef)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 TypeMirror (javax.lang.model.type.TypeMirror)3 VariableDeclaration (com.github.sviperll.adt4j.model.config.VariableDeclaration)2 MethodUsage (com.github.sviperll.adt4j.model.config.VisitorDefinition.MethodUsage)2 JCatchBlock (com.helger.jcodemodel.JCatchBlock)2