Search in sources :

Example 71 with JVar

use of com.helger.jcodemodel.JVar in project RoboBinding by RoboBinding.

the class PresentationModelObjectClassGen method defineConstructor.

/**
 * 	public PresentationModelType_PM(PresentationModelType presentationModel) {
 *		super(presentationModel);
 *		this.presentationModel = presentationModel;
 *	}
 */
@Override
public void defineConstructor() {
    /*
		 public class PresentationModelType_PM extends AbstractPresentationModelObject {
		 */
    definedClass._extends(AbstractPresentationModelObject.class);
    JMethod constructor = definedClass.constructor(JMod.PUBLIC);
    JVar presentationModelParam = constructor.param(presentationModelClass, "presentationModel");
    JBlock block = constructor.body();
    JInvocation superInvocation = JExpr.invoke("super").arg(presentationModelParam);
    block.add(superInvocation);
    block.assign(presentationModelField, presentationModelParam);
}
Also used : JBlock(com.helger.jcodemodel.JBlock) JInvocation(com.helger.jcodemodel.JInvocation) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Example 72 with JVar

use of com.helger.jcodemodel.JVar in project RoboBinding by RoboBinding.

the class ItemPresentationModelObjectClassGen method defineConstructor.

/**
 *	public StringItemPresentationModel_IPM(StringItemPresentationModel itemPresentationModel) {
 *		super(itemPresentationModel);
 *		this.itemPresentationModel = itemPresentationModel;
 *	}
 */
@Override
public void defineConstructor() {
    /*
		public class StringItemPresentationModel_IPM extends AbstractItemPresentationModelObject {
		 */
    definedClass._extends(AbstractItemPresentationModelObject.class);
    JMethod constructor = definedClass.constructor(JMod.PUBLIC);
    AbstractJType itemPresentationModelClass = codeModel.ref(presentationModelInfo.getPresentationModelTypeName());
    JVar itemPresentationModelParam = constructor.param(itemPresentationModelClass, "itemPresentationModel");
    JBlock block = constructor.body();
    JInvocation superInvocation = JExpr.invoke("super").arg(itemPresentationModelParam);
    block.add(superInvocation);
    block.assign(presentationModelField, itemPresentationModelParam);
}
Also used : AbstractJType(com.helger.jcodemodel.AbstractJType) JBlock(com.helger.jcodemodel.JBlock) JInvocation(com.helger.jcodemodel.JInvocation) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Example 73 with JVar

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

the class FinalValueClassModel method buildCaseClass.

private JDefinedClass buildCaseClass(String interfaceMethodName, Serialization serialization) throws JClassAlreadyExistsException {
    JDefinedClass caseClass = environment.buildValueClassInnerClass(JMod.PRIVATE | JMod.STATIC, Source.capitalize(interfaceMethodName) + "Case" + environment.acceptingInterfaceName(), EClassType.CLASS);
    for (JTypeVar visitorTypeParameter : environment.getValueTypeParameters()) {
        JTypeVar typeParameter = caseClass.generify(visitorTypeParameter.name());
        typeParameter.boundLike(visitorTypeParameter);
    }
    AbstractJClass usedAcceptingInterfaceType = environment.acceptingInterfaceType(caseClass.typeParams());
    AbstractJClass usedValueClassType = environment.wrappedValueClassType(caseClass.typeParams());
    VisitorDefinition.VisitorUsage usedVisitor = environment.visitor(usedValueClassType, usedValueClassType, types._RuntimeException);
    MethodUsage interfaceMethod = usedVisitor.findMethod(interfaceMethodName);
    if (interfaceMethod == null)
        throw new IllegalStateException("Method with given name not found: " + interfaceMethodName);
    JTypeVar[] methodArguments = new JTypeVar[interfaceMethod.typeParams().length];
    for (int i = 0; i < methodArguments.length; i++) {
        JTypeVar visitorMethodTypeParameter = interfaceMethod.typeParams()[i];
        JTypeVar typeParameter = caseClass.generify(visitorMethodTypeParameter.name());
        typeParameter.boundLike(visitorMethodTypeParameter);
        methodArguments[i] = typeParameter;
    }
    MethodUsage usedInterfaceMethod = interfaceMethod.narrow(methodArguments);
    caseClass._implements(usedAcceptingInterfaceType);
    if (serialization.isSerializable()) {
        caseClass._implements(types._Serializable);
        caseClass.field(JMod.PRIVATE | JMod.FINAL | JMod.STATIC, types._long, "serialVersionUID", JExpr.lit(serialization.serialVersionUIDForGeneratedCode()));
    }
    JMethod constructor = caseClass.constructor(JMod.NONE);
    for (VariableDeclaration param : usedInterfaceMethod.params()) {
        AbstractJType paramType = param.type().declarable();
        JFieldVar field = caseClass.field(JMod.PRIVATE | JMod.FINAL, paramType, param.name());
        JVar argument = constructor.param(paramType, param.name());
        constructor.body().assign(JExpr._this().ref(field), argument);
    }
    VariableDeclaration param = usedInterfaceMethod.varParam();
    if (param != null) {
        AbstractJType paramType = param.type().elementType().declarable();
        JFieldVar field = caseClass.field(JMod.PRIVATE | JMod.FINAL, paramType.array(), param.name());
        JVar argument = constructor.varParam(paramType, param.name());
        constructor.body().assign(JExpr._this().ref(field), argument);
    }
    JMethod acceptMethod = declareAcceptMethod(caseClass, usedValueClassType);
    JInvocation invocation = JExpr.invoke(acceptMethod.params().get(0), usedInterfaceMethod.name());
    for (AbstractJClass argument : methodArguments) {
        invocation.narrow(argument);
    }
    for (VariableDeclaration param1 : usedInterfaceMethod.params()) {
        invocation.arg(JExpr._this().ref(param1.name()));
    }
    VariableDeclaration param1 = usedInterfaceMethod.varParam();
    if (param1 != null) {
        invocation.arg(JExpr._this().ref(param1.name()));
    }
    acceptMethod.body()._return(invocation);
    return caseClass;
}
Also used : VisitorDefinition(com.github.sviperll.adt4j.model.config.VisitorDefinition) JDefinedClass(com.helger.jcodemodel.JDefinedClass) AbstractJType(com.helger.jcodemodel.AbstractJType) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) JTypeVar(com.helger.jcodemodel.JTypeVar) JFieldVar(com.helger.jcodemodel.JFieldVar) 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 74 with JVar

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

