Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comment text cut #21

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 46 additions & 10 deletions src/main/java/com/feenk/jdt2famix/injava/InJavaImporter.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.feenk.jdt2famix.injava;

import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
Expand All @@ -22,7 +24,6 @@
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.EnumConstantDeclaration;
import org.eclipse.jdt.core.dom.EnumDeclaration;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.FieldAccess;
import org.eclipse.jdt.core.dom.FieldDeclaration;
Expand Down Expand Up @@ -834,13 +835,25 @@ public void ensureCommentFromBodyDeclaration(SourcedEntity entity, BodyDeclarati
if (node.getJavadoc() != null)
createBasicComment(entity, node.getJavadoc().toString());
else {
//if there is no javadoc, we look for single line or multi line comments before the node
// if there is no javadoc, we look for single line or multi line
// comments before the node
RandomAccessFile source;
try {
source = new RandomAccessFile(this.currentFilePath, "r");
} catch (FileNotFoundException e) {
source = null;
e.printStackTrace();
}

CompilationUnit root = (CompilationUnit) node.getRoot();
int firstLeadingCommentIndex = root.firstLeadingCommentIndex(node);
if (firstLeadingCommentIndex >= 0)
//There seems to be a problem here: JDT does not seem to provide the contents of the comments.
//Only the types (one line or multi line).
createBasicComment(entity, root.getCommentList().get(firstLeadingCommentIndex).toString());
if (firstLeadingCommentIndex >= 0)
// There seems to be a problem here: JDT does not seem to
// provide the contents of the comments.
// Only the types (one line or multi line).
createBasicComment(entity,
(org.eclipse.jdt.core.dom.Comment) root.getCommentList().get(firstLeadingCommentIndex), source);

}
}
private void createBasicComment(SourcedEntity entity, String content) {
Expand All @@ -849,14 +862,37 @@ private void createBasicComment(SourcedEntity entity, String content) {
entity.addComments(comment);
repository.add(comment);
}

//UTILS


public void createBasicComment(SourcedEntity entity, org.eclipse.jdt.core.dom.Comment javaComment,
RandomAccessFile source) {
String commentContent = null;
if (javaComment != null) {
if (javaComment.isDocComment()) {
commentContent = javaComment.toString();
} else {
byte[] buffer = new byte[javaComment.getLength()];
try {
source.seek(javaComment.getStartPosition());
source.read(buffer, 0, javaComment.getLength());
commentContent = new String(buffer);
} catch (IOException e) {
e.printStackTrace();
return;
}

}
createBasicComment(entity, commentContent);

}
}

// UTILS

private void extractBasicModifiersFromBinding(int modifiers, NamedEntity entity) {
Boolean publicModifier = Modifier.isPublic(modifiers);
Boolean protectedModifier = Modifier.isProtected(modifiers);
Boolean privateModifier = Modifier.isPrivate(modifiers);
if (publicModifier )
if (publicModifier)
entity.addModifiers("public");
if (protectedModifier)
entity.addModifiers("protected");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.feenk.jdt2famix.injava.oneSample;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

Expand Down Expand Up @@ -39,10 +40,12 @@ public void testMethodWithOneLineComment() {
assertEquals(1, methodNamed("methodWithOneLineComment").getComments().size());
}

@Test(expected=AssertionError.class)
@Test
public void testMethodWithOneLineCommentIncludingALink() {
assertEquals(1, methodNamed("methodWithOneLineCommentIncludingALink").getComments().size());
assertEquals("//Method one line comment and link: http://feenk.com", methodNamed("methodWithOneLineCommentIncludingALink").getComments().stream().findAny().get().getContent());
assertEquals("//Method one line comment and link: http://feenk.com",
methodNamed("methodWithOneLineCommentIncludingALink").getComments().stream().findAny().get()
.getContent());
}

@Test
Expand All @@ -53,12 +56,7 @@ public void testMethodWithMultiLineComment() {
@Test
public void testMethodWithMultiLineCommentIncludingALink() {
assertEquals(1, methodNamed("methodWithMultiLineCommentIncludingALink").getComments().size());
assertTrue(methodNamed("methodWithMultiLineCommentIncludingALink").
getComments().
stream().
findAny().
get().
getContent().
contains("feenk.com"));
assertTrue(methodNamed("methodWithMultiLineCommentIncludingALink").getComments().stream().findAny().get()
.getContent().contains("feenk.com"));
}
}