Search in sources :

Example 66 with JVar

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

the class AbstractPresentationModelObjectClassGen method defineTryToCreateProperty.

/*
	@Override
	public SimpleProperty tryToCreateProperty(String name) {
		if(name.equals(PROP1)) {
			PropertyDescriptor descriptor = createPropertyDescriptor(String.class, name, true, true);
			
			AbstractGetSet<?> getSet = new AbstractGetSet<String>(descriptor) {
				@Override
				public String getValue() {
					return presentationModel.getProp1();
				}
				
				@Override
				public void setValue(String newValue) {
					presentationModel.setProp1(newValue);
				}
			};
			
			return new SimpleProperty(this, descriptor, getSet);
		}
		
		if(name.equals(PROP2)) {
			PropertyDescriptor descriptor = createPropertyDescriptor(Integer.class, name, true, true);
			
			AbstractGetSet<?> getSet = new AbstractGetSet<Integer>(descriptor) {
				@Override
				public Integer getValue() {
					return presentationModel.getProp2();
				}
				
				@Override
				public void setValue(Integer newValue) {
					presentationModel.setProp2(newValue);
				}
			};
			
			return new SimpleProperty(this, descriptor, getSet);
		} 
		
		return null;
	}
	 */
public void defineTryToCreateProperty() {
    try {
        JMethod method = declarePublicMethodOverride("tryToCreateProperty", SimpleProperty.class);
        JVar nameParam = method.param(String.class, "name");
        JBlock body = method.body();
        for (PropertyInfo propertyInfo : presentationModelInfo.properties()) {
            JConditional conditional = body._if(nameParam.invoke("equals").arg(propertyInfo.name()));
            JBlock conditionalBody = conditional._then();
            // create PropertyDescriptor.
            AbstractJClass propertyClass = codeModel.ref(propertyInfo.typeName());
            JInvocation createPropertyDescriptor = JExpr.invoke("createPropertyDescriptor").arg(propertyClass.dotclass()).arg(nameParam).arg(JExpr.lit(propertyInfo.isReadable())).arg(JExpr.lit(propertyInfo.isWritable()));
            JVar descriptorVar = conditionalBody.decl(propertyDescriptorClass, "descriptor", createPropertyDescriptor);
            // create AbstractGetSet.
            // JClass narrowedGetSet = getSetClass.narrow(codeModel.ref(propertyInfo.typeName()));
            AbstractJClass narrowedGetSet = getSetClass.narrow(propertyClass);
            JDefinedClass anonymousGetSet = codeModel.anonymousClass(narrowedGetSet);
            if (propertyInfo.isReadable()) {
                JMethod getter = declarePublicMethodOverride(anonymousGetSet, "getValue", propertyClass);
                getter.body()._return(presentationModelFieldWithoutThis.invoke(propertyInfo.getter()));
            }
            if (propertyInfo.isWritable()) {
                JMethod setter = declarePublicMethodOverride(anonymousGetSet, "setValue", Void.TYPE);
                JVar newValueParam = setter.param(propertyClass, "newValue");
                setter.body().add(presentationModelFieldWithoutThis.invoke(propertyInfo.setter()).arg(newValueParam));
            }
            JVar getSetVar = conditionalBody.decl(wildcardGetSetClass, "getSet", JExpr._new(anonymousGetSet).arg(descriptorVar));
            // return SimpleProperty.
            conditionalBody._return(JExpr._new(simplePropertyClass).arg(JExpr._this()).arg(descriptorVar).arg(getSetVar));
        }
        body._return(JExpr._null());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : JDefinedClass(com.helger.jcodemodel.JDefinedClass) JBlock(com.helger.jcodemodel.JBlock) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JConditional(com.helger.jcodemodel.JConditional) JInvocation(com.helger.jcodemodel.JInvocation) JMethod(com.helger.jcodemodel.JMethod) IOException(java.io.IOException) JClassAlreadyExistsException(com.helger.jcodemodel.JClassAlreadyExistsException) JVar(com.helger.jcodemodel.JVar)

Example 67 with JVar

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

the class AbstractPresentationModelObjectClassGen method defineTryToCreateDataSetProperty.

/*
	@Override
	public DataSetProperty tryToCreateDataSetProperty(String name) {
		if(name.equals(DATA_SET_PROP)) {
			PropertyDescriptor descriptor = createDataSetPropertyDescriptor(List.class, name);
			
			AbstractGetSet<?> getSet = new AbstractGetSet<List<String>>(descriptor) {
				@Override
				public List<String> getValue() {
					return presentationModel.getDataSetProp();
				}
			};
			
			RefreshableItemPresentationModelFactory factory = new RefreshableItemPresentationModelFactory() {
				
				@Override
				public RefreshableItemPresentationModel create(int itemViewType) {
					return new StringItemPresentationModel_IPM(new StringItemPresentationModel());
				}
			};	
			
			return new DataSetProperty(this, descriptor, new ListDataSet(factory, getSet));
		}
		
		if(name.equals(DATA_SET_PROP_WITH_FACTORY_METHOD)) {
			PropertyDescriptor descriptor = createDataSetPropertyDescriptor(List.class, name);
			
			AbstractGetSet<?> getSet = new AbstractGetSet<List<String>>(descriptor) {
				@Override
				public List<String> getValue() {
					return presentationModel.getDataSetPropWithFactoryMethod();
				}
			};
			
			RefreshableItemPresentationModelFactory factory = new RefreshableItemPresentationModelFactory() {
				@Override
				public RefreshableItemPresentationModel create(int itemViewType) {
					return new StringItemPresentationModel_IPM(presentationModel.newStringItemPresentationModel());
				}
			};
			
			return new DataSetProperty(this, descriptor, new ListDataSet(factory, getSet));
		} 
		
		
		if(name.equals(DATA_SET_PROP_WITH_VIEW_TYPE_SELECTOR)) {
			PropertyDescriptor descriptor = createDataSetPropertyDescriptor(List.class, name);
			
			AbstractGetSet<?> getSet = new AbstractGetSet<List<String>>(descriptor) {
				@Override
				public List<String> getValue() {
					return presentationModel.getDataSetPropWithViewTypeSelector();
				}
			};
			
			RefreshableItemPresentationModelFactory factory = new RefreshableItemPresentationModelFactory() {
				@Override
				public RefreshableItemPresentationModel create(int itemViewType) {
					return new StringItemPresentationModelPOC_IPM(new StringItemPresentationModelPOC());
				}
			};
			
			ViewTypeSelectable viewTypeSelector = new ViewTypeSelectable() {
				@Override
				@SuppressWarnings({ "rawtypes", "unchecked" }) 
				public int selectViewType(ViewTypeSelectionContext context) {
					return presentationModel.selectViewTypeWithParameter(context);
				}
			};
			
			return new DataSetProperty(this, descriptor, new ListDataSet(factory, getSet), viewTypeSelector);
		} 
		
		if(name.equals(DATA_SET_PROP_WITH_FACTORY_METHOD_AND_VIEW_TYPE_SELECTOR)) {
			PropertyDescriptor descriptor = createDataSetPropertyDescriptor(List.class, name);
			
			AbstractGetSet<?> getSet = new AbstractGetSet<List<String>>(descriptor) {
				@Override
				public List<String> getValue() {
					return presentationModel.getDataSetPropWithViewTypeSelector();
				}
			};
			
			RefreshableItemPresentationModelFactory factory = new RefreshableItemPresentationModelFactory() {
				@Override
				public RefreshableItemPresentationModel create(int itemViewType) {
					return new StringItemPresentationModelPOC_IPM(
							presentationModel.createStringItemPresentationModelWithParameter(itemViewType));
				}
			};
			
			ViewTypeSelectable viewTypeSelector = new ViewTypeSelectable() {
				@Override
				@SuppressWarnings({ "rawtypes", "unchecked" }) 
				public int selectViewType(ViewTypeSelectionContext context) {
					return presentationModel.selectViewTypeWithParameter(context);
				}
			};
			
			return new DataSetProperty(this, descriptor, new ListDataSet(factory, getSet), viewTypeSelector, PreInitializingViews.DEFAULT);
		}
		
		return null;
	}
	 */
public void defineTryToCreateDataSetProperty() {
    JMethod method = declarePublicMethodOverride("tryToCreateDataSetProperty", DataSetProperty.class);
    JVar nameParam = method.param(String.class, "name");
    JBlock body = method.body();
    for (DataSetPropertyInfo propertyInfo : presentationModelInfo.dataSetProperties()) {
        JConditional conditional = body._if(nameParam.invoke("equals").arg(propertyInfo.name()));
        JBlock conditionalBody = conditional._then();
        // create createDataSetPropertyDescriptor.
        AbstractJClass propertyClass = codeModel.ref(propertyInfo.type());
        JInvocation createDataSetPropertyDescriptor = JExpr.invoke("createDataSetPropertyDescriptor").arg(propertyClass.dotclass()).arg(nameParam);
        JVar descriptorVar = conditionalBody.decl(propertyDescriptorClass, "descriptor", createDataSetPropertyDescriptor);
        // create AbstractGetSet.
        AbstractJClass narrowedGetSet = getSetClass.narrow(codeModel.ref(propertyInfo.type()));
        JDefinedClass anonymousGetSet = codeModel.anonymousClass(narrowedGetSet);
        JMethod getter = declarePublicMethodOverride(anonymousGetSet, "getValue", propertyClass);
        getter.body()._return(presentationModelFieldWithoutThis.invoke(propertyInfo.getter()));
        JVar getSetVar = conditionalBody.decl(wildcardGetSetClass, "getSet", JExpr._new(anonymousGetSet).arg(descriptorVar));
        // JVar getSetVar = conditionalBody.decl(getSetClass, "getSet",
        // JExpr._new(anonymousGetSet.narrow(propertyClass)).arg(descriptorVar));
        // create RefreshableItemPresentationModelFactory.
        JDefinedClass anonymousFactory = codeModel.anonymousClass(RefreshableItemPresentationModelFactory.class);
        JMethod create = declarePublicMethodOverride(anonymousFactory, "create", RefreshableItemPresentationModel.class);
        JVar itemViewTypeParam = create.param(int.class, "itemViewType");
        AbstractJClass itemPresentationModelObjectClass = codeModel.ref(propertyInfo.itemPresentationModelObjectTypeName());
        JInvocation newItemPresentationModelObject = JExpr._new(itemPresentationModelObjectClass);
        if (propertyInfo.isCreatedByFactoryMethodWithArg()) {
            newItemPresentationModelObject.arg(presentationModelFieldWithoutThis.invoke(propertyInfo.factoryMethod()).arg(itemViewTypeParam));
        } else if (propertyInfo.isCreatedByFactoryMethodWithoutArg()) {
            newItemPresentationModelObject.arg(presentationModelFieldWithoutThis.invoke(propertyInfo.factoryMethod()));
        } else {
            newItemPresentationModelObject.arg(JExpr._new(codeModel.ref(propertyInfo.itemPresentationModelTypeName())));
        }
        create.body()._return(newItemPresentationModelObject);
        JVar factoryVar = conditionalBody.decl(refreshableItemPresentationModelFactoryClass, "factory", JExpr._new(anonymousFactory));
        // new DataSetProperty(this, descriptor, new ListDataSet(factory, getSet));
        JInvocation newDataSetProperty = JExpr._new(dataSetPropertyClass).arg(JExpr._this()).arg(descriptorVar).arg(JExpr._new(codeModel.ref(propertyInfo.dataSetImplementationType())).arg(factoryVar).arg(getSetVar));
        // create viewTypeSelector.
        if (propertyInfo.hasViewTypeSelector()) {
            JDefinedClass anonymousViewTypeSelector = codeModel.anonymousClass(ViewTypeSelectable.class);
            JMethod selectViewType = declarePublicMethodOverride(anonymousViewTypeSelector, "selectViewType", int.class);
            selectViewType.annotate(codeModel.ref(SuppressWarnings.class)).paramArray("value", "rawtypes", "unchecked");
            JVar contextParam = selectViewType.param(ViewTypeSelectionContext.class, "context");
            JInvocation userSelectViewTypeCall;
            if (propertyInfo.viewTypeSelectorAcceptsArg()) {
                userSelectViewTypeCall = presentationModelFieldWithoutThis.invoke(propertyInfo.viewTypeSelector()).arg(contextParam);
            } else {
                userSelectViewTypeCall = presentationModelFieldWithoutThis.invoke(propertyInfo.viewTypeSelector());
            }
            selectViewType.body()._return(userSelectViewTypeCall);
            JVar viewTypeSelectorVar = conditionalBody.decl(viewTypeSelectableClass, "viewTypeSelector", JExpr._new(anonymousViewTypeSelector));
            newDataSetProperty.arg(viewTypeSelectorVar);
        }
        newDataSetProperty.arg(JExpr.enumConstantRef(preInitializingViewsClass, propertyInfo.preInitializingViews().name()));
        // return DataSetProperty.
        conditionalBody._return(newDataSetProperty);
    }
    body._return(JExpr._null());
}
Also used : JDefinedClass(com.helger.jcodemodel.JDefinedClass) JBlock(com.helger.jcodemodel.JBlock) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JConditional(com.helger.jcodemodel.JConditional) JInvocation(com.helger.jcodemodel.JInvocation) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Example 68 with JVar

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

the class AbstractPresentationModelObjectClassGen method definePropertyDependencies.

/*
	  	@Override
	  	public Map<String, Set<String>> propertyDependencies() {
	 		Map<String, Set<String>> dependencies = Maps.newHashMap();
	 		dependencies.put(PROP1, Sets.newHashSet(PROP2));
	 		return dependencies;
	 	}
	 
	 */
public void definePropertyDependencies() {
    JMethod method = declarePublicMethodOverride("propertyDependencies", mapClassWithStringAndStringSet);
    JBlock body = method.body();
    JVar dependenciesVar = body.decl(mapClassWithStringAndStringSet, "dependencies", codeModel.ref(Maps.class).staticInvoke("newHashMap"));
    for (PropertyDependencyInfo propertyDependencyInfo : presentationModelInfo.propertyDependencies()) {
        body.add(dependenciesVar.invoke("put").arg(propertyDependencyInfo.property()).arg(newHashSetInvocation(propertyDependencyInfo.dependentProperties())));
    }
    body._return(dependenciesVar);
}
Also used : JBlock(com.helger.jcodemodel.JBlock) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Example 69 with JVar

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

the class ViewBindingObjectClassGen method defineMapBindingAttributesMethod.

/**
 *    	@Override
 *    	public void mapBindingAttributes(BindingAttributeMappings<ImageView> mappings) {
 *    		mappings.mapOneWayProperty(ImageAlphaAttribute.class, "imageAlpha");
 *    		mappings.mapOneWayProperty(MaxWidthAttribute.class, "maxWidth");
 *    		mappings.mapOneWayProperty(MaxHeightAttribute.class, "maxHeight");
 *
 *    		customViewBinding.mapBindingAttributes(mappings);
 *    	}
 */
public void defineMapBindingAttributesMethod() {
    JMethod method = definedClass.method(JMod.PUBLIC, codeModel.VOID, "mapBindingAttributes");
    method.annotate(Override.class);
    AbstractJClass bindingAttributeMappingsType = codeModel.ref(BindingAttributeMappings.class).narrow(viewClass);
    JVar mappingsParam = method.param(bindingAttributeMappingsType, "mappings");
    JBlock body = method.body();
    for (SimpleOneWayPropertyInfo info : simpleOneWayPropertyInfoList) {
        body.invoke(mappingsParam, "mapOneWayProperty").arg(info.getBindingClass().dotclass()).arg(info.propertyName());
    }
    body.invoke(customViewBindingFieldWithoutThis, "mapBindingAttributes").arg(mappingsParam);
}
Also used : BindingAttributeMappings(org.robobinding.viewbinding.BindingAttributeMappings) JBlock(com.helger.jcodemodel.JBlock) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Example 70 with JVar

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

the class ViewBindingObjectClassGen method defineConstructor.

/**
 *    	public MyCustomViewBinding$$VB(MyCustomViewBinding customViewBinding) {
 *			this.customViewBinding = customViewBinding;
 *		}
 */
public void defineConstructor() {
    JMethod constructor = definedClass.constructor(JMod.PUBLIC);
    JVar customViewBindingParam = constructor.param(customViewBindingClass, "customViewBinding");
    JBlock block = constructor.body();
    block.assign(customViewBindingField, customViewBindingParam);
}
Also used : JBlock(com.helger.jcodemodel.JBlock) JMethod(com.helger.jcodemodel.JMethod) 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