Skip to content

Commit 729c2b0

Browse files
authored
Merge pull request #431 from eclipse/python-custom-deserializer
Python custom deserializer
2 parents 7d3cf47 + 7c413ec commit 729c2b0

File tree

5 files changed

+73
-4
lines changed

5 files changed

+73
-4
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.eclipse.steady.python.sign;
2+
3+
import java.io.IOException;
4+
5+
import org.eclipse.steady.python.sign.PythonConstructDigest.ComputedFromType;
6+
import org.eclipse.steady.shared.enums.DigestAlgorithm;
7+
8+
import com.fasterxml.jackson.core.JsonParser;
9+
import com.fasterxml.jackson.core.JsonProcessingException;
10+
import com.fasterxml.jackson.databind.DeserializationContext;
11+
import com.fasterxml.jackson.databind.JsonNode;
12+
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
13+
14+
/** PythonConstructDigestDeserializer class. */
15+
public class PythonConstructDigestDeserializer extends StdDeserializer<PythonConstructDigest> {
16+
17+
/** Constructor for PythonConstructDigestDeserializer. */
18+
public PythonConstructDigestDeserializer() {
19+
this(null);
20+
}
21+
22+
/**
23+
* Constructor for PythonConstructDigestDeserializer.
24+
*
25+
* @param t a {@link java.lang.Class} object.
26+
*/
27+
public PythonConstructDigestDeserializer(Class<PythonConstructDigest> t) {
28+
super(t);
29+
}
30+
31+
/** {@inheritDoc} */
32+
@Override
33+
public PythonConstructDigest deserialize(JsonParser p, DeserializationContext ctxt)
34+
throws IOException, JsonProcessingException {
35+
36+
final JsonNode json_root = p.getCodec().readTree(p);
37+
String computedFrom = json_root.findValue("computedFrom").asText();
38+
ComputedFromType computedFromType =
39+
ComputedFromType.valueOf(json_root.findValue("computedFromType").asText());
40+
String digest = json_root.findValue("digest").asText();
41+
DigestAlgorithm digestAlgorithm =
42+
DigestAlgorithm.fromString(json_root.findValue("digestAlgorithm").asText());
43+
44+
PythonConstructDigest pythonConstructDigest =
45+
new PythonConstructDigest(digest, digestAlgorithm);
46+
pythonConstructDigest.setComputedFrom(computedFrom);
47+
pythonConstructDigest.setComputedFromType(computedFromType);
48+
return pythonConstructDigest;
49+
}
50+
}

lang-python/src/main/java/org/eclipse/steady/python/sign/PythonConstructDigestSerializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void serialize(
5656
jgen.writeStringField("computedFrom", value.getComputedFrom());
5757
jgen.writeStringField("computedFromType", value.getComputedFromType().toString());
5858
jgen.writeStringField("digest", value.getDigest());
59-
jgen.writeStringField("digestAlgorihtm", value.getDigestAlgorithm().toString());
59+
jgen.writeStringField("digestAlgorithm", value.getDigestAlgorithm().toString());
6060
jgen.writeEndObject();
6161
}
6262
}

patch-lib-analyzer/src/main/java/org/eclipse/steady/patcheval/ByteCodeComparator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.eclipse.steady.patcheval.representation.ArtifactResult2;
3232
import org.eclipse.steady.patcheval.representation.ConstructPathAssessment2;
3333
import org.eclipse.steady.patcheval.utils.CSVHelper2;
34+
import org.eclipse.steady.python.sign.PythonConstructDigest;
35+
import org.eclipse.steady.python.sign.PythonConstructDigestDeserializer;
3436
import org.eclipse.steady.shared.enums.ProgrammingLanguage;
3537
import org.eclipse.steady.shared.json.JacksonUtil;
3638
import org.eclipse.steady.shared.json.model.LibraryId;
@@ -60,6 +62,7 @@ public ByteCodeComparator(ArtifactResult2 ar, String _b) {
6062
this.ar = ar;
6163
this.bugId = _b;
6264
custom_deserializers.put(ASTSignatureChange.class, new ASTSignatureChangeDeserializer());
65+
custom_deserializers.put(PythonConstructDigest.class, new PythonConstructDigestDeserializer());
6366
}
6467

6568
/** {@inheritDoc} */

patch-lib-analyzer/src/main/java/org/eclipse/steady/patcheval/LibraryAnalyzerThread2.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.eclipse.steady.patcheval.representation.LidResult2;
4242
import org.eclipse.steady.patcheval.representation.OverallConstructChange;
4343
import org.eclipse.steady.python.sign.PythonConstructDigest;
44+
import org.eclipse.steady.python.sign.PythonConstructDigestDeserializer;
4445
import org.eclipse.steady.shared.enums.ConstructChangeType;
4546
import org.eclipse.steady.shared.enums.ProgrammingLanguage;
4647
import org.eclipse.steady.shared.json.JacksonUtil;
@@ -82,6 +83,7 @@ public LibraryAnalyzerThread2(
8283
this.tid = _id;
8384

8485
custom_deserializers.put(ASTSignatureChange.class, new ASTSignatureChangeDeserializer());
86+
custom_deserializers.put(PythonConstructDigest.class, new PythonConstructDigestDeserializer());
8587

8688
this.singleMethsConsCC = methsConsMOD;
8789
this.addedDelMethsConsCC = methsConsAD;
@@ -303,14 +305,21 @@ public List<ConstructPathLibResult2> call() throws Exception {
303305
ProgrammingLanguage.PY);
304306
PythonConstructDigest pythonConstructDigest =
305307
(PythonConstructDigest)
306-
JacksonUtil.asObject(ast_lid, PythonConstructDigest.class);
308+
JacksonUtil.asObject(
309+
ast_lid, custom_deserializers, PythonConstructDigest.class);
307310
if (pythonConstructDigest != null) {
308311
PythonConstructDigest vulnConstructDigest =
309312
(PythonConstructDigest)
310-
JacksonUtil.asObject(mcCC.getBuggyBody(), PythonConstructDigest.class);
313+
JacksonUtil.asObject(
314+
mcCC.getBuggyBody(),
315+
custom_deserializers,
316+
PythonConstructDigest.class);
311317
PythonConstructDigest fixedConstructDigest =
312318
(PythonConstructDigest)
313-
JacksonUtil.asObject(mcCC.getFixedBody(), PythonConstructDigest.class);
319+
JacksonUtil.asObject(
320+
mcCC.getFixedBody(),
321+
custom_deserializers,
322+
PythonConstructDigest.class);
314323
if (pythonConstructDigest.getDigest() != null
315324
&& vulnConstructDigest.getDigest() != null
316325
&& pythonConstructDigest

shared/src/main/java/org/eclipse/steady/shared/enums/DigestAlgorithm.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,11 @@ public String toString() {
4949
else if (this.value == 30) return "MD5";
5050
else throw new IllegalArgumentException("[" + this.value + "] is not a valid digest algorithm");
5151
}
52+
53+
public static DigestAlgorithm fromString(String _s) {
54+
if (_s.equals("SHA1")) return DigestAlgorithm.SHA1;
55+
if (_s.equals("SHA256")) return DigestAlgorithm.SHA256;
56+
if (_s.equals("MD5")) return DigestAlgorithm.MD5;
57+
else throw new IllegalArgumentException("[" + _s + "] is not a valid digest algorithm");
58+
}
5259
}

0 commit comments

Comments
 (0)