右手singleton.getinstance()赋值需要CodeModel帮助 [英] CodeModel help needed for right-hand singleton.getinstance() assignment

查看:97
本文介绍了右手singleton.getinstance()赋值需要CodeModel帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够使用CodeModel API生成99%的所需内容,但我在这里难倒......

I've been able to generate 99% of what I need with the CodeModel API, but I am stumped here...

使用各种directXX方法不会将import语句添加到生成的代码中,除了生成的类中的一个位置之外,我可以在没有directXXX类型的方法的情况下工作。

Using the various "directXX" methods does not add import statements to the generated code, and I can work without the "directXXX" type of methods except for one place in a generated class.

假设我想要生成的方法如:

Suppose I desire a generated method like:

/**
* Copies data from this Value-Obj instance, to the returned PERSON instance.
* 
* @return PERSON
* 
*/
public PERSON mapVOToPERSON() throws MappingException
{
   Mapper mapper = (com.blah.util.MapperSingleton.getMapperInstance());
   return mapper.map(this, PERSON.class);
}

您可以在parens中看到Mapper赋值的右侧。发出整个包+类是我能找到的唯一方法就是在右侧声明SomeSingleton.someMethod()并生成生成的代码。如果没有将MapperSingleton添加到对象模型,则不会导入导入...

You can see the right hand of the Mapper assignment in parens. Emitting the entire package+class was the only way I could find to just declare "SomeSingleton.someMethod()" on the right hand side and have the generated code compile. Without the MapperSingleton being added to the object model, there is no import generated...

问题:

1)有没有办法强制导入生成?

1) Is there a way to force an import to be generated?

2)如何声明一个表达式,它给出了对象模型中Mapper赋值的右侧(以便生成MapperSingleton的导入。

2) How to declare an expression that gives me the right side of the Mapper assignment within the object model (so that an import of MapperSingleton gets generated.

任何帮助表示感谢...

Any help appreciated...

推荐答案

也许我完全不理解这个问题,但这样的代码好吗?

Maybe I don't understand the question fully but, is code like this OK?

JCodeModel model = new JCodeModel();
JClass mapper = model.directClass("com.another.Mapper");
JClass factory = model.directClass("com.another.MapperSingleton");
JDefinedClass dc = model._class("com.example.Something");
JMethod method = dc.method(JMod.PUBLIC | JMod.STATIC, mapper, "testMethod");
method.body()._return(factory.staticInvoke("getMapperInstance"));
model.build(destinationDirectory);

它将生成

package com.example;

import com.another.Mapper;
import com.another.MapperSingleton;

public class Something {


    public static Mapper testMethod() {
        return MapperSingleton.getMapperInstance();
    }

}

这是CodeModel 2.4

This is with CodeModel 2.4

编辑!
第二次尝试

EDIT! Second try

    JCodeModel model = new JCodeModel();
    JClass mapper = model.directClass("com.blah.util.Mapper");
    JClass factory = model.directClass("com.blah.util.MapperSingleton");
    JDefinedClass dc = model._class("com.example.Something");
    JDefinedClass person = model._class("com.example.PERSON");
    JMethod method = dc.method(JMod.PUBLIC, person, "mapVOToPERSON");
    JBlock block = method.body();
    JVar lhs = block.decl(mapper, "mapper", factory.staticInvoke("getMapperInstance"));
    JInvocation map = lhs.invoke("map");
    map.arg(JExpr._this()); 
    map.arg(person.dotclass());
    method.body()._return(map);
    model.build(destinationDirectory);

生成

package com.example;

import com.blah.util.Mapper;
import com.blah.util.MapperSingleton;

public class Something {


    public PERSON mapVOToPERSON() {
        Mapper mapper = MapperSingleton.getMapperInstance();
        return mapper.map(this, PERSON.class);
    }

}

这篇关于右手singleton.getinstance()赋值需要CodeModel帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