Skip to content

Commit

Permalink
Fix some problems
Browse files Browse the repository at this point in the history
  • Loading branch information
LABSARI committed Mar 5, 2024
1 parent 956a25f commit a0e9988
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 35 deletions.
Binary file modified lib/verveine.extractor.java.jar
Binary file not shown.
40 changes: 29 additions & 11 deletions src/fr/inria/verveine/extractor/java/EntityDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ public Inheritance ensureFamixInheritance(TWithInheritances sup, TWithInheritanc
}

for (TInheritance i : (sup).getSubInheritances()) {

if (i.getSubclass() == sub) {
return (Inheritance) i;
}
Expand Down Expand Up @@ -550,7 +551,7 @@ public Implementation ensureFamixImplementation(TImplementable myInterface, TCan
return implementation;
}

protected void ensureImplementedInterfaces(ITypeBinding bnd, TType fmx, TWithTypes owner, TAssociation lastAssociation) {
public void ensureImplementedInterfaces(ITypeBinding bnd, TType fmx, TWithTypes owner, TAssociation lastAssociation) {
for (ITypeBinding intbnd : bnd.getInterfaces()) {
TType superTyp;
if(intbnd.isClass()){
Expand Down Expand Up @@ -1506,25 +1507,37 @@ public <T extends TWithTypes & TNamedEntity> TParametricEntity ensureFamixParame
}else {
fmx =(ParametricClass) ensureFamixParametricClass(bnd, name, owner);
}
TConcretization Concretization = this.ensureFamixConcretization(generic, fmx);
TConcretization concretization = this.ensureFamixConcretization(generic, fmx);

ITypeBinding[] genericTypeArguments = bnd.getTypeDeclaration().getTypeParameters();
ITypeBinding[] typeArguments = bnd.getTypeArguments();
for (int i=0; typeArguments.length>i; i++) {


for (int i=0; i<typeArguments.length; i++) {
ITypeBinding tp = typeArguments[i];
ParameterType genType;
// Sometimes we don't have genericTypeArguments because the generic is stub
if(genericTypeArguments.length == typeArguments.length) {
genType = this.ensureFamixParameterType( genericTypeArguments[i], null, null);
}else {
genType = this.ensureFamixParameterType(null, "Stub"+(i+1), generic);
genType.addGenericEntities(generic);
}

if(tp.isClass() || tp.isInterface() || tp.isWildcardType()) {
Type fmxParam = (Type) this.ensureFamixType(tp);
ParameterType genType = this.ensureFamixParameterType( genericTypeArguments[i], null, null);


TParameterConcretization pConcretization = this.ensureFamixParameterConcretization(genType, fmxParam);
Concretization.addParameterConcretizations(pConcretization);
concretization.addParameterConcretizations(pConcretization);
genType.addConcretizations(pConcretization);
fmxParam.addConcreteEntities(fmx);
}else if(tp.isTypeVariable()) {
if(tp.getDeclaringMethod() != null || (tp.getDeclaringClass() != null && tp.getDeclaringClass() != bnd.getErasure())){
ParameterType fmxParam = this.ensureFamixParameterType(tp, null, null);
ParameterType genType = this.ensureFamixParameterType( genericTypeArguments[i], null, null);

TParameterConcretization pConcretization = this.ensureFamixParameterConcretization(genType, fmxParam);
Concretization.addParameterConcretizations(pConcretization);
concretization.addParameterConcretizations(pConcretization);
genType.addConcretizations(pConcretization);
fmxParam.addConcreteEntities(fmx);
}else {
Expand All @@ -1535,7 +1548,7 @@ public <T extends TWithTypes & TNamedEntity> TParametricEntity ensureFamixParame
}
}

fmx.setGenericization(Concretization);
fmx.setGenericization(concretization);
}

// --------------- stub: same as ParameterizableClass
Expand Down Expand Up @@ -1887,15 +1900,15 @@ public ParameterType ensureFamixParameterType(ITypeBinding bnd, String name, TPa
// --------------- create
if (fmx == null) {
fmx = ensureFamixEntity(ParameterType.class, bnd, name);
if(bnd.getSuperclass() != null) {
if(bnd != null && bnd.getSuperclass() != null) {
Inheritance inh = new Inheritance();
ITypeBinding supBnd = bnd.getSuperclass();
TWithInheritances superClass = (TWithInheritances)ensureFamixType(supBnd);
inh.setSuperclass(superClass);
inh.setSubclass(fmx);
fmx.addSuperInheritances(inh);
}
if(bnd.getInterfaces().length > 0) {
if(bnd != null && bnd.getInterfaces().length > 0) {
for(ITypeBinding intbnd: bnd.getInterfaces()) {
Inheritance inh = new Inheritance();
TWithInheritances superClass = (TWithInheritances)ensureFamixType(intbnd);
Expand Down Expand Up @@ -2722,7 +2735,12 @@ public Parameter ensureFamixParameter(IVariableBinding bnd, String name, Type ty
Parameter fmx = null;

// --------------- to avoid useless computations if we can
fmx = (Parameter)getEntityByKey(bnd);
try {
fmx = (Parameter)getEntityByKey(bnd);

}catch(Throwable e) {
e.printStackTrace();
}
if (fmx != null) {
return fmx;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,27 +149,22 @@ public void endVisit(Initializer node) {
protected void ensureInheritances(ITypeBinding bnd, TWithInheritances fmx) {
TAssociation lastInheritance = null;

if (bnd.isInterface()) {
return;
}

// --------------- superclass
if (!bnd.isInterface()) {
ITypeBinding supbnd = bnd.getSuperclass();
TType t;
if (supbnd != null) {
t = dico.ensureFamixType(supbnd);
} else {
t = dico.ensureFamixClassObject(null);
}
lastInheritance = dico.ensureFamixInheritance((TWithInheritances) t, fmx, lastInheritance);

for (ITypeBinding intbnd : bnd.getInterfaces()) {
TImplementable interfac = (TImplementable) dico.ensureFamixType(intbnd, /*ctxt*/(TWithTypes) context.top());
// If "implementor" is an interface, then relation is an inheritance
if(fmx instanceof Interface ) {
dico.ensureFamixInheritance((Interface)interfac, fmx, lastInheritance);
} else {
dico.ensureFamixImplementation(interfac, (TCanImplement)fmx, lastInheritance);
}
}
ITypeBinding supbnd = bnd.getSuperclass();
TType t;
if (supbnd != null) {
t = dico.ensureFamixType(supbnd);
} else {
t = dico.ensureFamixClassObject(null);
}
lastInheritance = dico.ensureFamixInheritance((TWithInheritances) t, fmx, lastInheritance);

// --------------- interfaces
dico.ensureImplementedInterfaces(bnd, (TType)fmx, null, lastInheritance);
}

}
18 changes: 13 additions & 5 deletions tests/fr/inria/verveine/extractor/java/VerveineJTest_Generics.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,6 @@ public void testParametricMethodConcretization() {
assertNotNull(Concretization);

assertEquals(1, Concretization.getParameterConcretizations().size());


}

@Test
Expand All @@ -179,8 +177,6 @@ public void testParametricMethodInvocation() {

ParametricMethod concreteMethod = ((ParametricMethod)firstElt(parametricMethod.getConcretizations()).getConcreteEntity());
assertEquals(1, concreteMethod.getIncomingInvocations().size());


}

@Test
Expand All @@ -191,21 +187,33 @@ public void testParameterTypeConcretization() {
assertNotNull(classC);
ParametricClass classD = (ParametricClass)genericEntityNamed("D");
assertNotNull(classD);
ParametricClass conCOfB = (ParametricClass)firstElt(classB.getSuperInheritances()).getSuperclass();
assertNotNull(conCOfB);
ParametricClass conCOfD = (ParametricClass)firstElt(classD.getSuperInheritances()).getSuperclass();
assertNotNull(conCOfD);

assertEquals(2, classC.getConcretizations().size());

// C<K, String> or C<E, String>
for (TConcretization c : classC.getConcretizations()) {
int i= 0;
ParametricClass pc = (ParametricClass)c.getConcreteEntity();
assertEquals(2, pc.getConcreteParameters().size());
// [K, String] or [E, String]
for(TConcreteParameterType p : pc.getConcreteParameters()) {
ParameterType genParam = (ParameterType) firstElt(p.getGenerics()).getGenericParameter();
assertNotNull(genParam);

if(i==0) {
assertEquals(ParameterType.class, genParam.getClass());
assertEquals(firstElt(genParam.getGenericEntities()), classC);
// p is a concrete parameter of C but also a generic parameter of B or D
ParametricClass genClass = (ParametricClass)firstElt(((ParameterType)p).getGenericEntities());
assertEquals(ParameterType.class, genParam.getClass());
assertTrue(genClass == classB || genClass == classD);

ParametricClass conClass = (ParametricClass)firstElt(((ParameterType)p).getConcreteEntities());
assertTrue(conClass == conCOfB || conClass == conCOfD);

}else {
assertEquals(Class.class, p.getClass());
}
Expand Down

0 comments on commit a0e9988

Please sign in to comment.