本文整理匯總了Java中com.squareup.javapoet.ParameterSpec.builder方法的典型用法代碼示例。如果您正苦於以下問題:Java ParameterSpec.builder方法的具體用法?Java ParameterSpec.builder怎麽用?Java ParameterSpec.builder使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.squareup.javapoet.ParameterSpec
的用法示例。
在下文中一共展示了ParameterSpec.builder方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: mimicParameters
import com.squareup.javapoet.ParameterSpec; //導入方法依賴的package包/類
/** Compose method parameters that mimic original code. */
@NonNull
public static StringBuilder mimicParameters(@NonNull final MethodSpec.Builder builder,
@NonNull final Symbol.MethodSymbol ms) throws Exception {
String delimiter = "";
final StringBuilder arguments = new StringBuilder();
final com.sun.tools.javac.util.List<Symbol.VarSymbol> parameters = ms.getParameters();
for (int i = 0, len = parameters.size(); i < len; i++) {
final Symbol.VarSymbol param = parameters.get(i);
// mimic parameter of the method: name, type, modifiers
final TypeName paramType = TypeName.get(param.asType());
final String parameterName = param.name.toString();
final ParameterSpec.Builder parameter = ParameterSpec.builder(paramType, parameterName, Modifier.FINAL);
if (param.hasAnnotations()) {
// DONE: copy annotations of parameter
for (final Attribute.Compound am : param.getAnnotationMirrors()) {
final AnnotationSpec.Builder builderAnnotation = mimicAnnotation(am);
if (null != builderAnnotation) {
parameter.addAnnotation(builderAnnotation.build());
}
}
}
// support VarArgs if needed
builder.varargs(ms.isVarArgs() && i == len - 1);
builder.addParameter(parameter.build());
// compose parameters list for forwarding
arguments.append(delimiter).append(parameterName);
delimiter = ", ";
}
return arguments;
}
示例2: buildEventHandlerActionMethod
import com.squareup.javapoet.ParameterSpec; //導入方法依賴的package包/類
@Nonnull
private static MethodSpec.Builder buildEventHandlerActionMethod( @Nonnull final EventHandlerDescriptor eventHandler )
{
final MethodSpec.Builder method =
MethodSpec.methodBuilder( eventHandler.getMethod().getSimpleName().toString() ).
returns( TypeName.get( eventHandler.getMethodType().getReturnType() ) );
ProcessorUtil.copyTypeParameters( eventHandler.getMethodType(), method );
ProcessorUtil.copyAccessModifiers( eventHandler.getMethod(), method );
ProcessorUtil.copyDocumentedAnnotations( eventHandler.getMethod(), method );
final AnnotationSpec.Builder annotation =
AnnotationSpec.builder( ACTION_CLASSNAME ).
addMember( "reportParameters", "false" );
method.addAnnotation( annotation.build() );
final int paramCount = eventHandler.getMethod().getParameters().size();
for ( int i = 0; i < paramCount; i++ )
{
final TypeMirror paramType = eventHandler.getMethodType().getParameterTypes().get( i );
final ParameterSpec.Builder parameter =
ParameterSpec.builder( TypeName.get( paramType ), "arg" + i, Modifier.FINAL );
ProcessorUtil.copyDocumentedAnnotations( eventHandler.getMethod().getParameters().get( i ), parameter );
method.addParameter( parameter.build() );
}
final String params =
0 == paramCount ?
"" :
IntStream.range( 0, paramCount ).mapToObj( i -> "arg" + i ).collect( Collectors.joining( "," ) );
final boolean isVoid = eventHandler.getMethodType().getReturnType().getKind() == TypeKind.VOID;
method.addStatement( ( isVoid ? "" : "return " ) + "super.$N(" + params + ")",
eventHandler.getMethod().getSimpleName() );
return method;
}
示例3: createFactoryMethod
import com.squareup.javapoet.ParameterSpec; //導入方法依賴的package包/類
public MethodSpec createFactoryMethod(OutputSpec spec) {
MethodSpec.Builder factory =
MethodSpec.methodBuilder(asCamelCase(value.name()))
.addTypeVariables(spec.typeVariables())
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(spec.parameterizedOutputClass());
StringBuilder newString = new StringBuilder();
List<Object> newArgs = new ArrayList<>();
newString.append("return new $T(");
newArgs.add(value.parameterizedOutputClass());
boolean first = true;
for (Parameter parameter : value.parameters()) {
ParameterSpec.Builder builder = ParameterSpec.builder(parameter.type(), parameter.name());
if (!parameter.type().isPrimitive()) {
if (parameter.canBeNull()) {
builder.addAnnotation(Nullable.class);
} else {
builder.addAnnotation(Nonnull.class);
}
}
factory.addParameter(builder.build());
if (first) {
newString.append("$L");
first = false;
} else {
newString.append(", $L");
}
newArgs.add(parameter.name());
}
newString.append(")");
if (spec.hasTypeVariables()) {
newString.append(".as$L()");
newArgs.add(spec.outputClass().simpleName());
}
factory.addStatement(newString.toString(), newArgs.toArray());
return factory.build();
}