Skip to content

Commit

Permalink
Merge pull request #1960 from jplag/feature/tokenPositionTestsJava_An…
Browse files Browse the repository at this point in the history
…notations

Added token position tests for annotations
  • Loading branch information
tsaglam authored Dec 3, 2024
2 parents 862f483 + caa1eb1 commit 631911f
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.junit.jupiter.api.AfterAll;
Expand Down Expand Up @@ -247,7 +248,7 @@ final List<TokenPositionTestData> getTokenPositionTestData() {
@MethodSource("getAllTestData")
@DisplayName("Test that the tokens map to ascending line numbers")
final void testMonotoneTokenOrder(TestData data) throws ParsingException, IOException {
List<Token> tokens = parseTokens(data);
List<Token> tokens = parseTokens(data).stream().filter(it -> !getIgnoredTokensForMonotoneTokenOrder().contains(it.getType())).toList();

for (int i = 0; i < tokens.size() - 2; i++) {
Token first = tokens.get(i);
Expand Down Expand Up @@ -338,4 +339,8 @@ private <T, C extends Collection<T>> C ignoreEmptyTestType(C data) {
protected File getTestFileLocation() {
return new File(DEFAULT_TEST_CODE_PATH_BASE.toFile(), this.language.getIdentifier());
}

protected List<TokenType> getIgnoredTokensForMonotoneTokenOrder() {
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ public Void visitClass(ClassTree node, Void unused) {
}
}

long start = positions.getStartPosition(ast, node);
long start = positions.getEndPosition(ast, node.getModifiers()) + 1;
long nameLength = node.getSimpleName().length();
long end = positions.getEndPosition(ast, node) - 1;
CodeSemantics semantics = CodeSemantics.createControl();
if (node.getKind() == Tree.Kind.ENUM) {
Expand All @@ -146,7 +147,9 @@ public Void visitClass(ClassTree node, Void unused) {
} else if (node.getKind() == Tree.Kind.RECORD) {
addToken(JavaTokenType.J_RECORD_BEGIN, start, 1, semantics);
} else if (node.getKind() == Tree.Kind.ANNOTATION_TYPE) {
addToken(JavaTokenType.J_ANNO_T_BEGIN, start, 10, semantics);
// The start position for the is calculated that way, because the @ is the final element in the modifier list for
// annotations
addToken(JavaTokenType.J_ANNO_T_BEGIN, start - 2, (start - 2) + 11 + nameLength, semantics);
} else if (node.getKind() == Tree.Kind.CLASS) {
addToken(JavaTokenType.J_CLASS_BEGIN, start, 5, semantics);
}
Expand Down Expand Up @@ -508,7 +511,8 @@ public Void visitMethodInvocation(MethodInvocationTree node, Void unused) {
@Override
public Void visitAnnotation(AnnotationTree node, Void unused) {
long start = positions.getStartPosition(ast, node);
addToken(JavaTokenType.J_ANNO, start, 1, new CodeSemantics());
String annotationName = node.getAnnotationType().toString();
addToken(JavaTokenType.J_ANNO, start, annotationName.length() + 1, new CodeSemantics());
return super.visitAnnotation(node, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import static de.jplag.java.JavaTokenType.J_TRY_END;
import static de.jplag.java.JavaTokenType.J_VARDEF;

import java.util.List;

import de.jplag.TokenType;
import de.jplag.testutils.LanguageModuleTest;
import de.jplag.testutils.datacollector.TestDataCollector;
import de.jplag.testutils.datacollector.TestSourceIgnoredLinesCollector;
Expand Down Expand Up @@ -84,4 +87,9 @@ protected void configureIgnoredLines(TestSourceIgnoredLinesCollector collector)
collector.ignoreLinesByPrefix("})");
collector.ignoreByCondition(line -> line.contains("else") && !line.contains("if"));
}

@Override
protected List<TokenType> getIgnoredTokensForMonotoneTokenOrder() {
return List.of(JavaTokenType.J_ANNO);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
>@SuppressWarnings
$| J_ANNO 17
>class Anno {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
>@SuppressWarnings("SomeWarning")
$| J_ANNO 17
>class Anno {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
>class Anno {
> @Deprecated
$ | J_ANNO 11
> public void testFunction() {
> }
>}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
>import javax.annotation.processing.Generated;
>
>class Anno {
> public void testMethod(@Generated String param) {
$ | J_ANNO 10
> }
>}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
>public @interface MyAnnotation {}
$ | J_ANNO_T_BEGIN 23
$ | J_ANNO_T_END 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
>public @interface MyAnnotation {
$ | J_ANNO_T_BEGIN 23
>}
$| J_ANNO_T_END 1

0 comments on commit 631911f

Please sign in to comment.