Make serializable some classes that are going to participate in remote build strategy

This commit is contained in:
Alexey Andreev 2017-03-13 23:11:51 +03:00
parent 6b31e13cbb
commit 56398f58f9
15 changed files with 142 additions and 90 deletions

View File

@ -15,58 +15,98 @@
*/
package org.teavm.callgraph;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.*;
import org.teavm.model.FieldReference;
import org.teavm.model.MethodReference;
/**
*
* @author Alexey Andreev
*/
public class DefaultCallGraph implements CallGraph {
private Map<MethodReference, DefaultCallGraphNode> nodes = new HashMap<>();
private Map<FieldReference, Set<DefaultFieldAccessSite>> fieldAccessSites = new HashMap<>();
private Map<String, Set<DefaultClassAccessSite>> classAccessSites = new HashMap<>();
public class DefaultCallGraph implements CallGraph, Serializable {
private transient Map<MethodReference, DefaultCallGraphNode> nodes = new HashMap<>();
private List<Map.Entry<MethodReference, DefaultCallGraphNode>> nodeList;
private transient Map<FieldReference, Set<DefaultFieldAccessSite>> fieldAccessSites = new HashMap<>();
private List<Map.Entry<FieldReference, DefaultFieldAccessSite>> fieldAccessSiteList;
private transient Map<String, Set<DefaultClassAccessSite>> classAccessSites = new HashMap<>();
private List<Map.Entry<String, DefaultClassAccessSite>> classAccessSiteList;
@Override
public DefaultCallGraphNode getNode(MethodReference method) {
DefaultCallGraphNode node = nodes.get(method);
if (node == null) {
node = new DefaultCallGraphNode(this, method);
nodes.put(method, node);
}
return nodes.get(method);
ensureDeserialized();
return nodes.computeIfAbsent(method, k -> new DefaultCallGraphNode(this, method));
}
@Override
public Collection<DefaultFieldAccessSite> getFieldAccess(FieldReference reference) {
ensureDeserialized();
Set<DefaultFieldAccessSite> resultSet = fieldAccessSites.get(reference);
return resultSet != null ? Collections.unmodifiableSet(resultSet)
: Collections.<DefaultFieldAccessSite>emptySet();
return resultSet != null ? Collections.unmodifiableSet(resultSet) : Collections.emptySet();
}
void addFieldAccess(DefaultFieldAccessSite accessSite) {
Set<DefaultFieldAccessSite> sites = fieldAccessSites.get(accessSite.getField());
if (sites == null) {
sites = new HashSet<>();
fieldAccessSites.put(accessSite.getField(), sites);
}
sites.add(accessSite);
ensureDeserialized();
fieldAccessSites.computeIfAbsent(accessSite.getField(), k -> new HashSet<>()).add(accessSite);
}
@Override
public Collection<DefaultClassAccessSite> getClassAccess(String className) {
ensureDeserialized();
Set<DefaultClassAccessSite> resultSet = classAccessSites.get(className);
return resultSet != null ? Collections.unmodifiableSet(resultSet)
: Collections.<DefaultClassAccessSite>emptySet();
return resultSet != null ? Collections.unmodifiableSet(resultSet) : Collections.emptySet();
}
void addClassAccess(DefaultClassAccessSite accessSite) {
Set<DefaultClassAccessSite> sites = classAccessSites.get(accessSite.getClassName());
if (sites == null) {
sites = new HashSet<>();
classAccessSites.put(accessSite.getClassName(), sites);
ensureDeserialized();
classAccessSites.computeIfAbsent(accessSite.getClassName(), k -> new HashSet<>()).add(accessSite);
}
private void ensureDeserialized() {
if (nodes != null) {
return;
}
sites.add(accessSite);
nodes = new HashMap<>();
for (Map.Entry<MethodReference, DefaultCallGraphNode> entry : nodeList) {
nodes.put(entry.getKey(), entry.getValue());
}
nodeList = null;
fieldAccessSites = new HashMap<>();
for (Map.Entry<FieldReference, DefaultFieldAccessSite> entry : fieldAccessSiteList) {
fieldAccessSites.computeIfAbsent(entry.getKey(), k -> new HashSet<>()).add(entry.getValue());
}
fieldAccessSiteList = null;
classAccessSites = new HashMap<>();
for (Map.Entry<String, DefaultClassAccessSite> entry : classAccessSiteList) {
classAccessSites.computeIfAbsent(entry.getKey(), k -> new HashSet<>()).add(entry.getValue());
}
classAccessSiteList = null;
}
private void writeObject(ObjectOutputStream out) throws IOException {
ensureDeserialized();
nodeList = new ArrayList<>(nodes.entrySet());
fieldAccessSiteList = new ArrayList<>();
for (Map.Entry<FieldReference, Set<DefaultFieldAccessSite>> entry : fieldAccessSites.entrySet()) {
for (DefaultFieldAccessSite site : entry.getValue()) {
fieldAccessSiteList.add(new AbstractMap.SimpleEntry<>(entry.getKey(), site));
}
}
classAccessSiteList = new ArrayList<>();
for (Map.Entry<String, Set<DefaultClassAccessSite>> entry : classAccessSites.entrySet()) {
for (DefaultClassAccessSite site : entry.getValue()) {
classAccessSiteList.add(new AbstractMap.SimpleEntry<>(entry.getKey(), site));
}
}
out.defaultWriteObject();
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
}
}

View File

@ -15,6 +15,10 @@
*/
package org.teavm.callgraph;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@ -25,20 +29,19 @@ import org.teavm.model.FieldReference;
import org.teavm.model.MethodReference;
import org.teavm.model.TextLocation;
/**
*
* @author Alexey Andreev
*/
public class DefaultCallGraphNode implements CallGraphNode {
public class DefaultCallGraphNode implements CallGraphNode, Serializable {
private DefaultCallGraph graph;
private MethodReference method;
private Set<DefaultCallSite> callSites = new HashSet<>();
private transient Set<DefaultCallSite> callSites = new HashSet<>();
private List<DefaultCallSite> callSiteList;
private transient Set<DefaultCallSite> safeCallSites;
private List<DefaultCallSite> callerCallSites = new ArrayList<>();
private transient List<DefaultCallSite> safeCallersCallSites;
private Set<DefaultFieldAccessSite> fieldAccessSites = new HashSet<>();
private transient Set<DefaultFieldAccessSite> fieldAccessSites = new HashSet<>();
private List<DefaultFieldAccessSite> fieldAccessSiteList;
private transient Set<DefaultFieldAccessSite> safeFieldAccessSites;
private Set<DefaultClassAccessSite> classAccessSites = new HashSet<>();
private transient Set<DefaultClassAccessSite> classAccessSites = new HashSet<>();
private List<DefaultClassAccessSite> classAccessSiteList;
private transient Set<DefaultClassAccessSite> safeClassAccessSites;
DefaultCallGraphNode(DefaultCallGraph graph, MethodReference method) {
@ -58,6 +61,7 @@ public class DefaultCallGraphNode implements CallGraphNode {
@Override
public Collection<DefaultCallSite> getCallSites() {
ensureDeserialized();
if (safeCallSites == null) {
safeCallSites = Collections.unmodifiableSet(callSites);
}
@ -66,6 +70,7 @@ public class DefaultCallGraphNode implements CallGraphNode {
@Override
public Collection<DefaultCallSite> getCallerCallSites() {
ensureDeserialized();
if (safeCallersCallSites == null) {
safeCallersCallSites = Collections.unmodifiableList(callerCallSites);
}
@ -73,6 +78,7 @@ public class DefaultCallGraphNode implements CallGraphNode {
}
public boolean addCallSite(MethodReference method, TextLocation location) {
ensureDeserialized();
DefaultCallGraphNode callee = graph.getNode(method);
DefaultCallSite callSite = new DefaultCallSite(location, callee, this);
if (callSites.add(callSite)) {
@ -89,6 +95,7 @@ public class DefaultCallGraphNode implements CallGraphNode {
@Override
public Collection<DefaultFieldAccessSite> getFieldAccessSites() {
ensureDeserialized();
if (safeFieldAccessSites == null) {
safeFieldAccessSites = Collections.unmodifiableSet(fieldAccessSites);
}
@ -96,6 +103,7 @@ public class DefaultCallGraphNode implements CallGraphNode {
}
public boolean addFieldAccess(FieldReference field, TextLocation location) {
ensureDeserialized();
DefaultFieldAccessSite site = new DefaultFieldAccessSite(location, this, field);
if (fieldAccessSites.add(site)) {
graph.addFieldAccess(site);
@ -107,6 +115,7 @@ public class DefaultCallGraphNode implements CallGraphNode {
@Override
public Collection<? extends ClassAccessSite> getClassAccessSites() {
ensureDeserialized();
if (safeClassAccessSites == null) {
safeClassAccessSites = Collections.unmodifiableSet(classAccessSites);
}
@ -114,6 +123,7 @@ public class DefaultCallGraphNode implements CallGraphNode {
}
public boolean addClassAccess(String className, TextLocation location) {
ensureDeserialized();
DefaultClassAccessSite site = new DefaultClassAccessSite(location, this, className);
if (classAccessSites.add(site)) {
graph.addClassAccess(site);
@ -122,4 +132,31 @@ public class DefaultCallGraphNode implements CallGraphNode {
return false;
}
}
private void ensureDeserialized() {
if (callSites != null) {
return;
}
callSites = new HashSet<>(callSiteList);
callSiteList = null;
fieldAccessSites = new HashSet<>(fieldAccessSiteList);
fieldAccessSiteList = null;
classAccessSites = new HashSet<>(classAccessSiteList);
classAccessSiteList = null;
}
private void writeObject(ObjectOutputStream out) throws IOException {
ensureDeserialized();
callSiteList = new ArrayList<>(callSites);
fieldAccessSiteList = new ArrayList<>(fieldAccessSites);
classAccessSiteList = new ArrayList<>(classAccessSites);
out.defaultWriteObject();
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
}
}

View File

@ -15,14 +15,11 @@
*/
package org.teavm.callgraph;
import java.io.Serializable;
import java.util.Objects;
import org.teavm.model.TextLocation;
/**
*
* @author Alexey Andreev
*/
public class DefaultCallSite implements CallSite {
public class DefaultCallSite implements CallSite, Serializable {
private TextLocation location;
private DefaultCallGraphNode callee;
private DefaultCallGraphNode caller;

View File

@ -15,14 +15,11 @@
*/
package org.teavm.callgraph;
import java.io.Serializable;
import java.util.Objects;
import org.teavm.model.TextLocation;
/**
*
* @author Alexey Andreev
*/
public class DefaultClassAccessSite implements ClassAccessSite {
public class DefaultClassAccessSite implements ClassAccessSite, Serializable {
private TextLocation location;
private CallGraphNode callee;
private String className;

View File

@ -15,15 +15,12 @@
*/
package org.teavm.callgraph;
import java.io.Serializable;
import java.util.Objects;
import org.teavm.model.FieldReference;
import org.teavm.model.TextLocation;
/**
*
* @author Alexey Andreev
*/
public class DefaultFieldAccessSite implements FieldAccessSite {
public class DefaultFieldAccessSite implements FieldAccessSite, Serializable {
private TextLocation location;
private CallGraphNode callee;
private FieldReference field;

View File

@ -15,14 +15,11 @@
*/
package org.teavm.diagnostics;
import java.io.Serializable;
import java.util.Arrays;
import org.teavm.model.*;
/**
*
* @author Alexey Andreev
*/
public class Problem {
public class Problem implements Serializable {
private ProblemSeverity severity;
private CallLocation location;
private String text;

View File

@ -17,10 +17,6 @@ package org.teavm.diagnostics;
import java.util.List;
/**
*
* @author Alexey Andreev
*/
public interface ProblemProvider {
List<Problem> getProblems();

View File

@ -15,11 +15,9 @@
*/
package org.teavm.diagnostics;
/**
*
* @author Alexey Andreev
*/
public enum ProblemSeverity {
import java.io.Serializable;
public enum ProblemSeverity implements Serializable {
ERROR,
WARNING
}

View File

@ -15,9 +15,10 @@
*/
package org.teavm.model;
import java.io.Serializable;
import java.util.Objects;
public class CallLocation {
public class CallLocation implements Serializable {
private MethodReference method;
private TextLocation sourceLocation;

View File

@ -15,11 +15,9 @@
*/
package org.teavm.model;
/**
*
* @author Alexey Andreev
*/
public class FieldReference {
import java.io.Serializable;
public class FieldReference implements Serializable {
private String className;
private String fieldName;

View File

@ -17,6 +17,7 @@ package org.teavm.model;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import java.io.Serializable;
import java.util.Arrays;
/**
@ -30,7 +31,7 @@ import java.util.Arrays;
*
* @author Alexey Andreev
*/
public class MethodReference {
public class MethodReference implements Serializable {
private String className;
private String name;
private ValueType[] signature;

View File

@ -15,11 +15,9 @@
*/
package org.teavm.model;
/**
*
* @author Alexey Andreev
*/
public enum PrimitiveType {
import java.io.Serializable;
public enum PrimitiveType implements Serializable {
BOOLEAN,
CHARACTER,
BYTE,

View File

@ -15,9 +15,10 @@
*/
package org.teavm.model;
import java.io.Serializable;
import java.util.Objects;
public class TextLocation {
public class TextLocation implements Serializable {
private String fileName;
private int line = -1;

View File

@ -15,15 +15,11 @@
*/
package org.teavm.model;
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;
/**
*
* @author Alexey Andreev
*/
public abstract class ValueType {
public abstract class ValueType implements Serializable {
volatile String reprCache;
private static final Map<Class<?>, ValueType> primitiveMap = new HashMap<>();

View File

@ -15,11 +15,9 @@
*/
package org.teavm.vm;
/**
*
* @author Alexey Andreev
*/
public enum TeaVMProgressFeedback {
import java.io.Serializable;
public enum TeaVMProgressFeedback implements Serializable {
CONTINUE,
CANCEL
}