Skip to content

Commit

Permalink
Merge pull request #47 from Zir0-93/i46
Browse files Browse the repository at this point in the history
closes #46
  • Loading branch information
Zir0-93 authored Aug 27, 2016
2 parents b557ec4 + e7d5fce commit b96ec73
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 16 deletions.
33 changes: 17 additions & 16 deletions clarpse/src/main/java/com/clarity/listener/JavaTreeListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ private Component createComponent(Node node, ComponentType componentType) {
newCmp.setStartLine(String.valueOf(node.getBegin().line));
newCmp.setEndLine(String.valueOf(node.getEnd().line));
newCmp.setSourceFilePath(file.name());
newCmp.setCode(node.toStringWithoutComments());
return newCmp;
}

Expand All @@ -162,8 +163,8 @@ public final void visit(PackageDeclaration ctx, Object arg) {
currentImports.clear();
if (!componentStack.isEmpty()) {
System.out
.println("Clarity Java Listener found new package declaration while component stack not empty! component stack size is: "
+ componentStack.size());
.println("Clarity Java Listener found new package declaration while component stack not empty! component stack size is: "
+ componentStack.size());
}
super.visit(ctx, arg);
}
Expand Down Expand Up @@ -229,10 +230,10 @@ public final void visit(TypeParameter ctx, Object arg) {
for (final ClassOrInterfaceType type : ctx.getTypeBound()) {
for (final Type innerType : type.getTypeArgs()) {
currComponent
.insertComponentInvocation(new TypeReferenceInvocation(
resolveType(innerType
.toStringWithoutComments()), type
.getBegin().line));
.insertComponentInvocation(new TypeReferenceInvocation(
resolveType(innerType
.toStringWithoutComments()), type
.getBegin().line));
}
}
}
Expand Down Expand Up @@ -327,7 +328,7 @@ public final void visit(final MethodDeclaration ctx, Object arg) {
for (final ReferenceType stmt : ctx.getThrows()) {
currMethodCmp.insertComponentInvocation(new ThrownException(
resolveType(stmt.getType().toStringWithoutComments()), stmt
.getBegin().line));
.getBegin().line));
}
final String methodSignature = currMethodCmp.name()
+ formalParametersString;
Expand All @@ -346,8 +347,8 @@ public final void visit(final MethodDeclaration ctx, Object arg) {
methodParamCmp.setComponentName(generateComponentName(param
.getName()));
methodParamCmp
.setAccessModifiers(resolveJavaParserModifiers(param
.getModifiers()));
.setAccessModifiers(resolveJavaParserModifiers(param
.getModifiers()));
methodParamCmp.insertComponentInvocation(new TypeDeclaration(
resolveType(param.getType().toStringWithoutComments()),
param.getBegin().line));
Expand Down Expand Up @@ -403,7 +404,7 @@ public final void visit(final ConstructorDeclaration ctx, Object arg) {
for (final ReferenceType stmt : ctx.getThrows()) {
currMethodCmp.insertComponentInvocation(new ThrownException(
resolveType(stmt.getType().toStringWithoutComments()), stmt
.getBegin().line));
.getBegin().line));
}

final String methodSignature = currMethodCmp.name()
Expand All @@ -423,8 +424,8 @@ public final void visit(final ConstructorDeclaration ctx, Object arg) {
methodParamCmp.setComponentName(generateComponentName(param
.getName()));
methodParamCmp
.setAccessModifiers(resolveJavaParserModifiers(param
.getModifiers()));
.setAccessModifiers(resolveJavaParserModifiers(param
.getModifiers()));
methodParamCmp.insertComponentInvocation(new TypeDeclaration(
resolveType(param.getType().toStringWithoutComments()),
param.getBegin().line));
Expand Down Expand Up @@ -613,7 +614,7 @@ public final void visit(MethodCallExpr ctx, Object arg) {
new MethodInvocationSourceImpl("", ctx.getNameExpr()
.getName(), ctx.getBegin().line, ctx.getArgs()
.size(), srcModel, blockedInvocationSources),
currCmp));
currCmp));
ctx = (MethodCallExpr) ctx.getScope();
}

