use of com.squareup.javapoet.TypeName in project arez by arez.
the class TrackedDescriptor method buildTracked.
/**
* Generate the tracked wrapper.
* This is wrapped in case the user ever wants to explicitly call method
*/
@Nonnull
private MethodSpec buildTracked() throws ArezProcessorException {
assert null != _trackedMethod;
assert null != _trackedMethodType;
final MethodSpec.Builder builder = MethodSpec.methodBuilder(_trackedMethod.getSimpleName().toString());
ProcessorUtil.copyAccessModifiers(_trackedMethod, builder);
ProcessorUtil.copyExceptions(_trackedMethodType, builder);
ProcessorUtil.copyTypeParameters(_trackedMethodType, builder);
ProcessorUtil.copyDocumentedAnnotations(_trackedMethod, builder);
builder.addAnnotation(Override.class);
final TypeMirror returnType = _trackedMethodType.getReturnType();
builder.returns(TypeName.get(returnType));
final boolean isProcedure = returnType.getKind() == TypeKind.VOID;
final List<? extends TypeMirror> thrownTypes = _trackedMethod.getThrownTypes();
final boolean isSafe = thrownTypes.isEmpty();
final StringBuilder statement = new StringBuilder();
final ArrayList<Object> parameterNames = new ArrayList<>();
GeneratorUtil.generateNotDisposedInvariant(_componentDescriptor, builder);
if (!isProcedure) {
statement.append("return ");
}
statement.append("$N().");
parameterNames.add(_componentDescriptor.getContextMethodName());
if (isProcedure && isSafe) {
statement.append("safeTrack");
} else if (isProcedure) {
statement.append("track");
} else if (isSafe) {
statement.append("safeTrack");
} else {
statement.append("track");
}
statement.append("( this.$N, ");
parameterNames.add(getFieldName());
statement.append("() -> super.");
statement.append(_trackedMethod.getSimpleName());
statement.append("(");
boolean firstParam = true;
final List<? extends VariableElement> parameters = _trackedMethod.getParameters();
final int paramCount = parameters.size();
for (int i = 0; i < paramCount; i++) {
final VariableElement element = parameters.get(i);
final TypeName parameterType = TypeName.get(_trackedMethodType.getParameterTypes().get(i));
final ParameterSpec.Builder param = ParameterSpec.builder(parameterType, element.getSimpleName().toString(), Modifier.FINAL);
ProcessorUtil.copyDocumentedAnnotations(element, param);
builder.addParameter(param.build());
parameterNames.add(element.getSimpleName().toString());
if (!firstParam) {
statement.append(",");
}
firstParam = false;
statement.append("$N");
}
statement.append(")");
if (_reportParameters) {
for (final VariableElement parameter : parameters) {
parameterNames.add(parameter.getSimpleName().toString());
statement.append(", $N");
}
}
statement.append(" )");
GeneratorUtil.generateTryBlock(builder, thrownTypes, b -> b.addStatement(statement.toString(), parameterNames.toArray()));
return builder.build();
}
use of com.squareup.javapoet.TypeName in project ucsFramework by cjsoftware-lib.
the class UcsContractProcessor method ancestryContains.
private boolean ancestryContains(Class<?> ancestor, TypeElement element) {
List<? extends TypeMirror> interfaceList = element.getInterfaces();
ClassName ancestorTypeName = ClassName.get(ancestor);
for (TypeMirror typeMirror : interfaceList) {
if (typeMirror.getKind() == TypeKind.DECLARED) {
TypeName mirrorName = TypeName.get(typeMirror);
if (mirrorName instanceof ParameterizedTypeName) {
mirrorName = ((ParameterizedTypeName) mirrorName).rawType;
}
if (ancestorTypeName.equals(mirrorName)) {
return true;
} else {
DeclaredType declaredType = (DeclaredType) typeMirror;
return ancestryContains(ancestor, (TypeElement) declaredType.asElement());
}
}
}
return false;
}
use of com.squareup.javapoet.TypeName in project AndroidLife by CaMnter.
the class BindingSet method addMethodBindings.
/**
* 给 View 添加 listener
*
* @param result MethodSpec.Builder
* @param binding ViewBinding
*/
private void addMethodBindings(MethodSpec.Builder result, ViewBinding binding) {
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" + 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])) {
builder.add("$T.<$T>castParam(p$L, $S, $L, $S, $L)", UTILS, parameter.getType(), listenerPosition, method.name(), listenerPosition, methodBinding.getName(), i);
} 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 AndroidLife by CaMnter.
the class BindingSet method newBuilder.
/**
* 1.获取 .java 元素的类型
* 2.判断是否是 View, Activity 或者 Dialog
* 3.获取其 TypeName
* 4.获取其 packageName 和 className
* 5.构建一个 BindingSet.Builder
*
* @param enclosingElement .java 元素
* @return BindingSet.Builder
*/
static Builder newBuilder(TypeElement enclosingElement) {
TypeMirror typeMirror = enclosingElement.asType();
boolean isView = isSubtypeOfType(typeMirror, VIEW_TYPE);
boolean isActivity = isSubtypeOfType(typeMirror, ACTIVITY_TYPE);
boolean isDialog = isSubtypeOfType(typeMirror, DIALOG_TYPE);
TypeName targetType = TypeName.get(typeMirror);
if (targetType instanceof ParameterizedTypeName) {
targetType = ((ParameterizedTypeName) targetType).rawType;
}
String packageName = getPackage(enclosingElement).getQualifiedName().toString();
String className = enclosingElement.getQualifiedName().toString().substring(packageName.length() + 1).replace('.', '$');
ClassName bindingClassName = ClassName.get(packageName, className + "_ViewBinding");
boolean isFinal = enclosingElement.getModifiers().contains(Modifier.FINAL);
return new Builder(targetType, bindingClassName, isFinal, isView, isActivity, isDialog);
}
use of com.squareup.javapoet.TypeName in project AndroidLife by CaMnter.
the class BindingSet method addFieldAndUnbindStatement.
/**
* unbind 部分 - method 相关置空
*
* @param result TypeSpec.Builder
* @param unbindMethod MethodSpec.Builder
* @param bindings ViewBinding
*/
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" + 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();
}
}
Aggregations