Search in sources :

Example 1 with JDefinedClass

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

the class AbstractPresentationModelObjectClassGen method defineTryToCreateFunction.

/*
	@Override
	public Function tryToCreateFunction(MethodDescriptor methodDescriptor) {
		if(methodDescriptor.equals(createMethodDescriptor(ON_CLICK))) {
			return new Function() {
				
				@Override
				public Object call(Object... args) {
					presentationModel.onClick();
					return null;
				}
			};
		}
		
		if(methodDescriptor.equals(createMethodDescriptor(ON_CLICK_WITH_EVENT, AbstractViewEvent.class))){
			return new Function() {
				
				@Override
				public Object call(Object... args) {
					boolean result = presentationModel.onLongClickWithEvent((AbstractViewEvent)args[0]);
					return (Boolean)result;
				}
			};
		}
		
		return null;
	}
	 */
public void defineTryToCreateFunction() {
    JMethod method = declarePublicMethodOverride("tryToCreateFunction", Function.class);
    JVar methodDescriptorParam = method.param(MethodDescriptor.class, "methodDescriptor");
    JBlock body = method.body();
    for (EventMethodInfo eventMethodInfo : presentationModelInfo.eventMethods()) {
        JInvocation getMethod = JExpr.invoke("createMethodDescriptor").arg(eventMethodInfo.name());
        if (eventMethodInfo.hasEventArg()) {
            getMethod.arg(codeModel.ref(eventMethodInfo.eventArgType()).dotclass());
        }
        JConditional conditional = body._if(methodDescriptorParam.invoke("equals").arg(getMethod));
        JBlock conditionalBody = conditional._then();
        // create Function.
        JDefinedClass anonymousFunction = codeModel.anonymousClass(Function.class);
        JMethod call = declarePublicMethodOverride(anonymousFunction, "call", Object.class);
        JVar argsVar = call.varParam(Object.class, "args");
        JBlock callBody = call.body();
        // call event method.
        JInvocation onEvent = presentationModelFieldWithoutThis.invoke(eventMethodInfo.name());
        if (eventMethodInfo.hasEventArg()) {
            AbstractJClass eventArgClass = codeModel.ref(eventMethodInfo.eventArgType());
            onEvent.arg(JExpr.cast(eventArgClass, argsVar.component(JExpr.lit(0))));
        }
        // call return.
        if (eventMethodInfo.hasReturn()) {
            JVar returnVar = callBody.decl(codeModel.ref(eventMethodInfo.nonPrimitiveReturnType()), "result", onEvent);
            callBody._return(returnVar);
        } else {
            callBody.add(onEvent);
            callBody._return(JExpr._null());
        }
        // return Function.
        conditionalBody._return(JExpr._new(anonymousFunction));
    }
    body._return(JExpr._null());
}
Also used : JDefinedClass(com.helger.jcodemodel.JDefinedClass) JBlock(com.helger.jcodemodel.JBlock) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) JConditional(com.helger.jcodemodel.JConditional) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Example 2 with JDefinedClass

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

the class ViewBindingObjectClassGen method defineSimpleOneWayPropertyClasses.

/**
 *    	private static class ImageAlphaAttribute implements OneWayPropertyViewAttribute<ImageView, Integer> {
 *    		@Override
 *    		public void updateView(ImageView view, Integer newValue) {
 *    			view.setImageAlpha(newValue);
 *    		}
 *    	}
 */
public void defineSimpleOneWayPropertyClasses() {
    for (SimpleOneWayPropertyInfo propInfo : simpleOneWayPropertyInfoList) {
        JDefinedClass definedBindingAttributeClass = propInfo.defineBindingClass(new ClassDefinitionCallback() {

            @Override
            public JDefinedClass define(String typeName) {
                try {
                    return definedClass._class(JMod.PUBLIC | JMod.STATIC, typeName);
                } catch (JClassAlreadyExistsException e) {
                    throw new RuntimeException("Class '" + typeName + "' already exists", e);
                }
            }
        });
        AbstractJClass propertyClass = codeModel.ref(propInfo.propertyType());
        AbstractJClass oneWayPropertyInterface = codeModel.ref(OneWayPropertyViewAttribute.class).narrow(viewClass, propertyClass);
        definedBindingAttributeClass._implements(oneWayPropertyInterface);
        JMethod method = definedBindingAttributeClass.method(JMod.PUBLIC, codeModel.VOID, "updateView");
        method.annotate(Override.class);
        JVar viewParam = method.param(viewClass, "view");
        JVar newValueParam = method.param(propertyClass, "newValue");
        JBlock body = method.body();
        body.invoke(viewParam, propInfo.propertySetter()).arg(newValueParam);
    }
}
Also used : JClassAlreadyExistsException(com.helger.jcodemodel.JClassAlreadyExistsException) JDefinedClass(com.helger.jcodemodel.JDefinedClass) JBlock(com.helger.jcodemodel.JBlock) AbstractJClass(com.helger.jcodemodel.AbstractJClass) OneWayPropertyViewAttribute(org.robobinding.viewattribute.property.OneWayPropertyViewAttribute) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Example 3 with JDefinedClass

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

