Skip to content

Commit

Permalink
Support EolNativeType and refactor isCompatible
Browse files Browse the repository at this point in the history
  • Loading branch information
pkourouklidis committed Nov 13, 2024
1 parent 1f7fba6 commit 5d20017
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import java.io.FileOutputStream;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;

import javax.xml.parsers.DocumentBuilderFactory;
Expand All @@ -22,8 +21,8 @@
import javax.xml.transform.stream.StreamResult;

import org.eclipse.epsilon.common.util.CollectionUtil;
import org.eclipse.epsilon.eol.types.EolPrimitiveType;
import org.eclipse.epsilon.eol.types.EolType;
import org.eclipse.epsilon.eol.staticanalyser.types.EolPrimitiveType;
import org.eclipse.epsilon.eol.staticanalyser.types.EolType;
import org.w3c.dom.Document;
import org.w3c.dom.Text;

Expand All @@ -35,8 +34,8 @@ public boolean contributesTo(Object target) {
}

@Override
public List<EolType> contributesToType() {
return Arrays.asList(EolPrimitiveType.String);
public EolType contributesToType() {
return EolPrimitiveType.String;
}

public Object toEnum() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
import org.eclipse.epsilon.eol.staticanalyser.types.EolCollectionType;
import org.eclipse.epsilon.eol.staticanalyser.types.EolMapType;
import org.eclipse.epsilon.eol.staticanalyser.types.EolModelElementType;
import org.eclipse.epsilon.eol.staticanalyser.types.EolNativeType;
import org.eclipse.epsilon.eol.staticanalyser.types.EolNoType;
import org.eclipse.epsilon.eol.staticanalyser.types.EolPrimitiveType;
import org.eclipse.epsilon.eol.staticanalyser.types.EolType;
Expand Down Expand Up @@ -719,7 +720,7 @@ public void visit(OperationCallExpression operationCallExpression) {
for (IStaticOperation op: resolvedOperations) {
EolType opContextType = op.getContextType();
if(contextType == opContextType ||
isCompatible(opContextType, contextType) ||
opContextType.isAncestorOf(contextType) ||
canBeCompatible(opContextType, contextType)) {
temp.add(op);
}
Expand Down Expand Up @@ -755,7 +756,7 @@ public void visit(OperationCallExpression operationCallExpression) {
for (EolType reqParamType : reqParamTypess) {
EolType provParamType = getResolvedType(parameterExpressions.get(index));
index++;
if (!isCompatible(reqParamType, provParamType)
if (!reqParamType.isAncestorOf(provParamType)
&& !canBeCompatible(reqParamType, provParamType)) {
compatible = false;
break;
Expand Down Expand Up @@ -1251,8 +1252,6 @@ public EolType javaClassToEolType(Class<?> javaClass) {
} else if (javaClass == Double.class || javaClass == double.class || javaClass == Float.class
|| javaClass == float.class) {
return EolPrimitiveType.Real;
} else if (javaClass == Number.class) {
return new EolUnionType(EolPrimitiveType.Real, EolPrimitiveType.Integer);
} else if (javaClass == boolean.class || javaClass == Boolean.class) {
return EolPrimitiveType.Boolean;
} else if (javaClass == java.util.Collection.class) {
Expand All @@ -1264,7 +1263,7 @@ public EolType javaClassToEolType(Class<?> javaClass) {
} else if (javaClass == java.util.Map.class) {
return EolMapType.Map;
} else {
return EolAnyType.Instance;
return new EolNativeType(javaClass);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ public String toString() {
protected EolType getParentType() {
return null;
}

@Override
public boolean isAncestorOf(EolType type) {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.eclipse.epsilon.eol.staticanalyser.types;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class EolNativeType extends EolType {

private Class<?> javaClass;

public EolNativeType(Class<?> javaClass) {
this.javaClass = javaClass;
}

@Override
public String getName() {
return "Native";
}

@Override
public String toString() {
return "Native<" + javaClass.getName() +">";
}

@Override
public List<EolType> getParentTypes() {
if (javaClass == Object.class) {
return Collections.emptyList();
}else {
return Arrays.asList(new EolNativeType(javaClass.getSuperclass()));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ public EolType getParentType() {
return null;
}

@Override
public boolean isAncestorOf(EolType type){
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
******************************************************************************/
package org.eclipse.epsilon.eol.staticanalyser.types;

import java.util.Arrays;
import java.util.List;

public class EolPrimitiveType extends EolType {

private Class<?> clazz;
Expand Down Expand Up @@ -38,4 +41,9 @@ public String getName() {
public String toString() {
return getName();
}

@Override
public List<EolType> getParentTypes() {
return Arrays.asList(EolAnyType.Instance, new EolNativeType(this.clazz));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,86 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.Stack;

public abstract class EolType {

private Set<EolType> ancestorCache = null;

public abstract String getName();

public boolean isNot(EolType type) {
return this != type && this != EolAnyType.Instance;
}

@Override
public String toString() {
return getName();
}

@Override
public int hashCode() {
return Objects.hash(getName());
}

@Override
public boolean equals(Object other) {
if (this == other) return true;
if (other == null) return false;

if (this.getClass() != other.getClass()) return false;

if (this == other)
return true;
if (other == null)
return false;

if (this.getClass() != other.getClass())
return false;

EolType eolType = (EolType) other;

return Objects.equals(this.getName(), eolType.getName());
}

public List<EolType> getParentTypes() {
EolType parentType = getParentType();
if (parentType == null) return Collections.emptyList();
else return Arrays.asList(parentType);
if (parentType == null)
return Collections.emptyList();
else
return Arrays.asList(parentType);
}

protected EolType getParentType() {
return EolAnyType.Instance;
}

public List<EolType> getChildrenTypes(){

public Set<EolType> getAncestors() {
if (ancestorCache != null) {
return ancestorCache;
}
Set<EolType> ancestors = new HashSet<EolType>();

Stack<EolType> stack = new Stack<EolType>();
stack.push(this);
while (!stack.isEmpty()) {
EolType currentNode = stack.pop();
if (!ancestors.contains(currentNode)) {
ancestors.add(currentNode);
stack.addAll(currentNode.getParentTypes());
}
}

ancestorCache = ancestors;
return ancestors;
}

public List<EolType> getChildrenTypes() {
return Collections.emptyList();
}

public boolean isAncestorOf(EolType type) {
if (type.equals(EolNoType.Instance)) {
return false;
}
return type.getAncestors().contains(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/*Native<java.lang.Number>*/1.1.abs();

0 comments on commit 5d20017

Please sign in to comment.