the class InjectHelper method processParam.

private void processParam(Element element, T holder) {
    ExecutableElement method = (ExecutableElement) element.getEnclosingElement();
    List<? extends VariableElement> parameters = method.getParameters();
    List<ParamHelper> parameterList = methodParameterMap.get(method);
    JBlock targetBlock = methodBlockMap.get(method);
    int paramCount = parameters.size();
    if (parameterList == null) {
        parameterList = new ArrayList<>();
        methodParameterMap.put(method, parameterList);
    }
    if (targetBlock == null) {
        targetBlock = createBlock(holder, true);
        methodBlockMap.put(method, targetBlock);
    }
    for (int paramIndex = 0; paramIndex < paramCount; paramIndex++) {
        VariableElement param = parameters.get(paramIndex);
        if (param.equals(element)) {
            AbstractJClass type = codeModelHelper.typeMirrorToJClass(param.asType());
            JVar fieldRef = targetBlock.decl(type, param.getSimpleName().toString(), getDefault(param.asType()));
            handler.assignValue(targetBlock, fieldRef, holder, param, param);
            parameterList.add(new ParamHelper(fieldRef, paramIndex, param));
        }
    }
    if (parameterList.size() == paramCount) {
        String methodName = method.getSimpleName().toString();
        Collections.sort(parameterList);
        JInvocation invocation = JExpr.invoke(methodName);
        for (ParamHelper parameter : parameterList) {
            invocation.arg(parameter.beanInstance);
        }
        targetBlock.add(invocation);
        if (handler instanceof MethodInjectionHandler.AfterAllParametersInjectedHandler<?>) {
            ((MethodInjectionHandler.AfterAllParametersInjectedHandler<T>) handler).afterAllParametersInjected(holder, method, parameterList);
        }
        methodParameterMap.remove(method);
    }
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) VariableElement(javax.lang.model.element.VariableElement) JBlock(com.helger.jcodemodel.JBlock) JVar(com.helger.jcodemodel.JVar)

Example 75 with JVar

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

the class APTCodeModelHelper method addParamToMethod.

private void addParamToMethod(JMethod method, VariableElement parameter, int mod, Map<String, TypeMirror> actualTypes, boolean varParam) {
    String parameterName = parameter.getSimpleName().toString();
    AbstractJClass parameterClass = typeMirrorToJClass(parameter.asType(), actualTypes);
    JVar param = varParam ? method.varParam(mod, parameterClass.elementType(), parameterName) : method.param(mod, parameterClass, parameterName);
    copyNonAAAnnotations(param, parameter.getAnnotationMirrors());
}
Also used : AbstractJClass(com.helger.jcodemodel.AbstractJClass) JVar(com.helger.jcodemodel.JVar)

Aggregations

JVar (com.helger.jcodemodel.JVar)126 JBlock (com.helger.jcodemodel.JBlock)86 JMethod (com.helger.jcodemodel.JMethod)61 AbstractJClass (com.helger.jcodemodel.AbstractJClass)53 JInvocation (com.helger.jcodemodel.JInvocation)39 IJExpression (com.helger.jcodemodel.IJExpression)33 VariableElement (javax.lang.model.element.VariableElement)25 ExecutableElement (javax.lang.model.element.ExecutableElement)19 JFieldRef (com.helger.jcodemodel.JFieldRef)16 TypeMirror (javax.lang.model.type.TypeMirror)16 JDefinedClass (com.helger.jcodemodel.JDefinedClass)15 JFieldVar (com.helger.jcodemodel.JFieldVar)15 JConditional (com.helger.jcodemodel.JConditional)9 JTryBlock (com.helger.jcodemodel.JTryBlock)7 JCatchBlock (com.helger.jcodemodel.JCatchBlock)5 AbstractJType (com.helger.jcodemodel.AbstractJType)4 ArrayList (java.util.ArrayList)4 BundleHelper (org.androidannotations.helper.BundleHelper)4 JTypeVar (com.helger.jcodemodel.JTypeVar)3 VariableDeclaration (com.github.sviperll.adt4j.model.config.VariableDeclaration)2