com.sun.codemodel.JBlock._throw()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(10.9k)|赞(0)|评价(0)|浏览(110)

本文整理了Java中com.sun.codemodel.JBlock._throw()方法的一些代码示例,展示了JBlock._throw()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JBlock._throw()方法的具体详情如下:
包路径:com.sun.codemodel.JBlock
类名称:JBlock
方法名:_throw

JBlock._throw介绍

[英]Create a throw statement and add it to this block
[中]创建一个throw语句并将其添加到此块

代码示例

代码示例来源:origin: joelittlejohn/jsonschema2pojo

private void addSetProperty(JDefinedClass jclass, JBlock callSite, String propertyName, JType propertyType, JVar valueVar, JsonNode node) {
  JMethod propertySetter = jclass.getMethod(getSetterName(propertyName, node), new JType[] { propertyType });
  JConditional isInstance = callSite._if(valueVar._instanceof(propertyType.boxify().erasure()));
  isInstance._then()
  .invoke(propertySetter).arg(cast(propertyType.boxify(), valueVar));
  isInstance._else()
  ._throw(illegalArgumentInvocation(jclass, propertyName, propertyType, valueVar));
}

代码示例来源:origin: joelittlejohn/jsonschema2pojo

private JMethod addPublicSetMethod(JDefinedClass jclass, JMethod internalSetMethod) {
  JMethod method = jclass.method(PUBLIC, jclass.owner().VOID, SETTER_NAME);
  JVar nameParam = method.param(String.class, "name");
  JVar valueParam = method.param(Object.class, "value");
  JBlock body = method.body();
  JBlock notFound = body._if(JOp.not(invoke(internalSetMethod).arg(nameParam).arg(valueParam)))._then();
  // if we have additional properties, then put value.
  JMethod getAdditionalProperties = jclass.getMethod("getAdditionalProperties", new JType[] {});
  if (getAdditionalProperties != null) {
    JType additionalPropertiesType = ((JClass) (getAdditionalProperties.type())).getTypeParameters().get(1);
    notFound.add(invoke(getAdditionalProperties).invoke("put").arg(nameParam)
        .arg(cast(additionalPropertiesType, valueParam)));
  }
  // else throw exception.
  else {
    notFound._throw(illegalArgumentInvocation(jclass, nameParam));
  }
  return method;
}

代码示例来源:origin: joelittlejohn/jsonschema2pojo

private JMethod addPublicWithMethod(JDefinedClass jclass, JMethod internalSetMethod) {
  JMethod method = jclass.method(PUBLIC, jclass, BUILDER_NAME);
  JVar nameParam = method.param(String.class, "name");
  JVar valueParam = method.param(Object.class, "value");
  JBlock body = method.body();
  JBlock notFound = body._if(JOp.not(invoke(internalSetMethod).arg(nameParam).arg(valueParam)))._then();
  // if we have additional properties, then put value.
  JMethod getAdditionalProperties = jclass.getMethod("getAdditionalProperties", new JType[] {});
  if (getAdditionalProperties != null) {
    JType additionalPropertiesType = ((JClass) (getAdditionalProperties.type())).getTypeParameters().get(1);
    notFound.add(invoke(getAdditionalProperties).invoke("put").arg(nameParam)
        .arg(cast(additionalPropertiesType, valueParam)));
  }
  // else throw exception.
  else {
    notFound._throw(illegalArgumentInvocation(jclass, nameParam));
  }
  body._return(_this());
  return method;
}

代码示例来源:origin: apache/drill

JVar exVar = udfInitCatch.param("ex");
udfInitCatch.body()
 ._throw(JExpr._new(m.directClass(RuntimeException.class.getCanonicalName()))
  .arg(JExpr.lit(String.format("Failed to initialize GenericUDF"))).arg(exVar));

代码示例来源:origin: joelittlejohn/jsonschema2pojo

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(_enum, fromValue);
}

代码示例来源:origin: apache/drill

JVar exVar = udfEvalCatch.param("ex");
udfEvalCatch.body()
 ._throw(JExpr._new(m.directClass(RuntimeException.class.getCanonicalName()))
  .arg(JExpr.lit(String.format("GenericUDF.evaluate method failed"))).arg(exVar));

