private void emitViewInjection(StringBuilder builder, ViewInjection injection) { builder.append(" view = "); List<Binding> requiredBindings = injection.getRequiredBindings(); if (requiredBindings.isEmpty()) { builder.append("finder.findOptionalView(source, ").append(injection.getId()).append(");\n"); } else { builder.append("finder.findRequiredView(source, ").append(injection.getId()).append(", \""); emitHumanDescription(builder, requiredBindings); builder.append("\");\n"); } emitViewBindings(builder, injection); emitListenerBindings(builder, injection); }
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"); } }