Expand All @@ -623,8 +624,8 @@ public final void visit(MethodCallExpr ctx, Object arg) {
retrieveContainingClassName(ctx.getScope(), ctx
.getNameExpr().getName()), ctx
.getNameExpr().getName(),
ctx.getBegin().line, ctx.getArgs().size(),
srcModel, blockedInvocationSources), currCmp));
ctx.getBegin().line, ctx.getArgs().size(),
srcModel, blockedInvocationSources), currCmp));
final MethodInvocationSourceChain methodChain = new MethodInvocationSourceChain(
methodSources, srcModel, blockedInvocationSources);
methodChain.process();
Expand Down Expand Up @@ -654,7 +655,7 @@ private String retrieveContainingClassName(Expression expression,
.componentInvocations(ComponentInvocations.EXTENSION)
.get(0).invokedComponent();
} else {
String name = expression.toStringWithoutComments();
final String name = expression.toStringWithoutComments();
// variable or static method call..
final Component variableComponent = findLocalSourceFileComponent(name);
if (variableComponent != null && variableComponent.name() != null) {
Expand Down
10 changes: 10 additions & 0 deletions clarpse/src/main/java/com/clarity/sourcemodel/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public final class Component implements Serializable {
*/
private String name;
private String comment;
private String code;
/**
* Source file path from which the component was derived.
*/
Expand Down Expand Up @@ -63,6 +64,7 @@ public Component(final Component component) {
start = component.startLine();
end = component.endLine();
sourceFile = component.sourceFile();
code = component.code();
}

public Component() {
Expand Down Expand Up @@ -254,4 +256,12 @@ public void setAccessModifiers(List<String> list) {
modifiers = list;

}

public String code() {
return code;
}

public void setCode(String code) {
this.code = code;
}
}
81 changes: 81 additions & 0 deletions clarpse/src/test/java/com/clarity/java/ComponentCodeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.clarity.java;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

import com.clarity.parser.ClarpseProject;
import com.clarity.parser.Lang;
import com.clarity.parser.ParseRequestContent;
import com.clarity.parser.RawFile;
import com.clarity.sourcemodel.OOPSourceCodeModel;

public class ComponentCodeTest {

@Test
public void testClassLevelCode() throws Exception {

final String code = "public class Test { @Override Test(String str) { Object localVar;} @Deprecated interface Cakes { abstract void tester(); } }";
final ParseRequestContent rawData = new ParseRequestContent(Lang.JAVA);
rawData.insertFile(new RawFile("file2.java", code));
final ClarpseProject parseService = new ClarpseProject(rawData);
final OOPSourceCodeModel generatedSourceModel = parseService.result();
assertTrue(generatedSourceModel.getComponent("Test").code().trim().replaceAll("[\\n\\t\\r ]", "")
.equalsIgnoreCase(code.trim().replaceAll("[\\n\\t ]", "")));
}

@Test
public void testMethodLevelCode() throws Exception {

final String code = "public class Test { @Override Test(String str) { Object localVar;} @Deprecated interface Cakes { abstract void tester(); } }";
final ParseRequestContent rawData = new ParseRequestContent(Lang.JAVA);
rawData.insertFile(new RawFile("file2.java", code));
final ClarpseProject parseService = new ClarpseProject(rawData);
final OOPSourceCodeModel generatedSourceModel = parseService.result();
assertTrue(generatedSourceModel.getComponent("Test.Test(java.lang.String)").code().trim()
.replaceAll("[\\n\\t\\r ]", "")
.equalsIgnoreCase("@Override Test(String str) { Object localVar;}".trim().replaceAll("[\\n\\t ]", "")));
}

@Test
public void testMethodParamLevelCode() throws Exception {

final String code = "public class Test { @Override Test(String str) { Object localVar;} @Deprecated interface Cakes { abstract void tester(); } }";
final ParseRequestContent rawData = new ParseRequestContent(Lang.JAVA);
rawData.insertFile(new RawFile("file2.java", code));
final ClarpseProject parseService = new ClarpseProject(rawData);
final OOPSourceCodeModel generatedSourceModel = parseService.result();
assertTrue(generatedSourceModel.getComponent("Test.Test(java.lang.String).str").code().trim()
.replaceAll("[\\n\\t\\r ]", "").equalsIgnoreCase("String str".trim().replaceAll("[\\n\\t ]", "")));
}

@Test
public void testInterfaceLevelCode() throws Exception {

final String code = "public class Test { @Override Test(String str) { Object localVar;} @Deprecated interface Cakes { abstract void tester(); } }";
final ParseRequestContent rawData = new ParseRequestContent(Lang.JAVA);
rawData.insertFile(new RawFile("file2.java", code));
final ClarpseProject parseService = new ClarpseProject(rawData);
final OOPSourceCodeModel generatedSourceModel = parseService.result();
assertTrue(generatedSourceModel
.getComponent("Test.Cakes")
.code()
.trim()
.replaceAll("[\\n\\t\\r ]", "")
.equalsIgnoreCase(
"@Deprecated interface Cakes { abstract void tester(); }".trim().replaceAll("[\\n\\t ]", "")));
}

@Test
public void testInterfaceMethodLevelCode() throws Exception {

final String code = "public class Test { @Override Test(String str) { Object localVar;} @Deprecated interface Cakes { abstract void tester(); } }";
final ParseRequestContent rawData = new ParseRequestContent(Lang.JAVA);
rawData.insertFile(new RawFile("file2.java", code));
final ClarpseProject parseService = new ClarpseProject(rawData);
final OOPSourceCodeModel generatedSourceModel = parseService.result();
assertTrue(generatedSourceModel.getComponent("Test.Cakes.tester()").code().trim().replaceAll("[\\n\\t ]", "")
.equalsIgnoreCase("abstract void tester();".trim().replaceAll("[\\n\\t\\r ]", "")));
}

}

0 comments on commit b96ec73

Please sign in to comment.