private void emitCollectionBinding(StringBuilder builder, CollectionBinding binding, int[] ids) { builder.append(" target.").append(binding.getName()).append(" = "); switch (binding.getKind()) { case ARRAY: builder.append("Finder.arrayOf("); break; case LIST: builder.append("Finder.listOf("); break; default: throw new IllegalStateException("Unknown kind: " + binding.getKind()); } for (int i = 0; i < ids.length; i++) { if (i > 0) { builder.append(','); } builder.append("\n "); emitCastIfNeeded(builder, binding.getType()); if (binding.isRequired()) { builder .append("finder.findRequiredView(source, ") .append(ids[i]) .append(", \"") .append(binding.getName()) .append("\")"); } else { builder.append("finder.findOptionalView(source, ").append(ids[i]).append(")"); } } builder.append("\n );"); }
private void emitViewBindings(StringBuilder builder, ViewInjection injection) { Collection<ViewBinding> viewBindings = injection.getViewBindings(); if (viewBindings.isEmpty()) { return; } for (ViewBinding viewBinding : viewBindings) { builder.append(" target.").append(viewBinding.getName()).append(" = "); emitCastIfNeeded(builder, viewBinding.getType()); builder.append("view;\n"); } }
static void emitCastIfNeeded(StringBuilder builder, String viewType) { emitCastIfNeeded(builder, VIEW_TYPE, viewType); }
private void emitListenerBindings(StringBuilder builder, ViewInjection injection) { Map<ListenerClass, Map<ListenerMethod, ListenerBinding>> bindings = injection.getListenerBindings(); if (bindings.isEmpty()) { return; } String extraIndent = ""; // We only need to emit the null check if there are zero required bindings. boolean needsNullChecked = injection.getRequiredBindings().isEmpty(); if (needsNullChecked) { builder.append(" if (view != null) {\n"); extraIndent = " "; } for (Map.Entry<ListenerClass, Map<ListenerMethod, ListenerBinding>> e : bindings.entrySet()) { ListenerClass listener = e.getKey(); Map<ListenerMethod, ListenerBinding> methodBindings = e.getValue(); // Emit: ((OWNER_TYPE) view).SETTER_NAME( boolean needsCast = !VIEW_TYPE.equals(listener.targetType()); builder.append(extraIndent).append(" "); if (needsCast) { builder.append("((").append(listener.targetType()); if (listener.genericArguments() > 0) { builder.append('<'); for (int i = 0; i < listener.genericArguments(); i++) { if (i > 0) { builder.append(", "); } builder.append('?'); } builder.append('>'); } builder.append(") "); } builder.append("view"); if (needsCast) { builder.append(')'); } builder.append('.').append(listener.setter()).append("(\n"); // Emit: new TYPE() { builder.append(extraIndent).append(" new ").append(listener.type()).append("() {\n"); for (ListenerMethod method : getListenerMethods(listener)) { // Emit: @Override public RETURN_TYPE METHOD_NAME( builder .append(extraIndent) .append(" @Override public ") .append(method.returnType()) .append(' ') .append(method.name()) .append("(\n"); // Emit listener method arguments, each on their own line. String[] parameterTypes = method.parameters(); for (int i = 0, count = parameterTypes.length; i < count; i++) { builder .append(extraIndent) .append(" ") .append(parameterTypes[i]) .append(" p") .append(i); if (i < count - 1) { builder.append(','); } builder.append('\n'); } // Emit end of parameters, start of body. builder.append(extraIndent).append(" ) {\n"); // Set up the return statement, if needed. builder.append(extraIndent).append(" "); boolean hasReturnType = !"void".equals(method.returnType()); if (hasReturnType) { builder.append("return "); } if (methodBindings.containsKey(method)) { ListenerBinding binding = methodBindings.get(method); builder.append("target.").append(binding.getName()).append('('); List<Parameter> parameters = binding.getParameters(); String[] listenerParameters = method.parameters(); for (int i = 0, count = parameters.size(); i < count; i++) { Parameter parameter = parameters.get(i); int listenerPosition = parameter.getListenerPosition(); emitCastIfNeeded(builder, listenerParameters[listenerPosition], parameter.getType()); builder.append('p').append(listenerPosition); if (i < count - 1) { builder.append(", "); } } builder.append(");"); } else if (hasReturnType) { builder.append(method.defaultReturn()).append(';'); } builder.append('\n'); // Emit end of listener method. builder.append(extraIndent).append(" }\n"); } // Emit end of listener class body and close the setter method call. builder.append(extraIndent).append(" });\n"); } if (needsNullChecked) { builder.append(" }\n"); } }