@Test public void testProcessToString() { MethodSpec.Builder toStringBuilder = MethodSpec.methodBuilder("toString") .addModifiers(Modifier.PUBLIC) .returns(String.class); List<String> fields = Arrays.asList("one", "two"); MetaInfo metaInfo = new MetaInfo("Test", "com.flipkart.lyrics"); DefaultStyle style = new DefaultStyle(); style.processToString(toStringBuilder, fields, metaInfo); MethodSpec toString = toStringBuilder.build(); assertEquals("toString", toString.name); assertEquals(0, toString.parameters.size()); }
@Override public void processToString(MethodSpec.Builder toStringBuilder, List<String> nonStaticFields, MetaInfo metaInfo) { toStringBuilder.addStatement("final StringBuilder sb = new StringBuilder(\"$L{\")", metaInfo.getClassName()); if (nonStaticFields.size() > 0) { String firstField = nonStaticFields.get(0); toStringBuilder.addStatement("sb.append(\"$L=\").append($L)", firstField, firstField); if (nonStaticFields.size() > 1) { for (String field : nonStaticFields.subList(1, nonStaticFields.size())) { toStringBuilder.addStatement("sb.append(\", $L=\").append($L);", field, field); } } } toStringBuilder.addStatement("sb.append('}')"); toStringBuilder.addStatement("return sb.toString()"); }
DefaultStyle extends ObjectMethodsStyle { @Override public void processToString(MethodSpec.Builder toStringBuilder, List<String> nonStaticFields, MetaInfo metaInfo) { toStringBuilder.addStatement("final StringBuilder sb = new StringBuilder(\"$L{\")", metaInfo.getClassName()); if (nonStaticFields.size() > 0) { String firstField = nonStaticFields.get(0); toStringBuilder.addStatement("sb.append(\"$L=\").append($L)", firstField, firstField); if (nonStaticFields.size() > 1) { for (String field : nonStaticFields.subList(1, nonStaticFields.size())) { toStringBuilder.addStatement("sb.append(\", $L=\").append($L);", field, field); } } } toStringBuilder.addStatement("sb.append('}')"); toStringBuilder.addStatement("return sb.toString()"); } }
DefaultStyle extends ObjectMethodsStyle { @Override public void processToString(MethodSpec.Builder toStringBuilder, List<String> nonStaticFields, MetaInfo metaInfo) { toStringBuilder.addStatement("final StringBuilder sb = new StringBuilder(\"$L{\")", metaInfo.getClassName()); if (nonStaticFields.size() > 0) { String firstField = nonStaticFields.get(0); toStringBuilder.addStatement("sb.append(\"$L=\").append($L)", firstField, firstField); if (nonStaticFields.size() > 1) { for (String field : nonStaticFields.subList(1, nonStaticFields.size())) { toStringBuilder.addStatement("sb.append(\", $L=\").append($L);", field, field); } } } toStringBuilder.addStatement("sb.append('}')"); toStringBuilder.addStatement("return sb.toString()"); }  }
DefaultStyle extends ObjectMethodsStyle { @Override public void processToString(MethodSpec.Builder toStringBuilder, List<String> nonStaticFields, MetaInfo metaInfo) { toStringBuilder.addStatement("final StringBuilder sb = new StringBuilder(\"$L{\")", metaInfo.getClassName()); if (nonStaticFields.size() > 0) { String firstField = nonStaticFields.get(0); toStringBuilder.addStatement("sb.append(\"$L=\").append($L)", firstField, firstField); if (nonStaticFields.size() > 1) { for (String field : nonStaticFields.subList(1, nonStaticFields.size())) { toStringBuilder.addStatement("sb.append(\", $L=\").append($L);", field, field); } } } toStringBuilder.addStatement("sb.append('}')"); toStringBuilder.addStatement("return sb.toString()"); }  @Override void processToString(MethodSpec.Builder toStringBuilder, List<String> nonStaticFields, MetaInfo metaInfo); @Override void processEqualsAndHashCode(MethodSpec.Builder equalsBuilder, MethodSpec.Builder hashCodeBuilder, List<String> nonStaticFields, MetaInfo metaInfo, boolean testSuperEquality); }
DefaultStyle extends ObjectMethodsStyle { @Override public void processToString(MethodSpec.Builder toStringBuilder, List<String> nonStaticFields, MetaInfo metaInfo) { toStringBuilder.addStatement("final StringBuilder sb = new StringBuilder(\"$L{\")", metaInfo.getClassName()); if (nonStaticFields.size() > 0) { String firstField = nonStaticFields.get(0); toStringBuilder.addStatement("sb.append(\"$L=\").append($L)", firstField, firstField); if (nonStaticFields.size() > 1) { for (String field : nonStaticFields.subList(1, nonStaticFields.size())) { toStringBuilder.addStatement("sb.append(\", $L=\").append($L);", field, field); } } } toStringBuilder.addStatement("sb.append('}')"); toStringBuilder.addStatement("return sb.toString()"); }  @Override void processToString(MethodSpec.Builder toStringBuilder, List<String> nonStaticFields, MetaInfo metaInfo); @Override void processEqualsAndHashCode(MethodSpec.Builder equalsBuilder, MethodSpec.Builder hashCodeBuilder, List<String> nonStaticFields, MetaInfo metaInfo, boolean testSuperEquality);  }