Skip to content

Commit

Permalink
fixed #49
Browse files Browse the repository at this point in the history
  • Loading branch information
miho committed Feb 13, 2022
1 parent 21f004b commit 355f814
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 209 deletions.
18 changes: 12 additions & 6 deletions core/src/main/java/eu/mihosoft/vmf/core/Implementation.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,18 @@ private void collectAndSetProperties() {

// only add properties that are not already present in properties
this.properties.addAll(
implProperties.stream().filter(p -> !properties.contains(p)).distinct().collect(Collectors.toList()));

// filter out duplicates (considering overloaded properties)
List<Prop> distinctProperties = Prop.filterDuplicateProps(properties, false);
this.properties.clear();
this.properties.addAll(distinctProperties);
implProperties.stream().filter(p -> !properties.stream()
.map(myP->myP.getName())
.filter(myPName->myPName.equals(p.getName()))
.findAny().isPresent()
)
.distinct().collect(Collectors.toList())
);

// // filter out duplicates (considering overloaded properties)
// List<Prop> distinctProperties = Prop.filterDuplicateProps(type, properties, false);
// this.properties.clear();
// this.properties.addAll(distinctProperties);
}

private static List<Prop> computeImplementedProperties(ModelType type) {
Expand Down
23 changes: 17 additions & 6 deletions core/src/main/java/eu/mihosoft/vmf/core/Interface.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,24 @@ void initProperties() {
throw new RuntimeException("Properties of '" + getName() + "' already initialized!");
}

List<Prop> allProps = new ArrayList<>();
allProps.addAll(type.getProperties());
allProps.addAll(computeImplementedProperties(type));
properties.addAll(type.getProperties());

List<Prop> implProperties = new ArrayList<>();
implProperties.addAll(computeImplementedProperties(type));

// only add properties that are not already present in properties
this.properties.addAll(
implProperties.stream().filter(p -> !properties.stream()
.map(myP->myP.getName())
.filter(myPName->myPName.equals(p.getName()))
.findAny().isPresent()
)
.distinct().collect(Collectors.toList())
);

// filter out duplicates (considering overloaded properties)
List<Prop> distinctProperties = Prop.filterDuplicateProps(allProps, false);
this.properties.addAll(distinctProperties);
// // filter out duplicates (considering overloaded properties)
// List<Prop> distinctProperties = Prop.filterDuplicateProps(type, allProps, false);
// this.properties.addAll(distinctProperties);

this.propertiesWithoutCollectionsBasedContainment.addAll(
ModelType.propertiesWithoutCollectionsBasedContainment(
Expand Down
13 changes: 5 additions & 8 deletions core/src/main/java/eu/mihosoft/vmf/core/ModelType.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,18 @@ private void initProperties(Class<?> clazz) {

List<Method> list = new ArrayList<Method>();
for (Method m : clazz.getDeclaredMethods()) {
if (m.getName().startsWith("get")
if(!m.isBridge()) {
if (m.getName().startsWith("get")
|| (m.getName().startsWith("is")
&& Objects.equals(m.getReturnType(), boolean.class))) {
list.add(m);
list.add(m);
}
}
}

boolean hasPropertyWithoutCustomOrder = false;
for (Method m : list) {
Prop p = Prop.newInstance(this, m);

properties.add(p);

if(p.getCustomOrderIndex()!=null) {
Expand All @@ -206,7 +207,7 @@ public boolean test(Prop p) {
}
};

// find potential duplicates
// find potential duplicate order indices
long numDuplicates = properties.stream().filter(distinctByName).
collect(Collectors.groupingBy(p -> p.getCustomOrderIndex(),
Collectors.counting())).values().stream().filter(frequency -> frequency > 1).count();
Expand All @@ -217,10 +218,6 @@ public boolean test(Prop p) {
}
}

List<Prop> distinctProperties = Prop.filterDuplicateProps(properties, false);
this.properties.clear();
this.properties.addAll(distinctProperties);

sortProperties(properties, customPropertyOrderPresent);
}

Expand Down
173 changes: 0 additions & 173 deletions core/src/main/java/eu/mihosoft/vmf/core/Prop.java
Original file line number Diff line number Diff line change
Expand Up @@ -828,179 +828,6 @@ static String computeGetterPrefix(Method getterMethod) {
return getterPrefix;
}

static List<Prop> filterDuplicateProps(List<Prop> properties, boolean throwTypeNotResolvable) {
List<Prop> result = new ArrayList(properties);
List<String> distinctNames = result.stream().
map(p -> p.getName()).distinct().collect(Collectors.toList());

List<Prop> distinctProperties = new ArrayList<>();

for (String pName : distinctNames) {
List<Prop> collidingProps = result.stream().
filter(p -> Objects.equals(pName, p.getName())).collect(Collectors.toList());

if (collidingProps.size() < 2) {
continue;
}

Prop p = collidingProps.get(0);

for (Prop otherP : collidingProps) {

if (Objects.equals(p, otherP)) {
continue;
}


boolean firstIsModelType = p.getType() != null;
boolean secondIsModelType = otherP.getType() != null;
boolean modelType = firstIsModelType && secondIsModelType;

if (modelType && otherP.getType().extendsType(p.getType())) {
// System.out.println("Extends: " + p.getTypeName() + " -> " + otherP.getTypeName());
p = otherP; // otherP is more specific than p

// set inherited param
otherP.setInheritedProp(p);
} else if (modelType && p.getType().extendsType(otherP.getType())) {
// System.out.println("Extends: " + otherP.getTypeName() + " -> " + p.getTypeName());
// nothing to do since p is the most specific one already (p = p);

// set inherited param
p.setInheritedProp(otherP);
} else if (!modelType && Objects.equals(p.getTypeName(), otherP.getTypeName())) {
// types are identical. nothing to do
} else if (!modelType) {

if(firstIsModelType && !secondIsModelType) {
ModelType pType = p.getType();

if(pType.extendsType(otherP.getTypeName())) {
// System.out.println("Extends: " + otherP.getTypeName() + " -> " + p.getTypeName());
// nothing to do since p is the most specific one already (p = p);

// set inherited param
p.setInheritedProp(otherP);
}

} else if(!firstIsModelType && secondIsModelType) {
ModelType otherPType = otherP.getType();

if(otherPType.extendsType(p.getTypeName())) {
// System.out.println("Extends: " + otherP.getTypeName() + " -> " + p.getTypeName());
p = otherP; // otherP is more specific than p

// set inherited param
otherP.setInheritedProp(p);
}

} else {

// we try to get type information from external types:
Optional<Class<?>> pType = p.getParent().getModel().resolveExternalType(p.getTypeName());
Optional<Class<?>> otherPType = otherP.getParent().getModel().resolveExternalType(otherP.getTypeName());

if (!pType.isPresent() || !otherPType.isPresent()) {
if (!pType.isPresent()) {
String msg = "Cannot resolve external type '" + p.getTypeName() + "'";
System.err.println("TODO [28.05.2018]: " + msg);
System.err.println("TODO [28.05.2018]: could be that the types are not yet initialized.");
System.err.println("TODO [28.05.2018]: -> see PASS 0 & 4 in Model.java");
if (throwTypeNotResolvable) {
throw new RuntimeException(msg);
}
} else {
String msg = "Cannot resolve external type '" + otherP.getTypeName() + "'";
System.err.println("TODO [28.05.2018]: " + msg);
System.err.println("TODO [28.05.2018]: could be that the types are not yet initialized.");
System.err.println("TODO [28.05.2018]: -> see PASS 0 & 4 in Model.java");
if (throwTypeNotResolvable) {
throw new RuntimeException(msg);
}
}
} else if (pType.get().isAssignableFrom(otherPType.get())) {
// System.out.println("Extends: " + p.getTypeName() + " -> " + otherP.getTypeName());
p = otherP; // otherP is more specific than p
otherP.setInheritedProp(p);
} else if (otherPType.get().isAssignableFrom(pType.get())) {
// System.out.println("Extends: " + otherP.getTypeName() + " -> " + p.getTypeName());
// nothing to do since p is the most specific one already
p.setInheritedProp(otherP);
}
}
}
}

distinctProperties.add(p);
}

for (Prop dP : distinctProperties) {
for (Iterator<Prop> it = result.iterator(); it.hasNext(); ) {
Prop p = it.next();

if (Objects.equals(dP.getName(), p.getName())) {
it.remove();
}
}
}

result.addAll(distinctProperties);

return result;
}

static List<Prop> filterDuplicatePropsExp(List<Prop> properties) {
List<Prop> result = new ArrayList(properties);
List<String> distinctNames = result.stream().
map(p -> p.getName()).distinct().collect(Collectors.toList());

List<Prop> distinctProperties = new ArrayList<>();

for (String pName : distinctNames) {
List<Prop> collidingProps = result.stream().
filter(p -> Objects.equals(pName, p.getName())).collect(Collectors.toList());

if (collidingProps.size() < 2) {
continue;
}

Prop p = collidingProps.get(0);

for (Prop otherP : collidingProps) {

if (Objects.equals(p, otherP)) {
continue;
}

if (Objects.equals(p.getTypeName(), otherP.getTypeName())) {
continue;
} else if (!p.isGetterOnly() && !otherP.isGetterOnly()) {

System.err.println("getterp1: " + p.isGetterOnly());
System.err.println("getterp2: " + otherP.isGetterOnly());


}
}

distinctProperties.add(p);
}

for (Prop dP : distinctProperties) {
for (Iterator<Prop> it = result.iterator(); it.hasNext(); ) {
Prop p = it.next();

if (Objects.equals(dP.getName(), p.getName())) {
it.remove();
}
}
}

result.addAll(distinctProperties);

return result;
}

/**
* Returns the requested customOrderIndex of this property.
* @return the requested customOrderIndex of this property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,10 @@ interface __VMF_TYPE_${type.implementation.typeName} extends ${type.typeName}${t

#if(${prop.isInheritedProp()})## if inheritedprop
// --- PROP '$prop.name : $prop.typeName' , Inherited from: '${prop.inheritedProp.parent.typeName}.${prop.inheritedProp.name}'
void __vmf_set_prop_${prop.name}(${prop.inheritedProp.typeName} obj);
default void __vmf_set_prop_${prop.name}(${prop.inheritedProp.typeName} obj) { throw new UnsupportedOperationException("Should never be called."); }
#else##------------------------- else inheritedprop
// --- PROP '$prop.name : $prop.typeName'
void __vmf_set_prop_${prop.name}($prop.typeName obj);
default void __vmf_set_prop_${prop.name}($prop.typeName obj) { throw new UnsupportedOperationException("Should never be called."); }
#end##-------------------------- end inheritedprop
$prop.typeName __vmf_get_prop_${prop.name}();
#if(${prop.propType} == "COLLECTION")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ interface __VMF_TYPE_${type.implementation.typeName} extends ${type.packageName}

#foreach( $prop in ${type.implementation.properties})
// --- PROP '$prop.typeName'
void __vmf_set_prop_${prop.name}($prop.typeName obj);
default void __vmf_set_prop_${prop.name}($prop.typeName obj) { throw new UnsupportedOperationException("Should never be called."); }
$prop.typeName __vmf_get_prop_${prop.name}();
#if(${prop.propType} == "COLLECTION")
java.util.List<${prop.genericPackageName}.${prop.genericTypeName}> __vmf_get_prop_${prop.name}__vmf_RawList();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package eu.mihosoft.vmftest.propertyinheritance;

import eu.mihosoft.vmf.runtime.core.Property;
import org.junit.Assert;
import org.junit.Test;

public class PropertyInheritanceTest {


@Test
public void propertyInheritanceTest01() {


PropSample01 sample01 = PropSample01.newInstance();

GCode1 gCode1 = GCode1.newInstance();

gCode1.setLocation(LocationXY.newInstance());


Property p = gCode1.vmf().reflect().propertyByName("location").orElseThrow();
// ensure the property uses the correct type
Assert.assertEquals(LocationXY.type(), p.getType());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface WithXY extends WithX, WithY {

}


interface Location {

}
Expand Down Expand Up @@ -80,17 +81,17 @@ interface PropSample01 {
}

interface GCode1 extends WithLocationXY {

LocationXY getLocation();
}

interface GCode2 extends WithLocationX {

LocationX getLocation();
}

interface GCode3 extends WithLocationY {

LocationY getLocation();
}

interface GCode4 extends WithLocation {

Location getLocation();
}

0 comments on commit 355f814

Please sign in to comment.