diff --git a/clarpse/src/main/java/com/clarity/listener/JavaTreeListener.java b/clarpse/src/main/java/com/clarity/listener/JavaTreeListener.java index 006e49e..a8abde8 100644 --- a/clarpse/src/main/java/com/clarity/listener/JavaTreeListener.java +++ b/clarpse/src/main/java/com/clarity/listener/JavaTreeListener.java @@ -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; } @@ -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); } @@ -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)); } } } @@ -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; @@ -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)); @@ -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() @@ -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)); @@ -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(); } @@ -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(); @@ -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) { diff --git a/clarpse/src/main/java/com/clarity/sourcemodel/Component.java b/clarpse/src/main/java/com/clarity/sourcemodel/Component.java index b10cd14..3b17c30 100644 --- a/clarpse/src/main/java/com/clarity/sourcemodel/Component.java +++ b/clarpse/src/main/java/com/clarity/sourcemodel/Component.java @@ -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. */ @@ -63,6 +64,7 @@ public Component(final Component component) { start = component.startLine(); end = component.endLine(); sourceFile = component.sourceFile(); + code = component.code(); } public Component() { @@ -254,4 +256,12 @@ public void setAccessModifiers(List list) { modifiers = list; } + + public String code() { + return code; + } + + public void setCode(String code) { + this.code = code; + } } diff --git a/clarpse/src/test/java/com/clarity/java/ComponentCodeTest.java b/clarpse/src/test/java/com/clarity/java/ComponentCodeTest.java new file mode 100644 index 0000000..8f24b89 --- /dev/null +++ b/clarpse/src/test/java/com/clarity/java/ComponentCodeTest.java @@ -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 ]", ""))); + } + +}