Skip to content

Commit

Permalink
Merge pull request #112 from moosetechnology/issue-109
Browse files Browse the repository at this point in the history
fix #109
  • Loading branch information
NicolasAnquetil authored Nov 13, 2024
2 parents d1c7247 + b9da5aa commit 42cf4f3
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 50 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 @@ -168,9 +168,7 @@ public void endVisit(TypeDeclaration node) {
@Override
public boolean visit(ClassInstanceCreation node) {
// System.err.println("TRACE, Visiting ClassInstanceCreation: " + node);
if (node.getAnonymousClassDeclaration() != null) {
anonymousSuperTypeName.push(Util.jdtTypeName(node.getType()));
}
possiblyAnonymousClassDeclaration(node);
return super.visit(node);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import org.eclipse.jdt.core.dom.*;
import org.moosetechnology.model.famix.famixjavaentities.ContainerEntity;
import org.moosetechnology.model.famix.famixjavaentities.Method;
import org.moosetechnology.model.famix.famixjavaentities.ParameterType;
import org.moosetechnology.model.famix.famixjavaentities.ParametricClass;
import org.moosetechnology.model.famix.famixjavaentities.Reference;
import org.moosetechnology.model.famix.famixtraits.TNamedEntity;
import org.moosetechnology.model.famix.famixtraits.TType;
Expand Down Expand Up @@ -70,18 +68,19 @@ public void endVisit(TypeDeclaration node) {
public boolean visit(ClassInstanceCreation node) {
possiblyAnonymousClassDeclaration(node);

//if (node.getAnonymousClassDeclaration() == null) {
Expression expr = node.getExpression();
if (expr != null) {
expr.accept(this);
}
for (Type typeArg : (List<Type>)node.typeArguments()) {
typeArg.accept(this);
}
for (Expression arg : (List<Expression>)node.arguments()) {
arg.accept(this);
}
//}
if (node.getExpression() != null) {
node.getExpression().accept(this);
}
for (Type typeArg : (List<Type>)node.typeArguments()) {
typeArg.accept(this);
}
for (Expression arg : (List<Expression>)node.arguments()) {
arg.accept(this);
}
if (node.getAnonymousClassDeclaration() != null) {
node.getAnonymousClassDeclaration().accept(this);
}

return false;
}

Expand Down Expand Up @@ -225,7 +224,7 @@ public boolean visit(InstanceofExpression node) {
@Override
public boolean visit(FieldDeclaration node) {
hasInitBlock(node); // to recover optional EntityDictionary.INIT_BLOCK_NAME method
visitVariableDeclaration((List<VariableDeclaration>)node.fragments(), node.getType()); // to create the TypeRefs
visitVariablesDeclaration((List<VariableDeclaration>)node.fragments(), node.getType()); // to create the TypeRefs
return true;
}

Expand All @@ -242,6 +241,21 @@ public void endVisit(EnumConstantDeclaration node) {
endVisitEnumConstantDeclaration(node);
}

/**
* SingleVariableDeclaration ::=
* { ExtendedModifier } Type {Annotation} [ ... ] Identifier { Dimension } [ = Expression ]
*/
@Override
public boolean visit(SingleVariableDeclaration node) {
setVariableDeclaredType(
node,
referedType(
node.getType(),
(org.moosetechnology.model.famix.famixjavaentities.Type) context.topType(),
false));
return true;
}

/**
* VariableDeclarationExpression ::=
* { ExtendedModifier } Type VariableDeclarationFragment
Expand All @@ -250,7 +264,7 @@ public void endVisit(EnumConstantDeclaration node) {
@SuppressWarnings("unchecked")
@Override
public boolean visit(VariableDeclarationExpression node) {
return visitVariableDeclaration((List<VariableDeclaration>)node.fragments(), node.getType());
return visitVariablesDeclaration((List<VariableDeclaration>)node.fragments(), node.getType());
}

/**
Expand All @@ -261,7 +275,7 @@ public boolean visit(VariableDeclarationExpression node) {
@SuppressWarnings("unchecked")
@Override
public boolean visit(VariableDeclarationStatement node) {
return visitVariableDeclaration((List<VariableDeclaration>)node.fragments(), node.getType());
return visitVariablesDeclaration((List<VariableDeclaration>)node.fragments(), node.getType());
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -308,25 +322,20 @@ public boolean visit(SimpleName node) {
* VariableDeclaration ::=
* SingleVariableDeclaration VariableDeclarationFragment
*/
private <T extends TWithTypes & TNamedEntity> boolean visitVariableDeclaration(List<VariableDeclaration> fragments, Type declType) {
setVariablesDeclaredType(fragments, referedType(declType, (T) context.topType(), false));
@SuppressWarnings("unchecked")
private <T extends TWithTypes & TNamedEntity> boolean visitVariablesDeclaration(List<VariableDeclaration> fragments, Type declType) {
for (VariableDeclaration varDecl : fragments) {
TType declaredType = referedType(declType, (T) context.topType(), false);
setVariableDeclaredType( varDecl, declaredType);
varDecl.accept(this);
}
return false;
}

// public boolean visit(SimpleName node) {
// IBinding bnd = node.resolveBinding();
// if ( (bnd != null) && (bnd instanceof ITypeBinding) ) {
// referedType((ITypeBinding) bnd, (ContainerEntity) context.top(), !((ITypeBinding) bnd).isEnum());

private void setVariablesDeclaredType(List<VariableDeclaration> vars, TType varTyp) {
for (VariableDeclaration var : vars) {
TTypedEntity fmx = (TTypedEntity) dico.getEntityByKey(var.resolveBinding());
if (fmx != null) {
fmx.setDeclaredType(varTyp);
}
protected void setVariableDeclaredType(VariableDeclaration var, TType varTyp) {
TTypedEntity fmx = (TTypedEntity) dico.getEntityByKey(var.resolveBinding());
if (fmx != null) {
fmx.setDeclaredType(varTyp);
}
}

Expand Down
54 changes: 37 additions & 17 deletions tests/fr/inria/verveine/extractor/java/VerveineJTest_AdHoc.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
package fr.inria.verveine.extractor.java;


import fr.inria.verveine.extractor.java.utils.Util;
import org.junit.Before;
import org.junit.Test;
import org.moosetechnology.model.famix.famixjavaentities.*;
import org.moosetechnology.model.famix.famixjavaentities.Class;
import org.moosetechnology.model.famix.famixjavaentities.Enum;
import org.moosetechnology.model.famix.famixjavaentities.Package;
import org.moosetechnology.model.famix.famixtraits.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.lang.Exception;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -23,9 +21,34 @@
import java.util.List;
import java.util.Set;

import javax.swing.JFrame;
import org.junit.Before;
import org.junit.Test;
import org.moosetechnology.model.famix.famixjavaentities.Access;
import org.moosetechnology.model.famix.famixjavaentities.Attribute;
import org.moosetechnology.model.famix.famixjavaentities.ContainerEntity;
import org.moosetechnology.model.famix.famixjavaentities.Enum;
import org.moosetechnology.model.famix.famixjavaentities.EnumValue;
import org.moosetechnology.model.famix.famixjavaentities.Interface;
import org.moosetechnology.model.famix.famixjavaentities.Invocation;
import org.moosetechnology.model.famix.famixjavaentities.LocalVariable;
import org.moosetechnology.model.famix.famixjavaentities.Method;
import org.moosetechnology.model.famix.famixjavaentities.Package;
import org.moosetechnology.model.famix.famixjavaentities.Parameter;
import org.moosetechnology.model.famix.famixjavaentities.ParametricClass;
import org.moosetechnology.model.famix.famixjavaentities.ParametricInterface;
import org.moosetechnology.model.famix.famixjavaentities.Type;
import org.moosetechnology.model.famix.famixtraits.TAccess;
import org.moosetechnology.model.famix.famixtraits.TAttribute;
import org.moosetechnology.model.famix.famixtraits.TEnumValue;
import org.moosetechnology.model.famix.famixtraits.TInvocation;
import org.moosetechnology.model.famix.famixtraits.TLocalVariable;
import org.moosetechnology.model.famix.famixtraits.TMethod;
import org.moosetechnology.model.famix.famixtraits.TNamedEntity;
import org.moosetechnology.model.famix.famixtraits.TParameter;
import org.moosetechnology.model.famix.famixtraits.TReference;
import org.moosetechnology.model.famix.famixtraits.TType;

import static org.junit.Assert.*;
import fr.inria.verveine.extractor.java.utils.Util;

/**
* @author Nicolas Anquetil
Expand Down Expand Up @@ -151,11 +174,8 @@ public void testConstructorInvocations() {
}

@ Test
/*
* issue https://github.com/moosetechnology/VerveineJ/issues/109
* "creates two dependencies for one "new" instruction
* instruction: new XYZ() results in an invocation of the constructor XYZ() + a reference to the type XYZ
* The second is a mistake
/* issue https://github.com/moosetechnology/VerveineJ/issues/109
* no longer create a Reference to the type for "new" instruction
*/
public void testNoReferenceOnNew() {
parse(new String[] {"test_src/ad_hoc/DefaultConstructor.java"});
Expand All @@ -164,7 +184,7 @@ public void testNoReferenceOnNew() {
assertNotNull(aMethod);

assertEquals( 3, aMethod.numberOfOutgoingInvocations());
/* new DefaultConstructor(); x.methodWithInstanceScope(); new JFrame("My title"); */
/* new DefaultConstructor(); x.methodWithInstanceScope(); new JFrame(...); */
assertEquals(0, aMethod.numberOfOutgoingReferences());
}

Expand Down

0 comments on commit 42cf4f3

Please sign in to comment.