代码示例来源:origin: joelittlejohn/jsonschema2pojo

private JMethod addPublicGetMethod(JDefinedClass jclass, JMethod internalGetMethod, JFieldRef notFoundValue) {
  JMethod method = jclass.method(PUBLIC, jclass.owner()._ref(Object.class), GETTER_NAME);
  JTypeVar returnType = method.generify("T");
  method.type(returnType);
  Models.suppressWarnings(method, "unchecked");
  JVar nameParam = method.param(String.class, "name");
  JBlock body = method.body();
  JVar valueVar = body.decl(jclass.owner()._ref(Object.class), "value",
      invoke(internalGetMethod).arg(nameParam).arg(notFoundValue));
  JConditional found = method.body()._if(notFoundValue.ne(valueVar));
  found._then()._return(cast(returnType, valueVar));
  JBlock notFound = found._else();
  JMethod getAdditionalProperties = jclass.getMethod("getAdditionalProperties", new JType[] {});
  if (getAdditionalProperties != null) {
    notFound._return(cast(returnType, invoke(getAdditionalProperties).invoke("get").arg(nameParam)));
  } else {
    notFound._throw(illegalArgumentInvocation(jclass, nameParam));
  }
  return method;
}

代码示例来源:origin: org.jvnet.ws.wadl/wadl-core

/**
 * Invoked when we need to throw a generic failure exception because
 * we don't have an element mapped.
 */
protected void generateThrowWebApplicationExceptionFromResponse(JBlock caseBody, JVar $response) {
  // In RS 2.0 we can pass in the response object as they
  // are consistent across the API
  
  caseBody._throw(
    JExpr._new(webApplicationExceptionType())
      .arg($response));
}

代码示例来源:origin: org.codehaus.xfire/xfire-generator

@Override
protected void annotate(GenerationContext context, OperationInfo op, JMethod method)
{
  JType ex = context.getCodeModel()._ref(UnsupportedOperationException.class);
  method.body()._throw(JExpr._new(ex));
}

代码示例来源:origin: net.anwiba.commons.tools/anwiba-tools-generator-bean

private void addRuntimeExceptionThrowingCatchTo(final JTryBlock block, final JClass exceptionClass) {
 final JCatchBlock catchBlock = block._catch(exceptionClass);
 final JVar catchedException = catchBlock.param("exception");
 final JBlock catchBody = catchBlock.body();
 catchBody._throw(JExpr._new(_classByNames(RuntimeException.class.getName())).arg(catchedException));
}

代码示例来源:origin: com.sun.xml.ws/jaxws-tools

private void writeGetWsdlLocation(JType retType, JDefinedClass cls, JFieldVar urlField, JFieldVar exField) {
  JMethod m = cls.method(JMod.PRIVATE|JMod.STATIC , retType, "__getWsdlLocation");
  JConditional ifBlock = m.body()._if(exField.ne(JExpr._null()));
  ifBlock._then()._throw(exField);
  m.body()._return(urlField);
}

代码示例来源:origin: org.glassfish.metro/webservices-tools

private void writeGetWsdlLocation(JType retType, JDefinedClass cls, JFieldVar urlField, JFieldVar exField) {
  JMethod m = cls.method(JMod.PRIVATE|JMod.STATIC , retType, "__getWsdlLocation");
  JConditional ifBlock = m.body()._if(exField.ne(JExpr._null()));
  ifBlock._then()._throw(exField);
  m.body()._return(urlField);
}

代码示例来源:origin: javaee/metro-jax-ws

private void writeGetWsdlLocation(JType retType, JDefinedClass cls, JFieldVar urlField, JFieldVar exField) {
  JMethod m = cls.method(JMod.PRIVATE|JMod.STATIC , retType, "__getWsdlLocation");
  JConditional ifBlock = m.body()._if(exField.ne(JExpr._null()));
  ifBlock._then()._throw(exField);
  m.body()._return(urlField);
}

代码示例来源:origin: javaee/metro-jax-ws

