Skip to content

Commit

Permalink
Merge pull request #7 from gpes/fix/estrutura
Browse files Browse the repository at this point in the history
Fix/estrutura
  • Loading branch information
ricardojob authored Oct 5, 2017
2 parents 8ddfa8e + 6c1ce01 commit f2f9247
Show file tree
Hide file tree
Showing 110 changed files with 1,245 additions and 4,730 deletions.
19 changes: 14 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
/nbproject
.DS_Store

# Maven
/target/
parse-jdt/target/
parse-javaparser/target/
**/target/
.DS_Store
**/target/

# Netbeans
/nbproject
nb-configuration.xml

# Eclipse
**/.settings
**/.project
**/.classpath
/parse-jdt/nbproject/

# IDEA
**/target
.idea/**
**/*.iml
43 changes: 43 additions & 0 deletions mcp-core-jdt/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>br.edu.ifpb.gpes.mcp</groupId>
<artifactId>mcp</artifactId>
<version>0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>mcp-core-jdt</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>br.edu.ifpb.gpes.mcp</groupId>
<artifactId>mcp-core</artifactId>
<version>0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>mcp-samples</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.tycho</groupId>
<artifactId>org.eclipse.jdt.core</artifactId>
<version>3.10.0.v20140604-1726</version>
</dependency>
<dependency>
<groupId>org.eclipse.core</groupId>
<artifactId>org.eclipse.core.runtime</artifactId>
<version>3.7.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.core</groupId>
<artifactId>org.eclipse.core.resources</artifactId>
<version>3.7.100</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,31 @@
* @mail [email protected]
* @since 02/06/2017, 14:49:57
*/
//TODO: alterar nome da classe
public class SmartASTParser {
public class DefaultASTParser {

private final ASTParser parser = ASTParser.newParser(AST.JLS8);
private final String[] sources;
private final String[] classpath = {System.getProperty("java.home") + "/lib/rt.jar"};

public static SmartASTParser createParse(String[] sources) {
public static DefaultASTParser createParse(String[] sources) {

if (sources == null) {
throw new IllegalArgumentException("sources is null");
}

return new SmartASTParser(sources);
return new DefaultASTParser(sources);
}

public static SmartASTParser from(String... sources) {
public static DefaultASTParser from(String... sources) {

if (sources == null) {
throw new IllegalArgumentException("path is null");
}

return new SmartASTParser(sources);
return new DefaultASTParser(sources);
}

private SmartASTParser(String[] sources) {
private DefaultASTParser(String[] sources) {
this.sources = sources;
this.parser.setKind(ASTParser.K_COMPILATION_UNIT);
Hashtable options = JavaCore.getOptions();
Expand All @@ -63,14 +62,14 @@ public void updateUnitName(Path fileJava) {
this.parser.setEnvironment(classpath, sources, new String[]{"UTF-8"}, true);
this.parser.setSource(str.toCharArray());
} catch (IOException ex) {
Logger.getLogger(SmartASTParser.class.getName()).log(Level.SEVERE, null, ex);
Logger.getLogger(DefaultASTParser.class.getName()).log(Level.SEVERE, null, ex);
}
}

public void acceptVisitor(ASTVisitor visitor) {
ASTNode createAST = parser.createAST(null);
if (createAST.getAST().hasBindingsRecovery()) {
Logger.getLogger(SmartASTParser.class.getName()).log(Level.INFO, "Binding activated.");
Logger.getLogger(DefaultASTParser.class.getName()).log(Level.INFO, "Binding activated.");
}
createAST.accept(visitor);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ifpb.gpes.jdt;


import ifpb.gpes.Call;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -17,21 +18,21 @@
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation;

public class SmartAllVisitor extends ASTVisitor {
public class DefaultVisitor extends ASTVisitor {

private List<Call> ns;// = new ArrayList<>();
private List<Call> calls;// = new ArrayList<>();

private MethodDeclaration currentMethodDeclaration;
private Expression currentExpression;
//TODO: criar uma classe para fazer substituir
private final Stack<MethodDeclaration> stackMethodDeclaration = new Stack<>();

public SmartAllVisitor() {
public DefaultVisitor() {
this(new ArrayList<>());
}

public SmartAllVisitor(List<Call> elements) {
this.ns = elements;
public DefaultVisitor(List<Call> elements) {
this.calls = elements;
}

@Override
Expand Down Expand Up @@ -76,7 +77,7 @@ public boolean visit(ExpressionMethodReference node) {
no.setInvokedBy(ex.toString());
//"Nem todos os que vagueiam estao perdidos"

ns.add(no);
calls.add(no);

return super.visit(node);
}
Expand Down Expand Up @@ -140,15 +141,15 @@ public boolean visit(MethodInvocation mi) {
no.setCalledInMethod(calledInMethodLambda);
}

int count = ns.size();
int count = calls.size();

//TODO: podemos usar a ideia do Stack para analisar?
no.setInvokedBy(updateInv(mi));

String methodInvocation = getMethodInvocation(count, mi.getName().toString());

no.setCallMethod(methodInvocation);
ns.add(no);
calls.add(no);
return super.visit(mi);
}

Expand Down Expand Up @@ -184,11 +185,11 @@ private String getMethodInvocation(int count, String methodName) {
return null;
}

if (!ns.get(count - 1).getInvokedBy().contains(methodName)) {
if (!calls.get(count - 1).getInvokedBy().contains(methodName)) {
return null;
}

return ns.get(count - 1).getMethodName();
return calls.get(count - 1).getMethodName();
}

private String fillMethodName(String methodName, ITypeBinding[] bindings) {
Expand All @@ -202,12 +203,12 @@ private String fillMethodName(String methodName, ITypeBinding[] bindings) {
}

public List<Call> methodsCall() {
return Collections.unmodifiableList(ns);
return Collections.unmodifiableList(calls);
}

public List<Call> methodsCallFilter() {
return Collections
.unmodifiableList(ns.stream()
.unmodifiableList(calls.stream()
.filter(t -> t.getCallMethod() != null || "null".equalsIgnoreCase(t.getCallMethod()))
.collect(Collectors.toList()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ifpb.gpes.jdt;


import ifpb.gpes.ParseStrategy;
import org.eclipse.jdt.core.dom.ASTVisitor;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ifpb.gpes.jdt;


import ifpb.gpes.Call;
import ifpb.gpes.ParseStrategy;
import ifpb.gpes.Project;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public class SmartParseJDT implements ParseJDT {

//TODO: isso não está bom. Just do It!
public SmartParseJDT() {
this.elements = new ArrayList<>();
this.visitor = new SmartAllVisitor(elements);
this.visitor = new DefaultVisitor(elements);
}

private SmartParseJDT(ASTVisitor visitor) {
Expand All @@ -32,7 +31,7 @@ public static ParseJDT of(ASTVisitor visitor) {

@Override
public List<Call> from(Project project) {
SmartASTParser parser = SmartASTParser.from(project.sources());
DefaultASTParser parser = DefaultASTParser.from(project.sources());

project.files().forEach(p -> {
parser.updateUnitName(p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
*
* @author Ricardo Job
*/
public class ProjectTest {
public class ProjectWithJDTTest {

private static final Logger logger = Logger.getLogger(ProjectTest.class.getName());
private static final Logger logger = Logger.getLogger(ProjectWithJDTTest.class.getName());
private static final String sources = "../mcp-samples/src/main/java/";

@Test
public void testProjectDefault() {
Project project = Project
.root(SingletonPath.ROOT)
.path("src/test/java/ifpb/gpes/jdt/samples/AnonymousClass.java") // root
.sources("src/test/java/") // root - não obrigatorio
.root("")
.path(sources + "ifpb/gpes/domain/AnonymousClass.java") // root
.sources(sources) // root - não obrigatorio
.filter(".java");

Parse parse = Parse
Expand All @@ -41,9 +42,9 @@ public void testProjectDefault() {
@Test
public void testProjectOtherVisit() {
Project project = Project
.root(SingletonPath.ROOT)
.path("src/test/java/ifpb/gpes/jdt/samples/AnonymousClass.java") // root
.sources("src/test/java/") // root - não obrigatorio
.root("")
.path(sources + "ifpb/gpes/domain/AnonymousClass.java") // root
.sources(sources)// root - não obrigatorio
.filter(".java");

PrintASTVisitor visit = new PrintASTVisitor();
Expand Down
70 changes: 70 additions & 0 deletions mcp-core-jdt/src/test/java/ifpb/gpes/jdt/AnonymousClassTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package ifpb.gpes.jdt;

import ifpb.gpes.Call;
import ifpb.gpes.Parse;
import ifpb.gpes.Project;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.hamcrest.CoreMatchers.*;
import org.hamcrest.collection.IsIterableContainingInOrder;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

public class AnonymousClassTest {

private final List<Call> result = ofAnonymousClass();
private static final Logger logger = Logger.getLogger(AnonymousClassTest.class.getName());
private static final String sources = "../mcp-samples/src/main/java/";

@Test
public void testM1() {
List<Call> expected = ofListM1();

assertThat(result, hasItems(Call.of("java.util.List", "add[ifpb.gpes.domain.HasJCFObject]", "boolean", "ifpb.gpes.domain.SampleObject", "m2[]", null),
Call.of("java.util.List", "remove[java.lang.Object]", "boolean", "ifpb.gpes.domain.SampleObject", "m3[]", null),
Call.of("ifpb.gpes.domain.HasJCFObject", "getElements[]", "java.util.List<ifpb.gpes.domain.HasJCFObject>", "ifpb.gpes.domain.SampleObject", "m3[]", "isEmpty[]"),
Call.of("java.util.List", "hashCode[]", "int", "ifpb.gpes.domain.AnonymousClass", "m1[]", null),
Call.of("ifpb.gpes.domain.HasJCFObject", "getElements[]", "java.util.List<ifpb.gpes.domain.HasJCFObject>", "ifpb.gpes.domain.AnonymousClass", "m1[]", "hashCode[]")
));

assertEquals(result.size(), expected.size());
assertThat(result, is(expected));
assertThat(result, IsIterableContainingInOrder.contains(expected.toArray()));

result.forEach(no -> logger.log(Level.INFO, no.callGraph()));

}

private List<Call> ofListM1() {
return Arrays.asList(Call.of("java.util.List", "add[ifpb.gpes.domain.HasJCFObject]", "boolean", "ifpb.gpes.domain.SampleObject", "m2[]", null),
Call.of("ifpb.gpes.domain.HasJCFObject", "getElements[]", "java.util.List<ifpb.gpes.domain.HasJCFObject>", "ifpb.gpes.domain.SampleObject", "m2[]", "add[ifpb.gpes.domain.HasJCFObject]"),
Call.of("java.util.List", "remove[java.lang.Object]", "boolean", "ifpb.gpes.domain.SampleObject", "m3[]", null),
Call.of("ifpb.gpes.domain.HasJCFObject", "getElements[]", "java.util.List<ifpb.gpes.domain.HasJCFObject>", "ifpb.gpes.domain.SampleObject", "m3[]", "remove[java.lang.Object]"),
Call.of("java.util.List", "isEmpty[]", "boolean", "ifpb.gpes.domain.SampleObject", "m3[]", null),
Call.of("ifpb.gpes.domain.HasJCFObject", "getElements[]", "java.util.List<ifpb.gpes.domain.HasJCFObject>", "ifpb.gpes.domain.SampleObject", "m3[]", "isEmpty[]"),
Call.of("java.util.List", "iterator[]", "java.util.Iterator<ifpb.gpes.domain.HasJCFObject>", "ifpb.gpes.domain.SampleObject", "m2[]", null),
Call.of("ifpb.gpes.domain.HasJCFObject", "getElements[]", "java.util.List<ifpb.gpes.domain.HasJCFObject>", "ifpb.gpes.domain.SampleObject", "m2[]", "iterator[]"),
Call.of("java.util.List", "listIterator[]", "java.util.ListIterator<ifpb.gpes.domain.HasJCFObject>", "ifpb.gpes.domain.SampleObject", "m2[]", null),
Call.of("ifpb.gpes.domain.HasJCFObject", "getElements[]", "java.util.List<ifpb.gpes.domain.HasJCFObject>", "ifpb.gpes.domain.SampleObject", "m2[]", "listIterator[]"),
Call.of("java.util.List", "iterator[]", "java.util.Iterator<ifpb.gpes.domain.HasJCFObject>", "ifpb.gpes.domain.Interface", "semRetorno[]", null),
Call.of("ifpb.gpes.domain.HasJCFObject", "getElements[]", "java.util.List<ifpb.gpes.domain.HasJCFObject>", "ifpb.gpes.domain.Interface", "semRetorno[]", "iterator[]"),
Call.of("java.util.List", "hashCode[]", "int", "ifpb.gpes.domain.AnonymousClass", "m1[]", null),
Call.of("ifpb.gpes.domain.HasJCFObject", "getElements[]", "java.util.List<ifpb.gpes.domain.HasJCFObject>", "ifpb.gpes.domain.AnonymousClass", "m1[]", "hashCode[]"));

}

private List<Call> ofAnonymousClass() {
Project project = Project
.root("")
.path(sources + "ifpb/gpes/domain/AnonymousClass.java") // root
.sources(sources) // root - não obrigatorio
.filter(".java");

return Parse.with(ParseStrategies.JDT).from(project);

}

}
Loading

0 comments on commit f2f9247

Please sign in to comment.