the class ValueClassConfiguration method classCustomization.

private static GenerationResult<ClassCustomization> classCustomization(JAnnotationUse annotation, VisitorDefinition visitorDefinition, JDefinedClass valueClass) throws ClassCastException, NullPointerException {
    GenerationProcess generation = new GenerationProcess();
    AbstractJClass extendsClass = annotation.getParam("extendsClass", AbstractJClass.class);
    AbstractJClass wrapperClass = annotation.getParam("wrapperClass", AbstractJClass.class);
    if (wrapperClass == null)
        throw new NullPointerException("wrapperClass annotation argument should never be null");
    String wrapperClassFullName = wrapperClass.fullName();
    if (wrapperClassFullName == null)
        throw new NullPointerException("wrapperClass.fullName() is null");
    if (wrapperClassFullName.equals("java.lang.Object"))
        wrapperClass = null;
    String className = annotation.getParam("className", String.class);
    if (className == null)
        throw new NullPointerException("className annotation argument should never be null");
    if (wrapperClass == null) {
        if (className.equals(":auto")) {
            className = autoClassName(visitorDefinition.visitorName());
        }
    } else {
        AbstractJClass wrapperClassErasure = wrapperClass.erasure();
        if (wrapperClassErasure instanceof JDefinedClass) {
            JDefinedClass definition = (JDefinedClass) wrapperClassErasure;
            JAnnotationUse wrapsGeneratedAnnotation = null;
            for (JAnnotationUse wrapperAnnotaion : definition.annotations()) {
                String annotationClassFullName = wrapperAnnotaion.getAnnotationClass().erasure().fullName();
                if (annotationClassFullName != null && annotationClassFullName.equals(WrapsGeneratedValueClass.class.getName())) {
                    wrapsGeneratedAnnotation = wrapperAnnotaion;
                }
            }
            if (wrapsGeneratedAnnotation == null)
                generation.reportError(MessageFormat.format("Wrapper class should be annotated with @{0} annotation.", com.github.sviperll.adt4j.WrapsGeneratedValueClass.class.getName()));
            else {
                AbstractJClass visitor = wrapsGeneratedAnnotation.getParam("visitor", AbstractJClass.class);
                if (visitor == null || visitor.fullName() == null || !visitor.fullName().equals(visitorDefinition.qualifiedName()))
                    generation.reportError("@" + WrapsGeneratedValueClass.class.getName() + " annotation should have " + visitorDefinition.qualifiedName() + " as visitor argument");
            }
        }
        if (!className.equals(":auto")) {
            generation.reportError("You shouldn't define className when wrapperClass is used. Generated class name is derived from wrapper class' extends clause.");
        } else {
            AbstractJClass extendedClass = wrapperClass._extends();
            boolean extendedClassError = false;
            if (extendedClass != null) {
                if (extendedClass.isError()) {
                    className = extendedClass.name();
                } else {
                    if (valueClass == null) {
                        extendedClassError = true;
                    } else {
                        String valueClassFullName = valueClass.fullName();
                        if (valueClassFullName == null || !valueClassFullName.equals(extendedClass.erasure().fullName()))
                            extendedClassError = true;
                        else
                            className = valueClass.name();
                    }
                }
            }
            if (extendedClass == null || extendedClassError) {
                generation.reportError("Wrapper class should explicitly extend non-existing class, that class is to be generated");
                className = autoClassName(visitorDefinition.visitorName());
            } else {
                boolean typeParamsError = false;
                List<? extends AbstractJClass> typeArguments = extendedClass.getTypeParameters();
                List<JTypeVar> generatedTypeParameters = visitorDefinition.nonspecialTypeParameters();
                JTypeVar[] wrapperTypeParameters = wrapperClass.typeParams();
                if (wrapperTypeParameters.length != typeArguments.size() || wrapperTypeParameters.length != generatedTypeParameters.size())
                    typeParamsError = true;
                else {
                    for (int i = 0; i < wrapperTypeParameters.length; i++) {
                        JTypeVar wrapperTypeParameter = wrapperTypeParameters[i];
                        if (typeArguments.get(i) != wrapperTypeParameter) {
                            typeParamsError = true;
                            break;
                        }
                    }
                }
                if (typeParamsError) {
                    generation.reportError("Wrapper class should declare same type-parameters as generated class and should extend generated class with all type-arguments applied");
                }
            }
        }
    }
    ClassCustomization classCustomization = new ClassCustomization(className, wrapperClass, extendsClass);
    return generation.createGenerationResult(classCustomization);
}
Also used : JDefinedClass(com.helger.jcodemodel.JDefinedClass) AbstractJClass(com.helger.jcodemodel.AbstractJClass) WrapsGeneratedValueClass(com.github.sviperll.adt4j.WrapsGeneratedValueClass) JTypeVar(com.helger.jcodemodel.JTypeVar) JAnnotationUse(com.helger.jcodemodel.JAnnotationUse) GenerationProcess(com.github.sviperll.adt4j.model.util.GenerationProcess)

