@Override public int hashCode() { return toString().hashCode(); }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null) return false; if (getClass() != o.getClass()) return false; return toString().equals(o.toString()); }
private String literal(String raw) { if (raw.matches("\\w+")) return raw; return CodeBlock.builder() .add("$S", raw) .build() .toString(); }
@Override public String toString() { return CodeBlock.builder().add(format, args).build().toString(); } }
, ClassName.get("com.api", "Api") , e.getSimpleName().toString() , blockBuilder.build().toString() , ClassName.get("com.base.util.helper", "RxSchedulers")); tb.addMethod(methodBuilder.build());
@Override public void exitFieldAccess(FieldAccessExpression expr) { final CodeBlock object = codeSnippet.get(expr.object()); final CodeBlock field = codeSnippet.get(expr.field()); final Object objectRef = blockOrMissing(object, expr.object()); final Expression o = expr.object(); final PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(o.getType()); final ImmutableMap<String, PropertyDescriptor> propertyByName = Maps.uniqueIndex(Iterators.forArray(propertyDescriptors), FeatureDescriptor::getName); final String fieldName = field.toString(); final CodeBlock block; if (propertyByName.containsKey(fieldName)) { // we have the property, resolve the read method name for it final PropertyDescriptor descriptor = propertyByName.get(fieldName); final String methodName = descriptor.getReadMethod().getName(); block = CodeBlock.of("$L.$L()", objectRef, methodName); } else if (o instanceof Map) { // there wasn't any property, but the object is a Map, translate into .get() call block = CodeBlock.of("$L.get($S)", objectRef, field); } else { // this is basically an error, because we expected either a property to match or a map lookup. log.warn("Unable to determine field accessor for property {}", field); block = CodeBlock.of("null"); } codeSnippet.put(expr, block); }
.superclass(ParameterizedTypeName.get(ClassName.get(Binding.class), injectedClassName)) .addJavadoc("$L", bindingTypeDocs(injectableType(type.asType()), isAbstract, injectMembers, dependent).toString());
private void emitGetTargetScope(TypeSpec.Builder builder) { CodeBlock.Builder getParentScopeCodeBlockBuilder = getParentScopeCodeBlockBuilder(); MethodSpec.Builder getScopeBuilder = MethodSpec.methodBuilder("getTargetScope") .addAnnotation(Override.class) .addModifiers(Modifier.PUBLIC) .addParameter(ClassName.get(Scope.class), "scope") .returns(ClassName.get(Scope.class)) .addStatement("return scope$L", getParentScopeCodeBlockBuilder.build().toString()); builder.addMethod(getScopeBuilder.build()); }
@Test public void testStringArrayInit() { String expected = "java.lang.String[] names = new java.lang.String[] {\"1\",\"2\"}"; String[] testArr = new String[] { "1", "2" }; String literal = "{\"" + String.join("\",\"", testArr) + "\"}"; ArrayTypeName stringArray = ArrayTypeName.of(String.class); CodeBlock block = CodeBlock.builder().add("$1T names = new $1T $2L", stringArray, literal).build(); Assert.assertEquals(expected, block.toString()); }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null) return false; if (getClass() != o.getClass()) return false; return toString().equals(o.toString()); }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null) return false; if (getClass() != o.getClass()) return false; return toString().equals(o.toString()); }
private void emitGetTargetScope(TypeSpec.Builder builder) { CodeBlock.Builder getParentScopeCodeBlockBuilder = getParentScopeCodeBlockBuilder(); MethodSpec.Builder getScopeBuilder = MethodSpec.methodBuilder("getTargetScope") .addAnnotation(Override.class) .addModifiers(Modifier.PUBLIC) .addParameter(ClassName.get(Scope.class), "scope") .returns(ClassName.get(Scope.class)) .addStatement("return scope$L", getParentScopeCodeBlockBuilder.build().toString()); builder.addMethod(getScopeBuilder.build()); }
private void emitGetTargetScope(TypeSpec.Builder builder) { CodeBlock.Builder getParentScopeCodeBlockBuilder = getParentScopeCodeBlockBuilder(); MethodSpec.Builder getScopeBuilder = MethodSpec.methodBuilder("getTargetScope") .addAnnotation(Override.class) .addModifiers(Modifier.PUBLIC) .addParameter(ClassName.get(Scope.class), "scope") .returns(ClassName.get(Scope.class)) .addStatement("return scope$L", getParentScopeCodeBlockBuilder.build().toString()); builder.addMethod(getScopeBuilder.build()); }
@Nullable @Override public String apply(@Nullable FieldSpec fieldSpec) { if ( fieldSpec.type.isPrimitive()) { return CodeBlock.builder().add("(this.$L == other.$L)", fieldSpec.name, fieldSpec.name).build().toString(); } else { return CodeBlock.builder().add("$T.equals(this.$L, other.$L)", Objects.class, fieldSpec.name, fieldSpec.name).build().toString(); } } }).join(Joiner.on(" && "));
@Nullable @Override public String apply(@Nullable FieldSpec fieldSpec) { if ( fieldSpec.type.isPrimitive()) { return CodeBlock.builder().add("(this.$L == other.$L)", fieldSpec.name, fieldSpec.name).build().toString(); } else { return CodeBlock.builder().add("$T.equals(this.$L, other.$L)", Objects.class, fieldSpec.name, fieldSpec.name).build().toString(); } } }).join(Joiner.on(" && "));
private String buildCastCode(Element element) { if (typeUtils.typeExchange(element) == TypeKind.SERIALIZABLE.ordinal()) { return CodeBlock.builder().add("($T) ", ClassName.get(element.asType())).build().toString(); } return ""; }
@Test public void createUnmodifiableStatementShouldCopyListDefensively() throws Exception { // setup final TypeElement element = elements.getTypeElement(List.class.getCanonicalName()); final TypeMirror mirror = element.asType(); // exercise final CodeBlock actual = CodeBlocks.createUnmodifiableStatement(mirror, "value"); // verify assertThat(actual.toString()) .isEqualTo("new java.util.ArrayList<>(value)"); }
@Test public void createUnmodifiableStatementShouldCopySetDefensively() throws Exception { // setup final TypeElement element = elements.getTypeElement(Set.class.getCanonicalName()); final TypeMirror mirror = element.asType(); // exercise final CodeBlock actual = CodeBlocks.createUnmodifiableStatement(mirror, "value"); // verify assertThat(actual.toString()) .isEqualTo("new java.util.HashSet<>(value)"); }
@Test public void createUnmodifiableStatementShouldCopyMapDefensively() throws Exception { // setup final TypeElement element = elements.getTypeElement(Map.class.getCanonicalName()); final TypeMirror mirror = element.asType(); // exercise final CodeBlock actual = CodeBlocks.createUnmodifiableStatement(mirror, "value"); // verify assertThat(actual.toString()) .isEqualTo("new java.util.HashMap<>(value)"); }
@Override public Equality isEqualTo(Object that) { return Tester.of(InsertableRowExpression.class) .add("expression", o -> o.get().toString()) .test(this, that); }