-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d8b907
commit 320d2a7
Showing
28 changed files
with
903 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.probejs.jdoc; | ||
|
||
import com.probejs.jdoc.document.AbstractDocument; | ||
|
||
public interface IDocumentProvider<T extends AbstractDocument> { | ||
T genDoc(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.probejs.jdoc; | ||
|
||
import com.probejs.jdoc.property.AbstractProperty; | ||
|
||
public interface IPropertyProvider<T extends AbstractProperty> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.probejs.jdoc; | ||
|
||
import com.google.gson.JsonObject; | ||
|
||
public interface ISerde { | ||
JsonObject serialize(); | ||
|
||
void deserialize(JsonObject object); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.probejs.jdoc; | ||
|
||
public class Manager { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package com.probejs.jdoc; | ||
|
||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.probejs.info.ClassInfo; | ||
import com.probejs.info.type.*; | ||
import com.probejs.jdoc.document.AbstractDocument; | ||
import com.probejs.jdoc.document.DocumentClass; | ||
import com.probejs.jdoc.document.DocumentField; | ||
import com.probejs.jdoc.document.DocumentMethod; | ||
import com.probejs.jdoc.property.*; | ||
|
||
import java.lang.reflect.*; | ||
import java.util.Collection; | ||
import java.util.function.Supplier; | ||
|
||
public class Serde { | ||
public static void init() { | ||
//Types | ||
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyType.Array.class, "type:array"); | ||
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyType.Clazz.class, "type:class"); | ||
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyType.Parameterized.class, "type:parameterized"); | ||
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyType.Variable.class, "type:variable"); | ||
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyType.Primitive.class, "type:primitive"); | ||
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyType.Intersection.class, "type:intersection"); | ||
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyType.Union.class, "type:union"); | ||
|
||
//Properties | ||
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyComment.class, "property:comment"); | ||
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyHide.class, "property:hide"); | ||
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyMod.class, "property:mod"); | ||
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyModify.class, "property:modify"); | ||
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyRename.class, "property:rename"); | ||
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyReturns.class, "property:returns"); | ||
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyParam.class, "property:param"); | ||
|
||
//Documents | ||
AbstractDocument.DOCUMENT_TYPE_REGISTRY.put(DocumentClass.class, "document:class"); | ||
AbstractDocument.DOCUMENT_TYPE_REGISTRY.put(DocumentMethod.class, "document:method"); | ||
AbstractDocument.DOCUMENT_TYPE_REGISTRY.put(DocumentField.class, "document:field"); | ||
|
||
} | ||
|
||
public static AbstractDocument deserializeDocument(JsonObject obj) { | ||
String type = obj.get("type").getAsString(); | ||
try { | ||
AbstractDocument doc = AbstractDocument.DOCUMENT_TYPE_REGISTRY.inverse().get(type).getDeclaredConstructor().newInstance(); | ||
doc.deserialize(obj); | ||
return doc; | ||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | | ||
NoSuchMethodException e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
public static AbstractProperty deserializeProperty(JsonObject obj) { | ||
String type = obj.get("type").getAsString(); | ||
try { | ||
AbstractProperty property = AbstractProperty.PROPERTY_TYPE_REGISTRY.inverse().get(type).getDeclaredConstructor().newInstance(); | ||
property.deserialize(obj); | ||
return property; | ||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | | ||
NoSuchMethodException e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
private static PropertyType constructType(Supplier<PropertyType> builder, ITypeInfo type) { | ||
PropertyType property = builder.get(); | ||
property.deserializeFromType(type); | ||
return property; | ||
} | ||
|
||
public static PropertyType deserializeFromJavaType(ITypeInfo type) { | ||
if (type instanceof TypeInfoClass) { | ||
return constructType(PropertyType.Clazz::new, type); | ||
} | ||
if (type instanceof TypeInfoArray) { | ||
return constructType(PropertyType.Array::new, type); | ||
} | ||
if (type instanceof TypeInfoVariable) { | ||
return constructType(PropertyType.Variable::new, type); | ||
} | ||
if (type instanceof TypeInfoParameterized) { | ||
return constructType(PropertyType.Parameterized::new, type); | ||
} | ||
if (type instanceof TypeInfoWildcard) { | ||
return constructType(PropertyType.Clazz::new, new TypeInfoClass(Object.class)); | ||
} | ||
return null; | ||
} | ||
|
||
public static JsonArray serializeCollection(Iterable<? extends ISerde> serdes) { | ||
JsonArray result = new JsonArray(); | ||
serdes.forEach(serde -> result.add(serde.serialize())); | ||
return result; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public static <T extends AbstractDocument> void deserializeDocuments(Collection<T> serdes, JsonElement jsonArray) { | ||
for (JsonElement element : jsonArray.getAsJsonArray()) { | ||
serdes.add((T) deserializeDocument(element.getAsJsonObject())); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public static <T extends AbstractProperty> void deserializeProperties(Collection<T> serdes, JsonElement jsonArray) { | ||
for (JsonElement element : jsonArray.getAsJsonArray()) { | ||
serdes.add((T) deserializeProperty(element.getAsJsonObject())); | ||
} | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
common/src/main/java/com/probejs/jdoc/document/AbstractDocument.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.probejs.jdoc.document; | ||
|
||
import com.google.common.collect.BiMap; | ||
import com.google.common.collect.HashBiMap; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.probejs.jdoc.ISerde; | ||
import com.probejs.jdoc.Serde; | ||
import com.probejs.jdoc.property.AbstractProperty; | ||
import com.probejs.jdoc.property.PropertyHide; | ||
import com.probejs.jdoc.property.PropertyMod; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Represents a document. | ||
* <p> | ||
* You must ensure that the parameter-less constructor <b>does not</b> depend | ||
* on any other stateful objects. As this document might be constructed at any time. | ||
*/ | ||
public abstract class AbstractDocument<T extends AbstractDocument<T>> implements ISerde { | ||
public static final BiMap<Class<? extends AbstractDocument<?>>, String> DOCUMENT_TYPE_REGISTRY = HashBiMap.create(); | ||
|
||
protected List<AbstractProperty> properties = new ArrayList<>(); | ||
|
||
public abstract T merge(T other); | ||
|
||
public abstract T copy(); | ||
|
||
public JsonObject serialize() { | ||
JsonObject obj = new JsonObject(); | ||
obj.addProperty("type", DOCUMENT_TYPE_REGISTRY.get(this.getClass())); | ||
JsonArray jsonProperties = new JsonArray(); | ||
for (AbstractProperty property : properties) { | ||
jsonProperties.add(property.serialize()); | ||
} | ||
obj.add("properties", jsonProperties); | ||
return obj; | ||
} | ||
|
||
public void deserialize(JsonObject object) { | ||
JsonArray propertiesJson = object.get("properties").getAsJsonArray(); | ||
for (JsonElement element : propertiesJson) { | ||
AbstractProperty property = Serde.deserializeProperty(element.getAsJsonObject()); | ||
this.properties.add(property); | ||
} | ||
} | ||
|
||
public Optional<AbstractProperty> findProperty(Class<? extends AbstractProperty> property) { | ||
return this.properties.stream().filter(p -> p.getClass() == property).findFirst(); | ||
} | ||
|
||
public boolean hasProperty(Class<? extends AbstractProperty> property) { | ||
return findProperty(property).isPresent(); | ||
} | ||
|
||
public List<AbstractProperty> findProperties(Predicate<AbstractProperty> predicate) { | ||
return this.properties.stream().filter(predicate).collect(Collectors.toList()); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T extends AbstractProperty> List<T> findPropertiesOf(Class<T> property) { | ||
return this.properties.stream().filter(prop -> property.isAssignableFrom(prop.getClass())).map(prop -> (T) prop).collect(Collectors.toList()); | ||
} | ||
|
||
public boolean allModsLoaded() { | ||
return findPropertiesOf(PropertyMod.class).stream().allMatch(PropertyMod::isModLoaded); | ||
} | ||
|
||
public boolean isHidden() { | ||
return hasProperty(PropertyHide.class); | ||
} | ||
} |
Oops, something went wrong.