|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.gradle.js2p; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.databind.JsonNode; |
| 9 | +import com.sun.codemodel.ClassType; |
| 10 | +import com.sun.codemodel.JBlock; |
| 11 | +import com.sun.codemodel.JCodeModel; |
| 12 | +import com.sun.codemodel.JDefinedClass; |
| 13 | +import com.sun.codemodel.JExpr; |
| 14 | +import com.sun.codemodel.JFieldVar; |
| 15 | +import com.sun.codemodel.JMethod; |
| 16 | +import com.sun.codemodel.JMod; |
| 17 | +import com.sun.codemodel.JPackage; |
| 18 | +import com.sun.codemodel.JType; |
| 19 | +import com.sun.codemodel.JVar; |
| 20 | +import javax.annotation.Nullable; |
| 21 | +import org.jsonschema2pojo.Schema; |
| 22 | +import org.jsonschema2pojo.rules.ObjectRule; |
| 23 | +import org.jsonschema2pojo.rules.RuleFactory; |
| 24 | +import org.jsonschema2pojo.util.ParcelableHelper; |
| 25 | +import org.jsonschema2pojo.util.ReflectionHelper; |
| 26 | + |
| 27 | +/** |
| 28 | + * An {@link ObjectRule} that replaces jsonschema2pojo's generated {@code toString}/{@code |
| 29 | + * equals}/{@code hashCode} with implementations that mirror AutoValue's style. |
| 30 | + */ |
| 31 | +public class OtelObjectRule extends ObjectRule { |
| 32 | + |
| 33 | + public OtelObjectRule( |
| 34 | + RuleFactory ruleFactory, |
| 35 | + ParcelableHelper parcelableHelper, |
| 36 | + ReflectionHelper reflectionHelper) { |
| 37 | + super(ruleFactory, parcelableHelper, reflectionHelper); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public JType apply( |
| 42 | + String nodeName, JsonNode node, JsonNode parent, JPackage pkg, Schema schema) { |
| 43 | + JType type = super.apply(nodeName, node, parent, pkg, schema); |
| 44 | + if (type instanceof JDefinedClass |
| 45 | + && ((JDefinedClass) type).getClassType() == ClassType.CLASS) { |
| 46 | + addValueMethods((JDefinedClass) type); |
| 47 | + } |
| 48 | + return type; |
| 49 | + } |
| 50 | + |
| 51 | + private static void addValueMethods(JDefinedClass clazz) { |
| 52 | + JCodeModel model = clazz.owner(); |
| 53 | + |
| 54 | + addToString(clazz, model); |
| 55 | + addHashCode(clazz, model); |
| 56 | + addEquals(clazz, model); |
| 57 | + } |
| 58 | + |
| 59 | + // toString: ClassName{field1=value1, field2=value2} |
| 60 | + private static void addToString(JDefinedClass clazz, JCodeModel model) { |
| 61 | + JMethod toString = clazz.method(JMod.PUBLIC, model.ref(String.class), "toString"); |
| 62 | + toString.annotate(Override.class); |
| 63 | + |
| 64 | + StringBuilder expr = new StringBuilder("return \"").append(clazz.name()).append("{\""); |
| 65 | + boolean first = true; |
| 66 | + for (JFieldVar field : clazz.fields().values()) { |
| 67 | + if (isStatic(field)) { |
| 68 | + continue; |
| 69 | + } |
| 70 | + expr.append(" + \"") |
| 71 | + .append(first ? "" : ", ") |
| 72 | + .append(field.name()) |
| 73 | + .append("=\" + ") |
| 74 | + .append(field.name()); |
| 75 | + first = false; |
| 76 | + } |
| 77 | + expr.append(" + \"}\";"); |
| 78 | + toString.body().directStatement(expr.toString()); |
| 79 | + } |
| 80 | + |
| 81 | + // equals: instanceof + cast + (this.f == null ? that.f == null : this.f.equals(that.f)) && ... |
| 82 | + private static void addEquals(JDefinedClass clazz, JCodeModel model) { |
| 83 | + JMethod equals = clazz.method(JMod.PUBLIC, model.BOOLEAN, "equals"); |
| 84 | + equals.annotate(Override.class); |
| 85 | + JVar other = equals.param(model.ref(Object.class), "o"); |
| 86 | + other.annotate(Nullable.class); |
| 87 | + JBlock body = equals.body(); |
| 88 | + |
| 89 | + body._if(other.eq(JExpr._this()))._then()._return(JExpr.TRUE); |
| 90 | + |
| 91 | + JBlock matched = body._if(other._instanceof(clazz))._then(); |
| 92 | + matched.directStatement(clazz.name() + " that = (" + clazz.name() + ") o;"); |
| 93 | + |
| 94 | + StringBuilder comparison = new StringBuilder("return "); |
| 95 | + boolean first = true; |
| 96 | + for (JFieldVar field : clazz.fields().values()) { |
| 97 | + if (isStatic(field)) { |
| 98 | + continue; |
| 99 | + } |
| 100 | + String name = field.name(); |
| 101 | + comparison |
| 102 | + .append(first ? "" : " && ") |
| 103 | + .append("(this.").append(name).append(" == null ? that.").append(name) |
| 104 | + .append(" == null : this.").append(name).append(".equals(that.").append(name) |
| 105 | + .append("))"); |
| 106 | + first = false; |
| 107 | + } |
| 108 | + matched.directStatement(first ? "return true;" : comparison.append(";").toString()); |
| 109 | + |
| 110 | + body._return(JExpr.FALSE); |
| 111 | + } |
| 112 | + |
| 113 | + // hashCode: h = 1; h *= 1000003; h ^= (f == null ? 0 : f.hashCode()); ... |
| 114 | + private static void addHashCode(JDefinedClass clazz, JCodeModel model) { |
| 115 | + JMethod hashCode = clazz.method(JMod.PUBLIC, model.INT, "hashCode"); |
| 116 | + hashCode.annotate(Override.class); |
| 117 | + JBlock body = hashCode.body(); |
| 118 | + JVar h = body.decl(model.INT, "h", JExpr.lit(1)); |
| 119 | + |
| 120 | + for (JFieldVar field : clazz.fields().values()) { |
| 121 | + if (isStatic(field)) { |
| 122 | + continue; |
| 123 | + } |
| 124 | + String name = field.name(); |
| 125 | + body.directStatement("h *= 1000003;"); |
| 126 | + body.directStatement("h ^= (this." + name + " == null) ? 0 : this." + name + ".hashCode();"); |
| 127 | + } |
| 128 | + body._return(h); |
| 129 | + } |
| 130 | + |
| 131 | + private static boolean isStatic(JFieldVar field) { |
| 132 | + return (field.mods().getValue() & JMod.STATIC) == JMod.STATIC; |
| 133 | + } |
| 134 | +} |
0 commit comments