use of com.squareup.javapoet.TypeName in project react4j by react4j.
the class Generator method buildStaticCallbackMethod.
@Nonnull
private static MethodSpec.Builder buildStaticCallbackMethod(@Nonnull final ComponentDescriptor descriptor, @Nonnull final CallbackDescriptor callback) {
final TypeName handlerType = TypeName.get(callback.getCallbackType().asType());
final String handlerName = "_" + callback.getMethod().getSimpleName();
final MethodSpec.Builder method = MethodSpec.methodBuilder(handlerName).addAnnotation(NONNULL_CLASSNAME).returns(handlerType);
method.addModifiers(Modifier.STATIC);
final ParameterSpec.Builder parameter = ParameterSpec.builder(TypeName.get(descriptor.getElement().asType()), "component", Modifier.FINAL).addAnnotation(NONNULL_CLASSNAME);
method.addParameter(parameter.build());
if (!descriptor.getElement().getTypeParameters().isEmpty()) {
method.addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "unused").build());
ProcessorUtil.copyTypeParameters(descriptor.getElement(), method);
}
method.addStatement("return (($T) component).$N", descriptor.getEnhancedClassName(), handlerName);
return method;
}
use of com.squareup.javapoet.TypeName in project butterknife by JakeWharton.
the class BindingSet method addMethodBindings.
private void addMethodBindings(MethodSpec.Builder result, ViewBinding binding, boolean debuggable) {
Map<ListenerClass, Map<ListenerMethod, Set<MethodViewBinding>>> classMethodBindings = binding.getMethodBindings();
if (classMethodBindings.isEmpty()) {
return;
}
// We only need to emit the null check if there are zero required bindings.
boolean needsNullChecked = binding.getRequiredBindings().isEmpty();
if (needsNullChecked) {
result.beginControlFlow("if (view != null)");
}
// Add the view reference to the binding.
String fieldName = "viewSource";
String bindName = "source";
if (!binding.isBoundToRoot()) {
fieldName = "view" + Integer.toHexString(binding.getId().value);
bindName = "view";
}
result.addStatement("$L = $N", fieldName, bindName);
for (Map.Entry<ListenerClass, Map<ListenerMethod, Set<MethodViewBinding>>> e : classMethodBindings.entrySet()) {
ListenerClass listener = e.getKey();
Map<ListenerMethod, Set<MethodViewBinding>> methodBindings = e.getValue();
TypeSpec.Builder callback = TypeSpec.anonymousClassBuilder("").superclass(ClassName.bestGuess(listener.type()));
for (ListenerMethod method : getListenerMethods(listener)) {
MethodSpec.Builder callbackMethod = MethodSpec.methodBuilder(method.name()).addAnnotation(Override.class).addModifiers(PUBLIC).returns(bestGuess(method.returnType()));
String[] parameterTypes = method.parameters();
for (int i = 0, count = parameterTypes.length; i < count; i++) {
callbackMethod.addParameter(bestGuess(parameterTypes[i]), "p" + i);
}
boolean hasReturnType = !"void".equals(method.returnType());
CodeBlock.Builder builder = CodeBlock.builder();
if (hasReturnType) {
builder.add("return ");
}
if (methodBindings.containsKey(method)) {
for (MethodViewBinding methodBinding : methodBindings.get(method)) {
builder.add("target.$L(", methodBinding.getName());
List<Parameter> parameters = methodBinding.getParameters();
String[] listenerParameters = method.parameters();
for (int i = 0, count = parameters.size(); i < count; i++) {
if (i > 0) {
builder.add(", ");
}
Parameter parameter = parameters.get(i);
int listenerPosition = parameter.getListenerPosition();
if (parameter.requiresCast(listenerParameters[listenerPosition])) {
if (debuggable) {
builder.add("$T.castParam(p$L, $S, $L, $S, $L, $T.class)", UTILS, listenerPosition, method.name(), listenerPosition, methodBinding.getName(), i, parameter.getType());
} else {
builder.add("($T) p$L", parameter.getType(), listenerPosition);
}
} else {
builder.add("p$L", listenerPosition);
}
}
builder.add(");\n");
}
} else if (hasReturnType) {
builder.add("$L;\n", method.defaultReturn());
}
callbackMethod.addCode(builder.build());
callback.addMethod(callbackMethod.build());
}
boolean requiresRemoval = listener.remover().length() != 0;
String listenerField = null;
if (requiresRemoval) {
TypeName listenerClassName = bestGuess(listener.type());
listenerField = fieldName + ((ClassName) listenerClassName).simpleName();
result.addStatement("$L = $L", listenerField, callback.build());
}
if (!VIEW_TYPE.equals(listener.targetType())) {
result.addStatement("(($T) $N).$L($L)", bestGuess(listener.targetType()), bindName, listener.setter(), requiresRemoval ? listenerField : callback.build());
} else {
result.addStatement("$N.$L($L)", bindName, listener.setter(), requiresRemoval ? listenerField : callback.build());
}
}
if (needsNullChecked) {
result.endControlFlow();
}
}
use of com.squareup.javapoet.TypeName in project butterknife by JakeWharton.
the class BindingSet method addFieldAndUnbindStatement.
private void addFieldAndUnbindStatement(TypeSpec.Builder result, MethodSpec.Builder unbindMethod, ViewBinding bindings) {
// Only add fields to the binding if there are method bindings.
Map<ListenerClass, Map<ListenerMethod, Set<MethodViewBinding>>> classMethodBindings = bindings.getMethodBindings();
if (classMethodBindings.isEmpty()) {
return;
}
String fieldName = bindings.isBoundToRoot() ? "viewSource" : "view" + Integer.toHexString(bindings.getId().value);
result.addField(VIEW, fieldName, PRIVATE);
// We only need to emit the null check if there are zero required bindings.
boolean needsNullChecked = bindings.getRequiredBindings().isEmpty();
if (needsNullChecked) {
unbindMethod.beginControlFlow("if ($N != null)", fieldName);
}
for (ListenerClass listenerClass : classMethodBindings.keySet()) {
// We need to keep a reference to the listener
// in case we need to unbind it via a remove method.
boolean requiresRemoval = !"".equals(listenerClass.remover());
String listenerField = "null";
if (requiresRemoval) {
TypeName listenerClassName = bestGuess(listenerClass.type());
listenerField = fieldName + ((ClassName) listenerClassName).simpleName();
result.addField(listenerClassName, listenerField, PRIVATE);
}
if (!VIEW_TYPE.equals(listenerClass.targetType())) {
unbindMethod.addStatement("(($T) $N).$N($N)", bestGuess(listenerClass.targetType()), fieldName, removerOrSetter(listenerClass, requiresRemoval), listenerField);
} else {
unbindMethod.addStatement("$N.$N($N)", fieldName, removerOrSetter(listenerClass, requiresRemoval), listenerField);
}
if (requiresRemoval) {
unbindMethod.addStatement("$N = null", listenerField);
}
}
unbindMethod.addStatement("$N = null", fieldName);
if (needsNullChecked) {
unbindMethod.endControlFlow();
}
}
use of com.squareup.javapoet.TypeName in project web3sdk by FISCO-BCOS.
the class SolidityFunctionWrapper method buildVariableLengthReturnFunctionConstructor.
private static void buildVariableLengthReturnFunctionConstructor(MethodSpec.Builder methodBuilder, String functionName, String inputParameters, List<TypeName> outputParameterTypes) throws ClassNotFoundException {
List<Object> objects = new ArrayList<>();
objects.add(Function.class);
objects.add(Function.class);
objects.add(functionName);
objects.add(Arrays.class);
objects.add(Type.class);
objects.add(inputParameters);
objects.add(Arrays.class);
objects.add(TypeReference.class);
for (TypeName outputParameterType : outputParameterTypes) {
objects.add(TypeReference.class);
objects.add(outputParameterType);
}
String asListParams = Collection.join(outputParameterTypes, ", ", typeName -> "new $T<$T>() {}");
methodBuilder.addStatement("$T function = new $T($S, \n$T.<$T>asList($L), \n$T" + ".<$T<?>>asList(" + asListParams + "))", objects.toArray());
}
use of com.squareup.javapoet.TypeName in project Rocket by mozilla-tw.
the class AppModuleGenerator method generateGetExcludedModuleClasses.
// TODO: When we drop support for parsing GlideModules from AndroidManifests, remove this method.
private MethodSpec generateGetExcludedModuleClasses(Set<String> excludedClassNames) {
TypeName wildCardOfObject = WildcardTypeName.subtypeOf(Object.class);
ParameterizedTypeName classOfWildcardOfObjet = ParameterizedTypeName.get(ClassName.get(Class.class), wildCardOfObject);
ParameterizedTypeName setOfClassOfWildcardOfObject = ParameterizedTypeName.get(ClassName.get(Set.class), classOfWildcardOfObjet);
ParameterizedTypeName hashSetOfClassOfWildcardOfObject = ParameterizedTypeName.get(ClassName.get(HashSet.class), classOfWildcardOfObjet);
MethodSpec.Builder builder = MethodSpec.methodBuilder("getExcludedModuleClasses").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).returns(setOfClassOfWildcardOfObject);
if (excludedClassNames.isEmpty()) {
builder.addStatement("return $T.emptySet()", Collections.class);
} else {
builder.addStatement("$T excludedClasses = new $T()", setOfClassOfWildcardOfObject, hashSetOfClassOfWildcardOfObject);
for (String excludedClassName : excludedClassNames) {
// TODO: Remove this when we no longer support manifest parsing.
// Using a Literal ($L) instead of a type ($T) to get a fully qualified import that allows
// us to suppress deprecation warnings. Aimed at deprecated GlideModules.
builder.addStatement("excludedClasses.add($L.class)", excludedClassName);
}
builder.addStatement("return excludedClasses");
}
return builder.build();
}
Aggregations