Search in sources :

Example 86 with JMethod

use of com.helger.jcodemodel.JMethod 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 87 with JMethod

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

the class AbstractPresentationModelObjectClassGen method defineEventMethods.

/*
	  	@Override
	 	public Set<MethodDescriptor> eventMethods() {
	 		return Sets.newHashSet(
	 			createMethodDescriptor(ON_CLICK),
	 			createMethodDescriptor(ON_CLICK_WITH_EVENT, AbstractViewEvent.class));
	 	}
	 
	 */
public void defineEventMethods() {
    JMethod method = declarePublicMethodOverride("eventMethods", setClassWithMethodDescriptor);
    JInvocation invocation = codeModel.ref(Sets.class).staticInvoke("newHashSet");
    for (EventMethodInfo eventMethod : presentationModelInfo.eventMethods()) {
        JInvocation getMethodInvocation = JExpr.invoke("createMethodDescriptor").arg(eventMethod.name());
        if (eventMethod.hasEventArg()) {
            getMethodInvocation.arg(codeModel.ref(eventMethod.eventArgType()).dotclass());
        }
        invocation.arg(getMethodInvocation);
    }
    method.body()._return(invocation);
}
Also used : Sets(org.robobinding.util.Sets) JInvocation(com.helger.jcodemodel.JInvocation) JMethod(com.helger.jcodemodel.JMethod)

Example 88 with JMethod

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

the class AbstractPresentationModelObjectClassGen method declarePublicMethodOverride.

private static JMethod declarePublicMethodOverride(JDefinedClass definedClass, String methodName, Class<?> returnType) {
    JMethod method = definedClass.method(JMod.PUBLIC, returnType, methodName);
    method.annotate(Override.class);
    return method;
}
Also used : JMethod(com.helger.jcodemodel.JMethod)

Example 89 with JMethod

use of com.helger.jcodemodel.JMethod 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 90 with JMethod

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

the class AbstractPresentationModelObjectClassGen method declarePublicMethodOverride.

private static JMethod declarePublicMethodOverride(JDefinedClass definedClass, String methodName, AbstractJClass returnType) {
    JMethod method = definedClass.method(JMod.PUBLIC, returnType, methodName);
    method.annotate(Override.class);
    return method;
}
Also used : JMethod(com.helger.jcodemodel.JMethod)

Aggregations

JMethod (com.helger.jcodemodel.JMethod)122 JBlock (com.helger.jcodemodel.JBlock)73 JVar (com.helger.jcodemodel.JVar)56 AbstractJClass (com.helger.jcodemodel.AbstractJClass)36 JInvocation (com.helger.jcodemodel.JInvocation)26 JDefinedClass (com.helger.jcodemodel.JDefinedClass)20 IJExpression (com.helger.jcodemodel.IJExpression)15 ExecutableElement (javax.lang.model.element.ExecutableElement)13 JFieldVar (com.helger.jcodemodel.JFieldVar)10 JConditional (com.helger.jcodemodel.JConditional)8 JTryBlock (com.helger.jcodemodel.JTryBlock)7 JTypeVar (com.helger.jcodemodel.JTypeVar)7 TypeMirror (javax.lang.model.type.TypeMirror)7 VariableElement (javax.lang.model.element.VariableElement)6 JFieldRef (com.helger.jcodemodel.JFieldRef)5 JPrimitiveType (com.helger.jcodemodel.JPrimitiveType)5 ArrayList (java.util.ArrayList)5 VisitorDefinition (com.github.sviperll.adt4j.model.config.VisitorDefinition)4 GenerationProcess (com.github.sviperll.adt4j.model.util.GenerationProcess)4 AbstractJType (com.helger.jcodemodel.AbstractJType)4