private void writeGetWsdlLocation(JType retType, JDefinedClass cls, JFieldVar urlField, JFieldVar exField) {
  JMethod m = cls.method(JMod.PRIVATE|JMod.STATIC , retType, "__getWsdlLocation");
  JConditional ifBlock = m.body()._if(exField.ne(JExpr._null()));
  ifBlock._then()._throw(exField);
  m.body()._return(urlField);
}

代码示例来源:origin: org.jvnet.ws.wadl/wadl-core

/**
 * Invoked when we need to throw a generic failure exception because
 * we don't have an element mapped.
 */
protected void generateThrowWebApplicationExceptionFromResponse(JBlock caseBody, JVar $response) {
  // Just for a WebApplicationException in this case
  // with the right status code, in RS 2.0 we can provide
  // the entire reponse
  
  caseBody._throw(
    JExpr._new(webApplicationExceptionType())
      .arg(
       codeModel.ref(Response.class).staticInvoke("status")
        .arg($response.invoke("getClientResponseStatus")).invoke("build")));
}

代码示例来源:origin: bonitasoft/bonita-engine

private void addNotNullParamCheck(final JBlock methodBody, final JVar param) {
  methodBody._if(param.eq(JExpr._null()))._then()
      ._throw(JExpr._new(getModel().ref(IllegalArgumentException.class))
          .arg(JExpr.lit(param.name() + " cannot be null")));
}

代码示例来源:origin: bonitasoft/bonita-engine

private void addNotNullParamCheck(final JBlock methodBody, final JVar param) {
  methodBody._if(param.eq(JExpr._null()))._then()
      ._throw(JExpr._new(getModel().ref(IllegalArgumentException.class))
          .arg(JExpr.lit(param.name() + " cannot be null")));
}

代码示例来源:origin: org.jsonschema2pojo/jsonschema2pojo-core

private void addSetProperty(JDefinedClass jclass, JBlock callSite, String propertyName, JType propertyType, JVar valueVar, JsonNode node) {
  JMethod propertySetter = jclass.getMethod(getSetterName(propertyName, node), new JType[] { propertyType });
  JConditional isInstance = callSite._if(valueVar._instanceof(propertyType.boxify().erasure()));
  isInstance._then()
  .invoke(propertySetter).arg(cast(propertyType.boxify(), valueVar));
  isInstance._else()
  ._throw(illegalArgumentInvocation(jclass, propertyName, propertyType, valueVar));
}

代码示例来源:origin: com.envoisolutions.sxc/sxc-jaxb

public JFieldVar getDatatypeFactory() {
  if (datatypeFactory == null) {
    datatypeFactory = jaxbObjectClass.field(JMod.PRIVATE | JMod.FINAL, builderContext.toJClass(DatatypeFactory.class), fieldManager.createId("datatypeFactory"));
    // add code to constructor which initializes the static dtFactory field
    JTryBlock tryBlock = constructor.body()._try();
    tryBlock.body().assign(datatypeFactory, builderContext.toJClass(DatatypeFactory.class).staticInvoke("newInstance"));
    JCatchBlock catchBlock = tryBlock._catch((builderContext.toJClass(DatatypeConfigurationException.class)));
    catchBlock.body()._throw(JExpr._new(builderContext.toJClass(RuntimeException.class)).arg("Unable to initialize DatatypeFactory").arg(catchBlock.param("e")));
  }
  return datatypeFactory;
}

代码示例来源:origin: org.metatype.sxc/sxc-jaxb

public JFieldVar getDatatypeFactory() {
  if (datatypeFactory == null) {
    datatypeFactory = jaxbObjectClass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, builderContext.toJClass(DatatypeFactory.class), fieldManager.createId("datatypeFactory"));
    // add code to constructor which initializes the static dtFactory field
    JTryBlock tryBlock = jaxbObjectClass.init()._try();
    tryBlock.body().assign(datatypeFactory, builderContext.toJClass(DatatypeFactory.class).staticInvoke("newInstance"));
    JCatchBlock catchBlock = tryBlock._catch((builderContext.toJClass(DatatypeConfigurationException.class)));
    catchBlock.body()._throw(JExpr._new(builderContext.toJClass(RuntimeException.class)).arg("Unable to initialize DatatypeFactory").arg(catchBlock.param("e")));
  }
  return datatypeFactory;
}

相关文章