Skip to content

Commit

Permalink
Merge pull request #56 from Zir0-93/i54i55
Browse files Browse the repository at this point in the history
I54i55
  • Loading branch information
Zir0-93 authored Sep 26, 2016
2 parents 4273608 + eec17fb commit 7652f7c
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 174 deletions.
4 changes: 2 additions & 2 deletions clarpse/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<groupId>com.clarity.parse</groupId>
<artifactId>clarpse</artifactId>
<name>clarpse</name>
<packaging>jar</packaging>
<version>2.3</version>
<packaging>jar</packaging>
<version>2.4</version>
<properties>
<antlr4.plugin.version>4.5.3</antlr4.plugin.version>
<antlr4.version>4.5.3</antlr4.version>
Expand Down
28 changes: 16 additions & 12 deletions clarpse/src/main/java/com/clarity/listener/JavaTreeListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.clarity.sourcemodel.OOPSourceModelConstants.AccessModifiers;
import com.clarity.sourcemodel.OOPSourceModelConstants.ComponentInvocations;
import com.clarity.sourcemodel.OOPSourceModelConstants.ComponentType;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.ImportDeclaration;
import com.github.javaparser.ast.Node;
Expand Down Expand Up @@ -67,12 +68,12 @@
*/
public class JavaTreeListener extends VoidVisitorAdapter {

private final Stack<Component> componentStack = new Stack<Component>();
private final ArrayList<String> currentImports = new ArrayList<String>();
private String currentPkg = "";
private final OOPSourceCodeModel srcModel;
private final Map<String, String> currentImportsMap = new HashMap<String, String>();
private final RawFile file;
private final Stack<Component> componentStack = new Stack<Component>();
private final ArrayList<String> currentImports = new ArrayList<String>();
private String currentPkg = "";
private final OOPSourceCodeModel srcModel;
private final Map<String, String> currentImportsMap = new HashMap<String, String>();
private final RawFile file;
// key = required component name, value = blocked invocation source
private volatile Map<String, List<InvocationSourceChain>> blockedInvocationSources;

Expand All @@ -92,6 +93,7 @@ public JavaTreeListener(final OOPSourceCodeModel srcModel, final RawFile file,

public void populateModel() throws IOException {

JavaParser.setDoNotConsiderAnnotationsAsNodeStartForCodeAttribution(false);
CompilationUnit cu;
ByteArrayInputStream in = null;
try {
Expand Down Expand Up @@ -166,15 +168,19 @@ private Component createComponent(Node node, ComponentType componentType) {
final Component newCmp = new Component();
newCmp.setPackageName(currentPkg);
newCmp.setComponentType(componentType);
String comment = "";
if (node.getComment() != null) {
newCmp.setComment(node.getComment().toString());
comment = node.getComment().toString();
// set the start line of the component to include the comment
// if the comment is before the annotations
if (node.getComment().getBegin().line < node.getBegin().line) {
newCmp.setStartLine(String.valueOf(node.getComment().getBegin().line));
}
}
if (newCmp.startLine() == null) {
newCmp.setStartLine(String.valueOf(node.getBegin().line));
}
newCmp.setStartLine(String.valueOf(node.getBegin().line));
newCmp.setEndLine(String.valueOf(node.getEnd().line));
newCmp.setSourceFilePath(file.name());
newCmp.setCode(comment + node.toStringWithoutComments());
return newCmp;
}

Expand Down Expand Up @@ -211,7 +217,6 @@ public final void visit(ClassOrInterfaceDeclaration ctx, Object arg) {
} else {
cmp = createComponent(ctx, OOPSourceModelConstants.ComponentType.CLASS_COMPONENT);
}
cmp.setCode(file.content());
cmp.setAccessModifiers(resolveJavaParserModifiers(ctx.getModifiers()));
cmp.setComponentName(generateComponentName(ctx.getName()));
cmp.setName(ctx.getName());
Expand Down Expand Up @@ -296,7 +301,6 @@ public final void visit(EnumDeclaration ctx, Object arg) {
enumCmp.setImports(currentImports);
enumCmp.setName(ctx.getName());
enumCmp.setAccessModifiers(resolveJavaParserModifiers(ctx.getModifiers()));
enumCmp.setCode(file.content());
pointParentsToGivenChild(enumCmp);
if (ctx.getJavaDoc() != null) {
enumCmp.setComment(ctx.getJavaDoc().toString());
Expand Down
20 changes: 20 additions & 0 deletions clarpse/src/main/java/com/clarity/parser/Lang.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package com.clarity.parser;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

/**
* Indicates Clarpse's currently supported languages.
*
Expand All @@ -9,6 +17,12 @@ public enum Lang {

JAVA("java", ".java");

private static Map<String, Lang> namesMap = new HashMap<String, Lang>(1);

static {
namesMap.put(JAVA.value, JAVA);
}

private String value;
private String fileExt;

Expand All @@ -17,6 +31,12 @@ private Lang(final String value, final String extension) {
fileExt = extension;
}

@JsonCreator
public static Lang forValue(String value) {
return namesMap.get(StringUtils.lowerCase(value));
}

@JsonValue
public String value() {
return value;
}
Expand Down
49 changes: 13 additions & 36 deletions clarpse/src/main/java/com/clarity/sourcemodel/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import com.clarity.invocation.ComponentInvocation;
import com.clarity.sourcemodel.OOPSourceModelConstants.ComponentInvocations;
import com.clarity.sourcemodel.OOPSourceModelConstants.ComponentType;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

/**
* Representation of the individual code level components (classes,
Expand All @@ -17,8 +15,7 @@
*
* @author Muntazir Fadhel
*/
@JsonFormat(shape = JsonFormat.Shape.ARRAY)
@JsonPropertyOrder(alphabetic = true)

public final class Component implements Serializable {

private static final long serialVersionUID = 1L;
Expand All @@ -35,7 +32,6 @@ 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 @@ -68,7 +64,6 @@ public Component(final Component component) {
start = component.startLine();
end = component.endLine();
sourceFile = component.sourceFile();
code = component.code();
comment = component.comment();
}

Expand Down Expand Up @@ -143,13 +138,11 @@ public String componentName() {
return componentName;
}

public void setDeclarationTypeSnippet(
final String componentDeclarationTypeFragment) {
public void setDeclarationTypeSnippet(final String componentDeclarationTypeFragment) {
declarationTypeSnippet = componentDeclarationTypeFragment;
}

public void setExternalTypeReferences(
final ArrayList<ComponentInvocation> externalReferences) {
public void setExternalTypeReferences(final ArrayList<ComponentInvocation> externalReferences) {
invocations = externalReferences;
}

Expand All @@ -166,8 +159,7 @@ public List<String> modifiers() {
}

public void insertAccessModifier(final String modifier) {
if (OOPSourceModelConstants.getJavaAccessModifierMap().containsValue(
modifier)) {
if (OOPSourceModelConstants.getJavaAccessModifierMap().containsValue(modifier)) {
modifiers.add(modifier.toLowerCase());
}
}
Expand Down Expand Up @@ -209,38 +201,30 @@ public String parentUniqueName() {
if (!type.isMethodComponent()) {
if (uniqueName().contains(".")) {
final int lastPeriod = uniqueName().lastIndexOf(".");
final String currParentClassName = uniqueName().substring(0,
lastPeriod);
final String currParentClassName = uniqueName().substring(0, lastPeriod);
return currParentClassName;
} else {
throw new IllegalArgumentException(
"Cannot get parent of component: " + uniqueName());
throw new IllegalArgumentException("Cannot get parent of component: " + uniqueName());
}
} else {
final int lastOpeningBracket = uniqueName().lastIndexOf("(");
final String methodComponentUniqueNameMinusParamters = uniqueName()
.substring(0, lastOpeningBracket);
final int lastPeriod = methodComponentUniqueNameMinusParamters
.lastIndexOf(".");
final String currParentClassName = methodComponentUniqueNameMinusParamters
.substring(0, lastPeriod);
final String methodComponentUniqueNameMinusParamters = uniqueName().substring(0, lastOpeningBracket);
final int lastPeriod = methodComponentUniqueNameMinusParamters.lastIndexOf(".");
final String currParentClassName = methodComponentUniqueNameMinusParamters.substring(0, lastPeriod);
return currParentClassName;
}
}

public void insertComponentInvocations(
final ArrayList<ComponentInvocation> externalClassTypeReferenceList) {
public void insertComponentInvocations(final ArrayList<ComponentInvocation> externalClassTypeReferenceList) {
for (final ComponentInvocation typeRef : externalClassTypeReferenceList) {
insertComponentInvocation(typeRef);
}
}

public List<ComponentInvocation> componentInvocations(
final ComponentInvocations type) {
public List<ComponentInvocation> componentInvocations(final ComponentInvocations type) {
final List<ComponentInvocation> tmpInvocations = new ArrayList<ComponentInvocation>();
for (final ComponentInvocation compInvocation : invocations) {
if (type.getMatchingClass().isAssignableFrom(
compInvocation.getClass())) {
if (type.getMatchingClass().isAssignableFrom(compInvocation.getClass())) {
tmpInvocations.add(compInvocation);
}
}
Expand All @@ -250,6 +234,7 @@ public List<ComponentInvocation> componentInvocations(
public List<ComponentInvocation> componentInvocations() {
return invocations;
}

public void setName(final String name) {
this.name = name;
}
Expand All @@ -268,12 +253,4 @@ public void setAccessModifiers(List<String> list) {
}

}

public String code() {
return code;
}

public void setCode(String code) {
this.code = code;
}
}
25 changes: 6 additions & 19 deletions clarpse/src/test/java/ClarpseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
import com.clarity.java.ChildComponentsTest;
import com.clarity.java.CommentsParsingTest;
import com.clarity.java.ComplexMethodInvocationsTest;
import com.clarity.java.ComponentCodeTest;
import com.clarity.java.ComponentExistTest;
import com.clarity.java.PackageAttributeTest;
import com.clarity.java.ComponentTypeTest;
import com.clarity.java.LineNumberAttributeTest;
import com.clarity.java.PackageAttributeTest;
import com.clarity.java.SimpleMethodInvocationsTest;
import com.clarity.java.TypeDeclarationTest;
import com.clarity.java.TypeExtensionTest;
Expand All @@ -20,23 +19,11 @@
* Clarpse's main test suite.
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({ComponentExistTest.class,
PackageAttributeTest.class,
ComponentTypeTest.class,
LineNumberAttributeTest.class,
SimpleMethodInvocationsTest.class,
RawFileComparisonTest.class,
ComponentSourceFilePathTest.class,
ComplexMethodInvocationsTest.class,
AnnotationInvocationTest.class,
TypeExtensionTest.class,
TypeImplementationTest.class,
TypeDeclarationTest.class,
ChildComponentsTest.class,
AccessModifiersTest.class,
CommentsParsingTest.class,
ComponentCodeTest.class
})
@Suite.SuiteClasses({ ComponentExistTest.class, PackageAttributeTest.class, ComponentTypeTest.class,
LineNumberAttributeTest.class, SimpleMethodInvocationsTest.class, RawFileComparisonTest.class,
ComponentSourceFilePathTest.class, ComplexMethodInvocationsTest.class, AnnotationInvocationTest.class,
TypeExtensionTest.class, TypeImplementationTest.class, TypeDeclarationTest.class, ChildComponentsTest.class,
AccessModifiersTest.class, CommentsParsingTest.class })
public class ClarpseTest {

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class CommentsParsingTest {
@Test
public void testClassLevelComment() throws Exception {

final String code = "package test; /** Licensing */ import lol; /**A \n comment*/ public class Test { }";
final String code = "package test; /** Licensing */ import lol; /**A \n comment \n */ public class Test { }";
final ParseRequestContent rawData = new ParseRequestContent(Lang.JAVA);
rawData.insertFile(new RawFile("file2.java", code));
final ClarpseProject parseService = new ClarpseProject(rawData);
Expand Down Expand Up @@ -75,7 +75,7 @@ public void testMethodLevelComment() throws Exception {
@Test
public void testInterfaceMethodLevelComment() throws Exception {

final String code = "public interface Test { /**lolcakes*/abstract void test();}";
final String code = "public interface Test { /**lol \n cakes \n */abstract void test();}";
final ParseRequestContent rawData = new ParseRequestContent(Lang.JAVA);
rawData.insertFile(new RawFile("file2.java", code));
final ClarpseProject parseService = new ClarpseProject(rawData);
Expand Down Expand Up @@ -105,7 +105,6 @@ public void testMethodParamLevelComment() throws Exception {
final ClarpseProject parseService = new ClarpseProject(rawData);
final OOPSourceCodeModel generatedSourceModel = parseService.result();
assertTrue(generatedSourceModel.getComponent("Test.aMethod(java.lang.String).methodParam").comment()
.replaceAll("[\\n\\t\\r ]", "")
.equalsIgnoreCase("/**lolcakes*/"));
.replaceAll("[\\n\\t\\r ]", "").equalsIgnoreCase("/**lolcakes*/"));
}
}
Loading

0 comments on commit 7652f7c

Please sign in to comment.