Skip to content

Commit

Permalink
Improved interface type inference for some core interfaces; Fixed ser…
Browse files Browse the repository at this point in the history
…ialization of Arrays in JsonWriter; Little fixes and asserts added;
  • Loading branch information
studio42gmbh committed Oct 24, 2023
1 parent 6f66c57 commit ae25458
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 88 deletions.
7 changes: 5 additions & 2 deletions src/main/java/de/s42/dl/DLCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import de.s42.dl.exceptions.DLException;
import de.s42.dl.exceptions.InvalidPragma;
import de.s42.dl.exceptions.InvalidInstance;
import de.s42.dl.exceptions.InvalidType;
import java.util.*;

/**
Expand Down Expand Up @@ -97,7 +98,7 @@ public interface DLCore extends DLEntity

public List<DLType> getTypes();

public List<DLType> getTypes(Class<? extends DLAnnotation> annotationType);
public <AnnotationType extends DLAnnotation> List<DLType> getTypes(Class<AnnotationType> annotationType);

public boolean isAllowDefineTypes();

Expand Down Expand Up @@ -148,8 +149,10 @@ public interface DLCore extends DLEntity
public List<DLInstance> getExported();

public Optional<DLInstance> getExported(String name);

public <JavaType> List<DLInstance> getExportedByJavaType(Class<JavaType> annotationType) throws InvalidType;

public List<DLInstance> getExported(Class<? extends DLAnnotation> annotationType);
public <AnnotationType extends DLAnnotation> List<DLInstance> getExported(Class<AnnotationType> annotationType);

// PRAGMAS
public DLPragma definePragma(DLPragma pragma, String... aliases) throws DLException;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/de/s42/dl/DLInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ default public DLCore getCore()

public Optional<?> getChildAsJavaObject(DLType type);

public List getChildrenAsJavaType(Class<?> javaType);
public <ObjectType extends Object> Optional<ObjectType> getChildAsJavaObject(Class<ObjectType> javaType);

public <ObjectType extends Object> List<ObjectType> getChildrenAsJavaType(Class<ObjectType> javaType);

public List getChildrenAsJavaType();
}
12 changes: 6 additions & 6 deletions src/main/java/de/s42/dl/annotations/AbstractDLAnnotated.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ public void addAnnotation(DLAnnotation annotation)
}

@Override
public Optional<DLAnnotation> getAnnotation(Class<? extends DLAnnotation> type)
public <AnnotationType extends DLAnnotation> Optional<AnnotationType> getAnnotation(Class<AnnotationType> type)
{
assert type != null;

for (DLAnnotation annotation : annotations) {
if (type.isAssignableFrom(annotation.getClass())) {
return Optional.of(annotation);
return Optional.of((AnnotationType)annotation);
}
}

Expand All @@ -78,15 +78,15 @@ public Optional<DLAnnotation> getAnnotation(String name)
}

@Override
public List<DLAnnotation> getAnnotations(Class<? extends DLAnnotation> type)
public <AnnotationType extends DLAnnotation> List<AnnotationType> getAnnotations(Class<AnnotationType> type)
{
assert type != null;

List<DLAnnotation> result = new ArrayList<>();
List<AnnotationType> result = new ArrayList<>();

for (DLAnnotation annotation : annotations) {
if (type.isAssignableFrom(annotation.getClass())) {
result.add(annotation);
result.add((AnnotationType)annotation);
}
}

Expand All @@ -110,7 +110,7 @@ public List<DLAnnotation> getAnnotations(String name)
}

@Override
public boolean hasAnnotation(Class<? extends DLAnnotation> type)
public <AnnotationType extends DLAnnotation> boolean hasAnnotation(Class<AnnotationType> type)
{
assert type != null;

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/de/s42/dl/annotations/DLAnnotated.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ public interface DLAnnotated extends DLEntity

public List<DLAnnotation> getAnnotations();

public List<DLAnnotation> getAnnotations(Class<? extends DLAnnotation> type);
public <AnnotationType extends DLAnnotation> List<AnnotationType> getAnnotations(Class<AnnotationType> type);

public List<DLAnnotation> getAnnotations(String name);

public Optional<DLAnnotation> getAnnotation(Class<? extends DLAnnotation> type);
public <AnnotationType extends DLAnnotation> Optional<AnnotationType> getAnnotation(Class<AnnotationType> type);

public Optional<DLAnnotation> getAnnotation(String name);

public boolean hasAnnotations();

public boolean hasAnnotation(Class<? extends DLAnnotation> type);
public <AnnotationType extends DLAnnotation> boolean hasAnnotation(Class<AnnotationType> type);

public boolean hasAnnotation(String name);

Expand Down
95 changes: 59 additions & 36 deletions src/main/java/de/s42/dl/core/BaseDLCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private void init(boolean allowAll)
allowUsePragmas = allowAll;
allowRequire = allowAll;
allowUseAsserts = allowAll;

referenceResolver = DEFAULT_REFERENCE_RESOLVER;
pathResolver = new DefaultDLPathResolver();
loadModuleType(this);
Expand Down Expand Up @@ -245,29 +245,29 @@ protected void bindAnnotation(DLAnnotation annotation, DLAnnotated container) th
assert annotation != null;
assert container != null;

if (container instanceof DLAttribute) {
if (container instanceof DLAttribute dLAttribute) {

if (annotation instanceof DLValidator && !((DLValidator) annotation).canValidateAttribute()) {
throw new InvalidAnnotation("Annotation '" + annotation + "' can not validate attributes");
}

annotation.bindToAttribute((DLAttribute) container);
} else if (container instanceof DLInstance) {
annotation.bindToAttribute(dLAttribute);
} else if (container instanceof DLInstance dLInstance) {

if (annotation instanceof DLValidator && !((DLValidator) annotation).canValidateInstance()) {
throw new InvalidAnnotation("Annotation '" + annotation + "' can not validate instances");
}

annotation.bindToInstance((DLInstance) container);
} else if (container instanceof DLType) {
annotation.bindToInstance(dLInstance);
} else if (container instanceof DLType dLType) {

if (annotation instanceof DLValidator
&& !(((DLValidator) annotation).canValidateType()
|| ((DLValidator) annotation).canValidateTypeRead())) {
throw new InvalidAnnotation("Annotation '" + annotation + "' can not validate types");
}

annotation.bindToType((DLType) container);
annotation.bindToType(dLType);
}
}

Expand Down Expand Up @@ -511,14 +511,38 @@ public Optional<DLInstance> getExported(String name)
}

@Override
public List<DLInstance> getExported(Class<? extends DLAnnotation> annotationType)
public <JavaType> List<DLInstance> getExportedByJavaType(Class<JavaType> javaType) throws InvalidType
{
assert javaType != null;

List<DLInstance> result = new ArrayList<>();

Optional<DLType> optDlType = getType(javaType);

if (optDlType.isEmpty()) {
throw new InvalidType("No type mapped for " + javaType);
}

DLType dlType = optDlType.orElseThrow();

for (DLInstance instance : exported.values()) {
if (dlType.isAssignableFrom(instance.getType())) {
result.add(instance);
}
}

return result;
}

@Override
public <AnnotationType extends DLAnnotation> List<DLInstance> getExported(Class<AnnotationType> annotationType)
{
assert annotationType != null;

List<DLInstance> result = new ArrayList<>();

for (DLInstance instance : exported.values()) {
Optional<DLAnnotation> optEventAnnotation = instance.getAnnotation(annotationType);
Optional<AnnotationType> optEventAnnotation = instance.getAnnotation(annotationType);

if (optEventAnnotation.isPresent()) {
result.add(instance);
Expand All @@ -545,14 +569,14 @@ public List<DLType> getTypes()
}

@Override
public List<DLType> getTypes(Class<? extends DLAnnotation> annotationType)
public <AnnotationType extends DLAnnotation> List<DLType> getTypes(Class<AnnotationType> annotationType)
{
assert annotationType != null;

List<DLType> result = new ArrayList<>();

for (DLType type : types.values()) {
Optional<DLAnnotation> optEventAnnotation = type.getAnnotation(annotationType);
Optional<AnnotationType> optEventAnnotation = type.getAnnotation(annotationType);

if (optEventAnnotation.isPresent()) {
result.add(type);
Expand Down Expand Up @@ -658,8 +682,8 @@ public DLType defineType(DLType type, String... aliases) throws InvalidCore, Inv
}

// Default types get this core set as core
if (type instanceof DefaultDLType) {
((DefaultDLType) type).setCore(this);
if (type instanceof DefaultDLType defaultDLType) {
defaultDLType.setCore(this);
}

// Make sure the type is valid
Expand Down Expand Up @@ -919,15 +943,15 @@ && hasType(ObjectDLType.DEFAULT_SYMBOL)) {

// Handle contains with searching for DLContainer<?>
for (Type interfaceType : typeClass.getGenericInterfaces()) {
if (interfaceType instanceof ParameterizedType) {
if (interfaceType instanceof ParameterizedType parameterizedType) {

ParameterizedType paramType = ((ParameterizedType) interfaceType);
ParameterizedType paramType = parameterizedType;

// validate if the type is a DLContainer
if (DLContainer.class.isAssignableFrom((Class) paramType.getRawType())) {

// check and add its types in diamond operator as contained types
for (Type type : ((ParameterizedType) interfaceType).getActualTypeArguments()) {
for (Type type : parameterizedType.getActualTypeArguments()) {

// container uses the current type - use the new type
if ((type instanceof Class) && ((Class) type).equals(typeClass)) {
Expand Down Expand Up @@ -1444,34 +1468,34 @@ public synchronized Object convertFromInstance(DLInstance instance) throws Inval
convertInstance = type.createJavaInstance();

//allow maps to be set by that
if (convertInstance instanceof Map) {
if (convertInstance instanceof Map map) {

//write properties of the instance
for (String attributeName : instance.getAttributeNames()) {

Object value = instance.get(attributeName);

if (value instanceof Object[]) {
if (value instanceof Object[] objects) {

Object[] convValue = new Object[((Object[]) value).length];
Object[] convValue = new Object[objects.length];

for (int i = 0; i < ((Object[]) ((Object[]) value)).length; ++i) {
for (int i = 0; i < ((Object[]) objects).length; ++i) {

Object val = ((Object[]) value)[i];
Object val = objects[i];

if (val instanceof DLInstance) {
convValue[i] = ((DLInstance) val).toJavaObject();
if (val instanceof DLInstance dLInstance) {
convValue[i] = dLInstance.toJavaObject();
} else {
convValue[i] = val;
}
}

value = convValue;
} else if (value instanceof DLInstance) {
value = ((DLInstance) value).toJavaObject();
} else if (value instanceof DLInstance dLInstance) {
value = dLInstance.toJavaObject();
}

((Map) convertInstance).put(attributeName, value);
map.put(attributeName, value);
}

for (DLInstance child : instance.getChildren()) {
Expand All @@ -1480,7 +1504,7 @@ public synchronized Object convertFromInstance(DLInstance instance) throws Inval

Object convertChild = convertFromInstance(child);

((Map) convertInstance).put(child.getName(), convertChild);
map.put(child.getName(), convertChild);
}
}
} //default to assume its a bean
Expand All @@ -1494,26 +1518,26 @@ public synchronized Object convertFromInstance(DLInstance instance) throws Inval

Object value = instance.get(attributeName);

if (value instanceof Object[]) {
if (value instanceof Object[] objects) {

Object[] convValue = new Object[((Object[]) value).length];
Object[] convValue = new Object[objects.length];

for (int i = 0; i < ((Object[]) ((Object[]) value)).length; ++i) {
for (int i = 0; i < ((Object[]) objects).length; ++i) {

Object val = ((Object[]) value)[i];
Object val = objects[i];

if (val instanceof DLInstance) {
convValue[i] = ((DLInstance) val).toJavaObject();
if (val instanceof DLInstance dLInstance) {
convValue[i] = dLInstance.toJavaObject();
} else {
convValue[i] = val;
}
}

value = convValue;
} else if (value instanceof DLInstance) {
} else if (value instanceof DLInstance dLInstance) {

try {
value = ((DLInstance) value).toJavaObject();
value = dLInstance.toJavaObject();
} catch (AssertionError ex) {
throw new AssertionError("Error converting value '" + attributeName + "' to JavaObject in instance '" + instance.getName() + "'", ex);
}
Expand Down Expand Up @@ -1618,7 +1642,6 @@ public void setAllowUseAsserts(boolean allowUseAsserts)
this.allowUseAsserts = allowUseAsserts;
}


@Override
public String getName()
{
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/de/s42/dl/core/DefaultCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
import de.s42.log.LogLevel;
import de.s42.log.LogManager;
import de.s42.log.Logger;
import java.util.Optional;

/**
*
Expand Down Expand Up @@ -360,5 +361,8 @@ public static void loadTypes(BaseDLCore core) throws DLException
"java.util.Collections$UnmodifiableSet",
"java.util.ImmutableCollections$SetN",
SetDLType.class.getName());

// Define type for Optional<>
core.defineType(Optional.class);
}
}
Loading

0 comments on commit ae25458

Please sign in to comment.