本文整理汇总了Java中com.sun.codemodel.JInvocation类的典型用法代码示例。如果您正苦于以下问题:Java JInvocation类的具体用法?Java JInvocation怎么用?Java JInvocation使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JInvocation类属于com.sun.codemodel包,在下文中一共展示了JInvocation类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addFactoryMethod
import com.sun.codemodel.JInvocation; //导入依赖的package包/类
private void addFactoryMethod(JDefinedClass _enum, JType backingType) {
JFieldVar quickLookupMap = addQuickLookupMap(_enum, backingType);
JMethod fromValue = _enum.method(JMod.PUBLIC | JMod.STATIC, _enum, "fromValue");
JVar valueParam = fromValue.param(backingType, "value");
JBlock body = fromValue.body();
JVar constant = body.decl(_enum, "constant");
constant.init(quickLookupMap.invoke("get").arg(valueParam));
JConditional _if = body._if(constant.eq(JExpr._null()));
JInvocation illegalArgumentException = JExpr._new(_enum.owner().ref(IllegalArgumentException.class));
JExpression expr = valueParam;
// if string no need to add ""
if(!isString(backingType)){
expr = expr.plus(JExpr.lit(""));
}
illegalArgumentException.arg(expr);
_if._then()._throw(illegalArgumentException);
_if._else()._return(constant);
ruleFactory.getAnnotator().enumCreatorMethod(fromValue);
}
示例2: getDefaultSet
import com.sun.codemodel.JInvocation; //导入依赖的package包/类
/**
* Creates a default value for a set property by:
* <ol>
* <li>Creating a new {@link LinkedHashSet} with the correct generic type
* <li>Using {@link Arrays#asList(Object...)} to initialize the set with the
* correct default values
* </ol>
*
* @param fieldType
* the java type that applies for this field ({@link Set} with
* some generic type argument)
* @param node
* the node containing default values for this set
* @return an expression that creates a default value that can be assigned
* to this field
*/
private JExpression getDefaultSet(JType fieldType, JsonNode node) {
JClass setGenericType = ((JClass) fieldType).getTypeParameters().get(0);
JClass setImplClass = fieldType.owner().ref(LinkedHashSet.class);
setImplClass = setImplClass.narrow(setGenericType);
JInvocation newSetImpl = JExpr._new(setImplClass);
if (node instanceof ArrayNode && node.size() > 0) {
JInvocation invokeAsList = fieldType.owner().ref(Arrays.class).staticInvoke("asList");
for (JsonNode defaultValue : node) {
invokeAsList.arg(getDefaultValue(setGenericType, defaultValue));
}
newSetImpl.arg(invokeAsList);
} else if (!ruleFactory.getGenerationConfig().isInitializeCollections()) {
return JExpr._null();
}
return newSetImpl;
}
示例3: getDefaultList
import com.sun.codemodel.JInvocation; //导入依赖的package包/类
/**
* Creates a default value for a list property by:
* <ol>
* <li>Creating a new {@link ArrayList} with the correct generic type
* <li>Using {@link Arrays#asList(Object...)} to initialize the list with
* the correct default values
* </ol>
*
* @param fieldType
* the java type that applies for this field ({@link List} with
* some generic type argument)
* @param node
* the node containing default values for this list
* @return an expression that creates a default value that can be assigned
* to this field
*/
private JExpression getDefaultList(JType fieldType, JsonNode node) {
JClass listGenericType = ((JClass) fieldType).getTypeParameters().get(0);
JClass listImplClass = fieldType.owner().ref(ArrayList.class);
listImplClass = listImplClass.narrow(listGenericType);
JInvocation newListImpl = JExpr._new(listImplClass);
if (node instanceof ArrayNode && node.size() > 0) {
JInvocation invokeAsList = fieldType.owner().ref(Arrays.class).staticInvoke("asList");
for (JsonNode defaultValue : node) {
invokeAsList.arg(getDefaultValue(listGenericType, defaultValue));
}
newListImpl.arg(invokeAsList);
} else if (!ruleFactory.getGenerationConfig().isInitializeCollections()) {
return JExpr._null();
}
return newListImpl;
}
示例4: addToString
import com.sun.codemodel.JInvocation; //导入依赖的package包/类
private void addToString(JDefinedClass jclass) {
Map<String, JFieldVar> fields = jclass.fields();
JMethod toString = jclass.method(JMod.PUBLIC, String.class, "toString");
Set<String> excludes = new HashSet<String>(Arrays.asList(ruleFactory.getGenerationConfig().getToStringExcludes()));
JBlock body = toString.body();
Class<?> toStringBuilder = ruleFactory.getGenerationConfig().isUseCommonsLang3() ? org.apache.commons.lang3.builder.ToStringBuilder.class : org.apache.commons.lang.builder.ToStringBuilder.class;
JClass toStringBuilderClass = jclass.owner().ref(toStringBuilder);
JInvocation toStringBuilderInvocation = JExpr._new(toStringBuilderClass).arg(JExpr._this());
if (!jclass._extends().fullName().equals(Object.class.getName())) {
toStringBuilderInvocation = toStringBuilderInvocation.invoke("appendSuper").arg(JExpr._super().invoke("toString"));
}
for (JFieldVar fieldVar : fields.values()) {
if (excludes.contains(fieldVar.name()) || (fieldVar.mods().getValue() & JMod.STATIC) == JMod.STATIC) {
continue;
}
toStringBuilderInvocation = toStringBuilderInvocation.invoke("append").arg(fieldVar.name()).arg(fieldVar);
}
body._return(toStringBuilderInvocation.invoke("toString"));
toString.annotate(Override.class);
}
示例5: addHashCode
import com.sun.codemodel.JInvocation; //导入依赖的package包/类
private void addHashCode(JDefinedClass jclass, JsonNode node) {
Map<String, JFieldVar> fields = removeFieldsExcludedFromEqualsAndHashCode(jclass.fields(), node);
JMethod hashCode = jclass.method(JMod.PUBLIC, int.class, "hashCode");
Class<?> hashCodeBuilder = ruleFactory.getGenerationConfig().isUseCommonsLang3() ? org.apache.commons.lang3.builder.HashCodeBuilder.class : org.apache.commons.lang.builder.HashCodeBuilder.class;
JBlock body = hashCode.body();
JClass hashCodeBuilderClass = jclass.owner().ref(hashCodeBuilder);
JInvocation hashCodeBuilderInvocation = JExpr._new(hashCodeBuilderClass);
if (!jclass._extends().fullName().equals(Object.class.getName())) {
hashCodeBuilderInvocation = hashCodeBuilderInvocation.invoke("appendSuper").arg(JExpr._super().invoke("hashCode"));
}
for (JFieldVar fieldVar : fields.values()) {
if ((fieldVar.mods().getValue() & JMod.STATIC) == JMod.STATIC) {
continue;
}
hashCodeBuilderInvocation = hashCodeBuilderInvocation.invoke("append").arg(fieldVar);
}
body._return(hashCodeBuilderInvocation.invoke("toHashCode"));
hashCode.annotate(Override.class);
}
示例6: generateCode
import com.sun.codemodel.JInvocation; //导入依赖的package包/类
@Override
void generateCode(ClassGenerator<WindowFramer> cg) {
final GeneratorMapping mapping = GeneratorMapping.create("setupPartition", "outputRow", "resetValues", "cleanup");
final MappingSet mappingSet = new MappingSet(null, "outIndex", mapping, mapping);
cg.setMappingSet(mappingSet);
final JVar vv = cg.declareVectorValueSetupAndMember(cg.getMappingSet().getOutgoing(), fieldId);
final JExpression outIndex = cg.getMappingSet().getValueWriteIndex();
JInvocation setMethod = vv.invoke("getMutator").invoke("setSafe").arg(outIndex)
.arg(JExpr.direct("partition.ntile(" + numTiles + ")"));
cg.getEvalBlock().add(setMethod);
}
示例7: declareVectorValueSetupAndMember
import com.sun.codemodel.JInvocation; //导入依赖的package包/类
public JVar declareVectorValueSetupAndMember(DirectExpression batchName, TypedFieldId fieldId) {
final ValueVectorSetup setup = new ValueVectorSetup(batchName, fieldId);
final Class<?> valueVectorClass = fieldId.getIntermediateClass();
final JClass vvClass = model.ref(valueVectorClass);
final JClass retClass = fieldId.isHyperReader() ? vvClass.array() : vvClass;
final JVar vv = declareClassField("vv", retClass);
final JBlock b = getSetupBlock();
int[] fieldIndices = fieldId.getFieldIds();
JInvocation invoke = model.ref(VectorResolver.class).staticInvoke(fieldId.isHyperReader() ? "hyper" : "simple")
.arg(batchName)
.arg(vvClass.dotclass());
for(int i = 0; i < fieldIndices.length; i++){
invoke.arg(JExpr.lit(fieldIndices[i]));
}
// we have to cast here since Janino doesn't handle generic inference well.
JExpression casted = JExpr.cast(retClass, invoke);
b.assign(vv, casted);
vvDeclaration.put(setup, vv);
return vv;
}
示例8: getUDFInstance
import com.sun.codemodel.JInvocation; //导入依赖的package包/类
private JInvocation getUDFInstance(JCodeModel m) {
if (isGenericUDF) {
return JExpr._new(m.directClass(genericUdfClazz.getCanonicalName()));
} else {
return JExpr._new(m.directClass(GenericUDFBridge.class.getCanonicalName()))
.arg(JExpr.lit(udfName))
.arg(JExpr.lit(false))
.arg(JExpr.lit(udfClazz.getCanonicalName().toString()));
}
}
示例9: createAddIfNullSetEmptyArrayAndReturnClosure
import com.sun.codemodel.JInvocation; //导入依赖的package包/类
public static IProcedure<JVar, RuntimeException> createAddIfNullSetEmptyArrayAndReturnClosure(
final JCodeModel codeModel,
final JMethod method,
final JExpression returnValue) {
ensureThatArgument(method, notNull());
return new IProcedure<JVar, RuntimeException>() {
@Override
public void execute(final JVar param) throws RuntimeException {
ensureThatArgument(param, notNull());
final ValueConverter valueConverter = new ValueConverter(codeModel);
final JInvocation invocation = JExpr._new(param.type());
method
.body()
._if(param.eq(JExpr._null()))
._then()
.block()
.assign(JExpr.refthis(param.name()), valueConverter.convert(invocation))
._return(returnValue);
}
};
}
示例10: addCode
import com.sun.codemodel.JInvocation; //导入依赖的package包/类
protected void addCode(JMethod method, AbstractType tp, String kind, Action action) {
ArrayList<Link> arrayList = links.get(tp);
if (arrayList == null) {
arrayList = new ArrayList<>();
links.put(tp, arrayList);
}
String role = kind;
if (kind.equals("get")) {
role = "self";
}
arrayList.add(new Link(role, action.resource().getUri(), action.method()));
JType type = context.getType(tp);
if (type == null) {
return;
}
JInvocation iv = JExpr.invoke(JExpr.ref("manager"), kind).arg(JExpr.dotclass((JClass) type))
.arg(JExpr.ref(method.name() + "_meta"));
method.body().decl((JClass) type, "result", iv);
method.params().forEach(x -> iv.arg(x));
}
示例11: testClassWithPersistenceContextWithKonfiguredUnitNameSpecified
import com.sun.codemodel.JInvocation; //导入依赖的package包/类
@Test
public void testClassWithPersistenceContextWithKonfiguredUnitNameSpecified() throws Exception {
// GIVEN
final JCodeModel jCodeModel = new JCodeModel();
final JPackage jp = jCodeModel.rootPackage();
final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest");
final JFieldVar ruleField = jClass.field(JMod.PUBLIC, JpaUnitRule.class, "rule");
ruleField.annotate(Rule.class);
final JInvocation instance = JExpr._new(jCodeModel.ref(JpaUnitRule.class)).arg(JExpr.direct("getClass()"));
ruleField.init(instance);
final JFieldVar emField = jClass.field(JMod.PRIVATE, EntityManager.class, "em");
final JAnnotationUse jAnnotation = emField.annotate(PersistenceContext.class);
jAnnotation.param("unitName", "test-unit-1");
final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod");
jMethod.annotate(Test.class);
buildModel(testFolder.getRoot(), jCodeModel);
compileModel(testFolder.getRoot());
final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name());
final BlockJUnit4ClassRunner runner = new BlockJUnit4ClassRunner(cut);
final RunListener listener = mock(RunListener.class);
final RunNotifier notifier = new RunNotifier();
notifier.addListener(listener);
// WHEN
runner.run(notifier);
// THEN
final ArgumentCaptor<Description> descriptionCaptor = ArgumentCaptor.forClass(Description.class);
verify(listener).testStarted(descriptionCaptor.capture());
assertThat(descriptionCaptor.getValue().getClassName(), equalTo("ClassUnderTest"));
assertThat(descriptionCaptor.getValue().getMethodName(), equalTo("testMethod"));
verify(listener).testFinished(descriptionCaptor.capture());
assertThat(descriptionCaptor.getValue().getClassName(), equalTo("ClassUnderTest"));
assertThat(descriptionCaptor.getValue().getMethodName(), equalTo("testMethod"));
}
示例12: addCtor
import com.sun.codemodel.JInvocation; //导入依赖的package包/类
/**
* The code generator creates a method called __DRILL_INIT__ which takes the
* place of the constructor when the code goes though the byte code merge.
* For Plain-old Java, we call the method from a constructor created for
* that purpose. (Generated code, fortunately, never includes a constructor,
* so we can create one.) Since the init block throws an exception (which
* should never occur), the generated constructor converts the checked
* exception into an unchecked one so as to not require changes to the
* various places that create instances of the generated classes.
*
* Example:<code><pre>
* public StreamingAggregatorGen1() {
* try {
* __DRILL_INIT__();
* } catch (SchemaChangeException e) {
* throw new UnsupportedOperationException(e);
* }
* }</pre></code>
*
* Note: in Java 8 we'd use the <tt>Parameter</tt> class defined in Java's
* introspection package. But, Drill prefers Java 7 which only provides
* parameter types.
*/
private void addCtor(Class<?>[] parameters) {
JMethod ctor = clazz.constructor(JMod.PUBLIC);
JBlock body = ctor.body();
// If there are parameters, need to pass them to the super class.
if (parameters.length > 0) {
JInvocation superCall = JExpr.invoke("super");
// This case only occurs for nested classes, and all nested classes
// in Drill are inner classes. Don't pass along the (hidden)
// this$0 field.
for (int i = 1; i < parameters.length; i++) {
Class<?> p = parameters[i];
superCall.arg(ctor.param(model._ref(p), "arg" + i));
}
body.add(superCall);
}
JTryBlock tryBlock = body._try();
tryBlock.body().invoke(SignatureHolder.DRILL_INIT_METHOD);
JCatchBlock catchBlock = tryBlock._catch(model.ref(SchemaChangeException.class));
catchBlock.body()._throw(JExpr._new(model.ref(UnsupportedOperationException.class)).arg(catchBlock.param("e")));
}
示例13: withEquals
import com.sun.codemodel.JInvocation; //导入依赖的package包/类
private void withEquals() {
JMethod equals = this.pojo.method(JMod.PUBLIC, boolean.class, "equals");
JVar otherObject = equals.param(Object.class, "other");
Class<?> equalsBuilderClass = org.apache.commons.lang3.builder.EqualsBuilder.class;
if (!config.isUseCommonsLang3()) {
equalsBuilderClass = org.apache.commons.lang.builder.EqualsBuilder.class;
}
JBlock body = equals.body();
body._if(otherObject.eq(JExpr._null()))._then()._return(JExpr.FALSE);
body._if(otherObject.eq(JExpr._this()))._then()._return(JExpr.TRUE);
body._if(JExpr._this().invoke("getClass").ne(otherObject.invoke("getClass")))._then()._return(JExpr.FALSE);
JVar otherObjectVar = body.decl(this.pojo, "otherObject").init(JExpr.cast(this.pojo, otherObject));
JClass equalsBuilderRef = this.pojo.owner().ref(equalsBuilderClass);
JInvocation equalsBuilderInvocation = appendFieldsToEquals(getNonTransientAndNonStaticFields(), otherObjectVar, equalsBuilderRef);
body._return(equalsBuilderInvocation.invoke("isEquals"));
}
示例14: caseAPrimaryMethodInvocation
import com.sun.codemodel.JInvocation; //导入依赖的package包/类
@Override
public void caseAPrimaryMethodInvocation(APrimaryMethodInvocation node) {
ExpressionAdapter ea = new ExpressionAdapter(new JExprParent(), context);
node.getExpressionNoName().apply(ea);
JExpression lhs = ea.expr;
JInvocation inv = parent.invoke(lhs, node.getIdentifier().getText());
if (!node.getNonWildTypeArguments().isEmpty()) {
Logger.error(context.getFile(), node.getIdentifier(), "CodeModel does not support type arguments for methods, yet. Ignoring...");
}
Argumentable ia = new InvocationArgumentable(inv);
for (PArgument arg : node.getArgument()) {
ArgumentAdapter aa = new ArgumentAdapter(ia, context);
arg.apply(aa);
}
expr = inv;
}
示例15: caseAClassMethodInvocation
import com.sun.codemodel.JInvocation; //导入依赖的package包/类
@Override
public void caseAClassMethodInvocation(AClassMethodInvocation node) {
AClassName acname = (AClassName) node.getClassName();
String name = nameToString(acname.getName());
JClass jc = context.resolveType(acname.getName());
JExpression jcs = JExpr.dotsuper(jc);
JInvocation inv = jcs.invoke(node.getIdentifier().getText());
Argumentable ia = new InvocationArgumentable(inv);
for (PArgument arg : node.getArgument()) {
ArgumentAdapter aa = new ArgumentAdapter(ia, context);
arg.apply(aa);
}
expr = inv;
parent.addInvocation(inv);
}