Example 4 with JDefinedClass

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

the class FinalValueClassModel method buildCaseClasses.

private Map<String, JDefinedClass> buildCaseClasses(Serialization serialization) throws JClassAlreadyExistsException {
    Map<String, JDefinedClass> caseClasses = new TreeMap<>();
    for (JMethod interfaceMethod : environment.visitorDefinition().methodDefinitions()) {
        JDefinedClass caseClass = buildCaseClass(interfaceMethod.name(), serialization);
        caseClasses.put(interfaceMethod.name(), caseClass);
    }
    return caseClasses;
}
Also used : JDefinedClass(com.helger.jcodemodel.JDefinedClass) TreeMap(java.util.TreeMap) JMethod(com.helger.jcodemodel.JMethod)

Example 5 with JDefinedClass

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

the class FinalValueClassModel method buildFactory.

JMethod buildFactory(Map<String, JMethod> constructorMethods) throws JClassAlreadyExistsException {
    JDefinedClass factory = buildFactoryClass(constructorMethods);
    JFieldVar factoryField = environment.buildValueClassField(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, factory, "FACTORY");
    JAnnotationUse fieldAnnotationUse = factoryField.annotate(SuppressWarnings.class);
    JAnnotationArrayMember paramArray = fieldAnnotationUse.paramArray("value");
    paramArray.param("unchecked");
    paramArray.param("rawtypes");
    factoryField.init(JExpr._new(factory));
    JMethod factoryMethod = environment.buildValueClassMethod(Source.toJMod(environment.factoryMethodAccessLevel()) | JMod.STATIC, "factory");
    Source.annotateNonnull(factoryMethod);
    JAnnotationUse methodAnnotationUse = factoryMethod.annotate(SuppressWarnings.class);
    methodAnnotationUse.param("value", "unchecked");
    for (JTypeVar visitorTypeParameter : environment.getValueTypeParameters()) {
        JTypeVar typeParameter = factoryMethod.generify(visitorTypeParameter.name());
        typeParameter.boundLike(visitorTypeParameter);
    }
    AbstractJClass usedValueClassType = environment.wrappedValueClassType(factoryMethod.typeParams());
    factoryMethod.type(environment.visitor(usedValueClassType, usedValueClassType, types._RuntimeException).getVisitorType());
    factoryMethod.body()._return(factoryField);
    return factoryMethod;
}
Also used : JDefinedClass(com.helger.jcodemodel.JDefinedClass) JFieldVar(com.helger.jcodemodel.JFieldVar) JTypeVar(com.helger.jcodemodel.JTypeVar) JAnnotationUse(com.helger.jcodemodel.JAnnotationUse) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JAnnotationArrayMember(com.helger.jcodemodel.JAnnotationArrayMember) 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