Skip to content

Commit

Permalink
Merge pull request #16 from moosetechnology/create-invocation-for-ano…
Browse files Browse the repository at this point in the history
…nymous-constructor-invoc

Create invocation for anonymous constructor invoc
  • Loading branch information
badetitou authored Nov 6, 2020
2 parents 67b9ce8 + c39d137 commit 96d14ee
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 42 deletions.
Binary file modified lib/verveine.extractor.java.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import fr.inria.verveine.extractor.java.JavaDictionary;
import fr.inria.verveine.extractor.java.VerveineJParser.anchorOptions;
import fr.inria.verveine.extractor.java.utils.NodeTypeChecker;
import fr.inria.verveine.extractor.java.utils.Util;
import fr.inria.verveine.extractor.java.visitors.GetVisitedEntityAbstractVisitor;

import org.eclipse.jdt.core.dom.*;
Expand Down Expand Up @@ -86,18 +87,30 @@ public void endVisit(TypeDeclaration node) {
*/
public boolean visit(ClassInstanceCreation node) {
visitClassInstanceCreation(node);
if ( (node.getAnonymousClassDeclaration() == null) && (!classSummary) ) {
Type clazz = node.getType();
eu.synectique.verveine.core.gen.famix.Type fmx = referedType(clazz, (ContainerEntity) context.top(), true);
if ((!classSummary)) {

// create an invocation to the constructor
String typName;
if (fmx == null) {
typName = findTypeName(clazz);
eu.synectique.verveine.core.gen.famix.Type fmx;

if (node.getAnonymousClassDeclaration() != null) {
ITypeBinding bnd = node.resolveTypeBinding();
fmx =
this.dico.getFamixClass(bnd, Util.stringForAnonymousName(getAnonymousSuperTypeName(), context), /*owner*/(ContainerEntity)context.top());
typName = fmx.getName();
}
else {
typName = fmx.getName();

Type clazz = node.getType();
fmx = referedType(clazz, (ContainerEntity) context.top(), true);

// create an invocation to the constructor
if (fmx == null) {
typName = findTypeName(clazz);
} else {
typName = fmx.getName();
}
}

methodInvocation(node.resolveConstructorBinding(), typName, /*receiver*/null, /*methOwner*/fmx, node.arguments());
Invocation lastInvok = context.getLastInvocation();
if ( (anchors == anchorOptions.assoc)
Expand Down
16 changes: 13 additions & 3 deletions tests/fr/inria/verveine/extractor/java/VerveineJTest_Inner.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package fr.inria.verveine.extractor.java;


import eu.synectique.verveine.core.gen.famix.*;
import eu.synectique.verveine.core.gen.famix.Class;
import eu.synectique.verveine.core.gen.famix.IndexedFileAnchor;
import eu.synectique.verveine.core.gen.famix.Method;
import eu.synectique.verveine.core.gen.famix.NamedEntity;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.lang.Exception;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
Expand Down Expand Up @@ -56,4 +55,15 @@ public void testAnonymousClassArePatateAndCanard() {
assertEquals("_Anonymous(Patate)", classes.get(1).getName());
}

@Test
public void testInvocationsOfPatateAndCanardConstructor() {
parse(new String[] {"test_src/inner"});
List<Invocation> invocations = entitiesOfType(Invocation.class).stream()
.sorted(Comparator.comparing(anInvocation2 -> ((Invocation)anInvocation2).getSender().getName()))
.collect(Collectors.toList());
assertEquals(invocations.size(), 3);
assert(invocations.get(0).getSignature().startsWith("Canard("));
assert(invocations.get(1).getSignature().startsWith("Patate("));
}

}
49 changes: 17 additions & 32 deletions tests/fr/inria/verveine/extractor/java/VerveineJTest_LanModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,15 @@
package fr.inria.verveine.extractor.java;


import eu.synectique.verveine.core.gen.famix.*;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.lang.Exception;
import java.util.Collection;
import java.util.stream.Collectors;

import eu.synectique.verveine.core.gen.famix.Access;
import eu.synectique.verveine.core.gen.famix.AnnotationInstance;
import eu.synectique.verveine.core.gen.famix.AnnotationType;
import eu.synectique.verveine.core.gen.famix.Attribute;
import eu.synectique.verveine.core.gen.famix.BehaviouralEntity;
import eu.synectique.verveine.core.gen.famix.Comment;
import eu.synectique.verveine.core.gen.famix.ImplicitVariable;
import eu.synectique.verveine.core.gen.famix.IndexedFileAnchor;
import eu.synectique.verveine.core.gen.famix.Inheritance;
import eu.synectique.verveine.core.gen.famix.Invocation;
import eu.synectique.verveine.core.gen.famix.LocalVariable;
import eu.synectique.verveine.core.gen.famix.Method;
import eu.synectique.verveine.core.gen.famix.Namespace;
import eu.synectique.verveine.core.gen.famix.Parameter;
import eu.synectique.verveine.core.gen.famix.ParameterizableClass;
import eu.synectique.verveine.core.gen.famix.PrimitiveType;
import eu.synectique.verveine.core.gen.famix.SourceAnchor;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

/**
Expand Down Expand Up @@ -94,23 +79,23 @@ private void parseFile(String file) {

@Test
public void testEntitiesNumber() {
int nbClasses = 11+14+1; // 11+ Object,String,StringBuffer,PrintStream,System,AbstractStringBuilder,FilterOutputStream,OutputStream,Comparable,Serializable,Flushable,Appendable,CharSequence,Closeable, +(java7)AutoCloseable} + 1 Anonymous class IPrinter
int nbInherit =9+21+1;
int nbClasses = 11 + 14 + 1; // 11+ Object,String,StringBuffer,PrintStream,System,AbstractStringBuilder,FilterOutputStream,OutputStream,Comparable,Serializable,Flushable,Appendable,CharSequence,Closeable, +(java7)AutoCloseable} + 1 Anonymous class IPrinter
int nbInherit = 9 + 21 + 1;

if ( System.getProperty("java.version").startsWith("1.") &&
System.getProperty("java.version").charAt(2) >= '7' ) {
// class Autocloseable starting in Java 7
if (System.getProperty("java.version").startsWith("1.") &&
System.getProperty("java.version").charAt(2) >= '7') {
// class Autocloseable starting in Java 7
nbClasses++;
nbInherit++;
}

assertEquals( nbClasses, entitiesOfType( eu.synectique.verveine.core.gen.famix.Class.class).size());
assertEquals(3, entitiesOfType( PrimitiveType.class).size());//int,boolean,void
assertEquals(40+8, entitiesOfType( Method.class).size());//40+{System.out.println(),System.out.println(...),System.out.print,StringBuffer.append,Object.equals,String.equals,Object.toString,<Initializer>}
assertEquals(nbClasses, entitiesOfType(eu.synectique.verveine.core.gen.famix.Class.class).size());
assertEquals(3, entitiesOfType(PrimitiveType.class).size());//int,boolean,void
assertEquals(40+8+1, entitiesOfType( Method.class).size());//40+{System.out.println(),System.out.println(...),System.out.print,StringBuffer.append,Object.equals,String.equals,Object.toString,<Initializer>} + Call to the constructor of anonymous IPrinter
assertEquals(10+1, entitiesOfType( Attribute.class).size());//10+{System.out}
assertEquals(2+4, entitiesOfType( Namespace.class).size());//2+{moose,java.lang,java.io,java}
assertEquals(26, entitiesOfType( Parameter.class).size());
assertEquals(54, entitiesOfType( Invocation.class).size());
assertEquals(55, entitiesOfType( Invocation.class).size());
assertEquals(nbInherit, entitiesOfType( Inheritance.class).size());
assertEquals(45, entitiesOfType( Access.class).size());// 17 "internal" attributes + 9 System.out + 18 "this" + 1 "super"
assertEquals(0, entitiesOfType( LocalVariable.class).size());
Expand Down Expand Up @@ -182,12 +167,12 @@ public void testAnonymous() {
eu.synectique.verveine.core.gen.famix.Class clazz = detectFamixElement(eu.synectique.verveine.core.gen.famix.Class.class, "_Anonymous(IPrinter)");
assertNotNull(clazz);
assertEquals("_Anonymous(IPrinter)", clazz.getName());
assertEquals(1, clazz.numberOfMethods());
assertEquals(2, clazz.numberOfMethods()); // the method print and the stub constructor
assertEquals(0, clazz.numberOfAttributes());
assertSame(detectFamixElement(Method.class, "PrintServer"), clazz.getContainer());
assertFalse(clazz.getIsInterface());

Method mth = firstElt(clazz.getMethods());
Method mth = firstElt(clazz.getMethods().stream().filter(aMethod -> !aMethod.getIsStub()).collect(Collectors.toList()));
assertEquals("print", mth.getName());
assertEquals(1, mth.getOutgoingReferences().size()); // System
assertEquals(1, mth.getAccesses().size()); // out
Expand Down

0 comments on commit 96d14ee

Please sign in to comment.