Search in sources :

Example 11 with JDefinedClass

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

the class FragmentArgHandler method createBuilderInjectMethod.

public void createBuilderInjectMethod(EFragmentHolder holder, Element element, List<ArgHelper> argHelpers) {
    JDefinedClass builderClass = holder.getBuilderClass();
    JFieldRef builderArgsField = holder.getBuilderArgsField();
    JMethod builderMethod = builderClass.method(PUBLIC, holder.narrow(builderClass), element.getSimpleName().toString());
    String docComment = getProcessingEnvironment().getElementUtils().getDocComment(element);
    codeModelHelper.addTrimmedDocComment(builderMethod, docComment);
    for (ArgHelper argHelper : argHelpers) {
        String fieldName = argHelper.param.getSimpleName().toString();
        TypeMirror actualType = codeModelHelper.getActualTypeOfEnclosingElementOfInjectedElement(holder, argHelper.param);
        BundleHelper bundleHelper = new BundleHelper(getEnvironment(), actualType);
        JFieldVar argKeyStaticField = getOrCreateStaticArgField(holder, argHelper.argKey, fieldName);
        AbstractJClass paramClass = codeModelHelper.typeMirrorToJClass(actualType);
        JVar arg = builderMethod.param(paramClass, fieldName);
        builderMethod.body().add(bundleHelper.getExpressionToSaveFromField(builderArgsField, argKeyStaticField, arg));
        builderMethod.javadoc().addParam(fieldName).append("value for this Fragment argument");
    }
    builderMethod.javadoc().addReturn().append("the FragmentBuilder to chain calls");
    builderMethod.body()._return(_this());
}
Also used : JFieldRef(com.helger.jcodemodel.JFieldRef) JDefinedClass(com.helger.jcodemodel.JDefinedClass) TypeMirror(javax.lang.model.type.TypeMirror) JFieldVar(com.helger.jcodemodel.JFieldVar) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JMethod(com.helger.jcodemodel.JMethod) BundleHelper(org.androidannotations.helper.BundleHelper) JVar(com.helger.jcodemodel.JVar)

Example 12 with JDefinedClass

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

the class AbstractListenerHandler method process.

@Override
public void process(Element element, T holder) {
    methodName = element.getSimpleName().toString();
    ExecutableElement executableElement = (ExecutableElement) element;
    List<? extends VariableElement> parameters = executableElement.getParameters();
    TypeMirror returnType = executableElement.getReturnType();
    List<JFieldRef> idsRefs = annotationHelper.extractAnnotationFieldRefs(element, getResourceType(), true);
    JDefinedClass listenerAnonymousClass = getCodeModel().anonymousClass(getListenerClass(holder));
    JMethod listenerMethod = createListenerMethod(listenerAnonymousClass);
    listenerMethod.annotate(Override.class);
    JBlock listenerMethodBody = listenerMethod.body();
    IJExpression activityRef = holder.getGeneratedClass().staticRef("this");
    JInvocation call = invoke(activityRef, methodName);
    makeCall(listenerMethodBody, call, returnType);
    processParameters(holder, listenerMethod, call, parameters);
    assignListeners(holder, idsRefs, listenerAnonymousClass);
}
Also used : JFieldRef(com.helger.jcodemodel.JFieldRef) TypeMirror(javax.lang.model.type.TypeMirror) JDefinedClass(com.helger.jcodemodel.JDefinedClass) ExecutableElement(javax.lang.model.element.ExecutableElement) JBlock(com.helger.jcodemodel.JBlock) IJExpression(com.helger.jcodemodel.IJExpression) JInvocation(com.helger.jcodemodel.JInvocation) JMethod(com.helger.jcodemodel.JMethod)

Example 13 with JDefinedClass

use of com.helger.jcodemodel.JDefinedClass 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 14 with JDefinedClass

use of com.helger.jcodemodel.JDefinedClass 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 15 with JDefinedClass

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

the class Stage1ValueClassModel method createResult.

public GenerationResult<JDefinedClass> createResult() {
    GenerationProcess generation = new GenerationProcess();
    generation.reportAllErrors(validateInterfaces());
    Map<String, FieldConfiguration> gettersConfigutation = generation.processGenerationResult(configuration.getGettersConfigutation(valueClass, types));
    Map<String, FieldConfiguration> updatersConfiguration = generation.processGenerationResult(configuration.getUpdatersConfiguration(valueClass, types));
    Map<String, PredicateConfigutation> predicates = generation.processGenerationResult(configuration.getPredicates());
    FinalValueClassModel result;
    if (generation.hasErrors()) {
        FinalValueClassModelEnvironment environment = new FinalValueClassModelEnvironment(valueClass, null, configuration);
        result = FinalValueClassModel.createErrorModel(environment, types);
    } else {
        JDefinedClass acceptingInterface;
        try {
            acceptingInterface = createAcceptingInterface();
        } catch (JClassAlreadyExistsException ex) {
            throw new RuntimeException("Unexpected exception", ex);
        }
        if (configuration.isValueClassSerializable()) {
            acceptingInterface._extends(types._Serializable);
        }
        FinalValueClassModelEnvironment environment = new FinalValueClassModelEnvironment(valueClass, acceptingInterface, configuration);
        result = FinalValueClassModel.createModel(environment, types);
    }
    result.buildSerialVersionUID();
    FinalValueClassModel.MethodBuilder methodBuilder = result.createMethodBuilder(configuration.serialization());
    Map<String, JMethod> constructorMethods = methodBuilder.buildConstructorMethods(configuration.serialization());
    methodBuilder.buildPrivateConstructor();
    if (configuration.isValueClassSerializable())
        methodBuilder.buildReadObjectMethod();
    methodBuilder.buildProtectedConstructor(configuration.serialization());
    methodBuilder.buildAcceptMethod();
    for (FieldConfiguration getter : gettersConfigutation.values()) {
        methodBuilder.generateGetter(getter);
    }
    for (FieldConfiguration updater : updatersConfiguration.values()) {
        methodBuilder.generateUpdater(updater);
    }
    for (Map.Entry<String, PredicateConfigutation> predicate : predicates.entrySet()) {
        methodBuilder.generatePredicate(predicate.getKey(), predicate.getValue());
    }
    if (configuration.isValueClassComparable()) {
        methodBuilder.buildCompareTo();
    }
    methodBuilder.buildEqualsMethod();
    methodBuilder.buildHashCodeMethod(configuration.hashCodeBase());
    methodBuilder.buildToStringMethod();
    try {
        result.buildFactory(constructorMethods);
    } catch (JClassAlreadyExistsException ex) {
        throw new RuntimeException("Unexpected exception :)", ex);
    }
    return generation.createGenerationResult(valueClass);
}
Also used : JDefinedClass(com.helger.jcodemodel.JDefinedClass) JClassAlreadyExistsException(com.helger.jcodemodel.JClassAlreadyExistsException) PredicateConfigutation(com.github.sviperll.adt4j.model.config.PredicateConfigutation) GenerationProcess(com.github.sviperll.adt4j.model.util.GenerationProcess) JMethod(com.helger.jcodemodel.JMethod) Map(java.util.Map) FieldConfiguration(com.github.sviperll.adt4j.model.config.FieldConfiguration)

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