Skip to content

Commit

Permalink
ctf.core: Add initialization to GSON created nodes
Browse files Browse the repository at this point in the history
Nodes created using GSON do not use constructors and
therefore ignore null type annotations and do not
initialize children nodes. This is fixed by adding
an initialize method to all CTFJsonMetadataNodes.

Change-Id: I13b2741e77366d2f8fab19395f64d8a305c55609
Signed-off-by: Sehr Moosabhoy <sehr.moosabhoy@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/c/tracecompass/org.eclipse.tracecompass/+/203054
Tested-by: Trace Compass Bot <tracecompass-bot@eclipse.org>
Tested-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-by: Marco Miller <marco.miller@ericsson.com>
  • Loading branch information
Sehr Moosabhoy authored and marco-miller committed Jul 24, 2023
1 parent 16f9917 commit a2cd56e
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ctf/org.eclipse.tracecompass.ctf.core/META-INF/MANIFEST.MF
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %Bundle-Name
Bundle-Vendor: %Bundle-Vendor
Bundle-Version: 4.2.0.qualifier
Bundle-Version: 4.3.0.qualifier
Bundle-Localization: plugin
Bundle-SymbolicName: org.eclipse.tracecompass.ctf.core;singleton:=true
Bundle-Activator: org.eclipse.tracecompass.internal.ctf.core.Activator
Expand Down
Expand Up @@ -922,7 +922,7 @@ public void setEnvironment(@NonNull Map<String, String> parseEnvironment) {
* @return true if the packet type matches, false otherwise
* @throws CTFException
* If the file is not found.
* @since 4.2
* @since 4.3
*/
public static boolean startsWithRecordSeparator(File file, int recordSeparator)
throws CTFException {
Expand Down
Expand Up @@ -204,7 +204,7 @@ public void parseFile() throws CTFException {
*
* @throws CTFException
* if there was an issue parsing the metadata
* @since 4.2
* @since 4.3
*
*/
public void parseJsonFile() throws CTFException {
Expand Down Expand Up @@ -262,6 +262,8 @@ private static ICTFMetadataNode parseJsonToTree(String json) throws CTFException
fragment = gson.fromJson(jsonBlocks[i], JsonFieldClassAliasMetadataNode.class);
}

((CTFJsonMetadataNode) fragment).initialize();

root.addChild(fragment);
fragment.setParent(root);
}
Expand All @@ -275,7 +277,7 @@ private static ICTFMetadataNode parseJsonToTree(String json) throws CTFException
* @throws CTFException
* throws exception if file is invalid
*
* @since 4.2
* @since 4.3
*/
public void checkCTFVersion() throws CTFException {
File metadataFile = new File(getMetadataPath());
Expand Down
Expand Up @@ -19,7 +19,9 @@
import java.util.Map;
import java.util.Objects;

import org.eclipse.tracecompass.ctf.core.CTFException;
import org.eclipse.tracecompass.internal.ctf.core.event.types.ICTFMetadataNode;

import com.google.gson.annotations.SerializedName;

/**
Expand All @@ -30,14 +32,14 @@
public class CTFJsonMetadataNode implements ICTFMetadataNode {
//deserialized attributes from Json
@SerializedName("type")
private final String fType;
private String fType;
@SerializedName("user-attributes")
private Map<String, String> fUserAttributes;

//other attributes added for convenience
private ICTFMetadataNode fParent;
private final Map<String, ICTFMetadataNode> fChildren;
private final ArrayList<ICTFMetadataNode> fChildrenList;
private Map<String, ICTFMetadataNode> fChildren;
private ArrayList<ICTFMetadataNode> fChildrenList;
private final String fValue;


Expand Down Expand Up @@ -127,4 +129,42 @@ public Map<String, String> getUserAttributes() {
return fUserAttributes;
}

/**
* Set the type of the node, specifically used for nodes that do not have a
* type when created such as JsonStructureFieldMemberMetadataNode
*
* @param type
* the new type of the node
*/
public void setType(String type) {
fType = Objects.requireNonNull(type);
}

/**
* Set the childrenList of the node
*
* @param childrenList
* the children of the node
*/
public void setChildrenList(List<ICTFMetadataNode> childrenList) {
fChildrenList = (ArrayList<ICTFMetadataNode>) childrenList;
}

/**
* Helper method to initialize fields that aren't set by gson library
*
* @throws CTFException
* if type is null but node is created with gson
*/
public void initialize() throws CTFException {
if (fChildren == null) {
fChildren = new HashMap<>();
}
if (fChildrenList == null) {
fChildrenList = new ArrayList<>();
}
if (fType == null) {
throw new CTFException("type of node cannot be null"); //$NON-NLS-1$
}
}
}
Expand Up @@ -13,7 +13,9 @@
*******************************************************************************/
package org.eclipse.tracecompass.internal.ctf.core.event.metadata;

import org.eclipse.tracecompass.ctf.core.CTFException;
import org.eclipse.tracecompass.internal.ctf.core.event.types.ICTFMetadataNode;

import com.google.gson.annotations.SerializedName;

/**
Expand Down Expand Up @@ -116,4 +118,17 @@ public JsonStructureFieldMetadataNode getEventRecordCommonContextClass() {
return fEventRecordCommonContextClass;
}

@Override
public void initialize() throws CTFException {
super.initialize();
if (fPacketContextFieldClass != null) {
fPacketContextFieldClass.initialize();
}
if (fEventRecordCommonContextClass != null) {
fEventRecordCommonContextClass.initialize();
}
if (fEventRecordHeaderClass != null) {
fEventRecordHeaderClass.initialize();
}
}
}
Expand Up @@ -13,7 +13,9 @@
*******************************************************************************/
package org.eclipse.tracecompass.internal.ctf.core.event.metadata;

import org.eclipse.tracecompass.ctf.core.CTFException;
import org.eclipse.tracecompass.internal.ctf.core.event.types.ICTFMetadataNode;

import com.google.gson.annotations.SerializedName;

/**
Expand Down Expand Up @@ -105,4 +107,14 @@ public JsonStructureFieldMetadataNode getPayloadFieldClass() {
return fPayloadFieldClass;
}

@Override
public void initialize() throws CTFException {
super.initialize();
if (fSpecificContextClass != null) {
fSpecificContextClass.initialize();
}
if (fPayloadFieldClass != null) {
fPayloadFieldClass.initialize();
}
}
}
Expand Up @@ -13,8 +13,11 @@
*******************************************************************************/
package org.eclipse.tracecompass.internal.ctf.core.event.metadata;

import org.eclipse.tracecompass.ctf.core.CTFException;
import org.eclipse.tracecompass.internal.ctf.core.event.types.ICTFMetadataNode;
import org.eclipse.tracecompass.internal.ctf.core.utils.JsonMetadataStrings;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.annotations.SerializedName;

Expand All @@ -29,7 +32,7 @@ public class JsonStructureFieldMemberMetadataNode extends CTFJsonMetadataNode {
@SerializedName("name")
private final String fName;
@SerializedName("field-class")
private final JsonObject fFieldClass;
private final JsonElement fFieldClass;

/**
* Constructor for a JsonStructureFieldMemberMetadataNode
Expand All @@ -47,8 +50,8 @@ public class JsonStructureFieldMemberMetadataNode extends CTFJsonMetadataNode {
*/
public JsonStructureFieldMemberMetadataNode(ICTFMetadataNode parent, String type, String value, String name, JsonObject fieldClass) {
super(parent, type, value);
this.fName = name;
this.fFieldClass = fieldClass;
fName = name;
fFieldClass = fieldClass;
}

/**
Expand All @@ -65,8 +68,17 @@ public String getName() {
*
* @return the field class
*/
public JsonObject getFieldClass() {
public JsonElement getFieldClass() {
return fFieldClass;
}

@Override
public void initialize() throws CTFException {
if (fFieldClass.isJsonObject() && fFieldClass.getAsJsonObject().has(JsonMetadataStrings.TYPE)) {
setType(fFieldClass.getAsJsonObject().get(JsonMetadataStrings.TYPE).getAsString());
} else {
setType(JsonMetadataStrings.ALIAS);
}
super.initialize();
}
}
Expand Up @@ -13,7 +13,11 @@
*******************************************************************************/
package org.eclipse.tracecompass.internal.ctf.core.event.metadata;

import java.util.List;

import org.eclipse.tracecompass.ctf.core.CTFException;
import org.eclipse.tracecompass.internal.ctf.core.event.types.ICTFMetadataNode;

import com.google.gson.annotations.SerializedName;

/**
Expand All @@ -25,7 +29,7 @@
public class JsonStructureFieldMetadataNode extends CTFJsonMetadataNode {

@SerializedName("member-classes")
private JsonStructureFieldMemberMetadataNode[] fMemberClasses;
private List<JsonStructureFieldMemberMetadataNode> fMemberClasses;
@SerializedName("minimum-alignment")
private int fMinimumAlignment;

Expand All @@ -48,7 +52,7 @@ public JsonStructureFieldMetadataNode(ICTFMetadataNode parent, String type, Stri
*
* @return the member classes
*/
public JsonStructureFieldMemberMetadataNode[] getMemberClasses() {
public List<JsonStructureFieldMemberMetadataNode> getMemberClasses() {
return fMemberClasses;
}

Expand All @@ -61,4 +65,15 @@ public int getMinimumAlignment() {
return fMinimumAlignment;
}

@Override
public void initialize() throws CTFException {
super.initialize();
if (fMemberClasses != null) {
for (JsonStructureFieldMemberMetadataNode member : fMemberClasses) {
member.initialize();
addChild(member);
member.setParent(this);
}
}
}
}
Expand Up @@ -13,8 +13,10 @@
*******************************************************************************/
package org.eclipse.tracecompass.internal.ctf.core.event.metadata;

import org.eclipse.tracecompass.ctf.core.CTFException;
import org.eclipse.tracecompass.internal.ctf.core.event.types.ICTFMetadataNode;
import org.json.JSONObject;

import com.google.gson.annotations.SerializedName;

/**
Expand Down Expand Up @@ -64,4 +66,22 @@ public JSONObject getEnvironment() {
return fEnvironment;
}

/**
* Get the packet header of the trace
*
* @return the packet header
*/
public JsonStructureFieldMetadataNode getPacketHeader() {
return fPacketHeader;
}

@Override
public void initialize() throws CTFException {
super.initialize();
if (fPacketHeader != null) {
fPacketHeader.initialize();
addChild(fPacketHeader);
fPacketHeader.setParent(this);
}
}
}
Expand Up @@ -56,4 +56,14 @@ private JsonMetadataStrings() {
* Type string for a CTF2 event record class fragment
*/
public static final String FRAGMENT_EVENT_RECORD = "event-record-class"; //$NON-NLS-1$

/**
* Field string for the type of a CTF2 node
*/
public static final String TYPE = "type"; //$NON-NLS-1$

/**
* Type string for a field class that points to an alias
*/
public static final String ALIAS = "alias"; //$NON-NLS-1$
}

0 comments on commit a2cd56e

Please sign in to comment.