Skip to content

Commit

Permalink
[java] RecordBody - test with more annotation and use deep lookahead
Browse files Browse the repository at this point in the history
Record constructors are not allowed to throw exceptions.
  • Loading branch information
adangel committed Feb 28, 2020
1 parent 0ecd1da commit 7d3df99
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
5 changes: 2 additions & 3 deletions pmd-java/etc/grammar/Java.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -1162,9 +1162,9 @@ void RecordBody():
void RecordBodyDeclaration() #void :
{}
{
LOOKAHEAD(4) RecordConstructorDeclaration()
LOOKAHEAD(ClassOrInterfaceBodyDeclaration()) ClassOrInterfaceBodyDeclaration()
|
ClassOrInterfaceBodyDeclaration()
RecordConstructorDeclaration()
}

void RecordConstructorDeclaration():
Expand All @@ -1176,7 +1176,6 @@ void RecordConstructorDeclaration():
modifiers = Modifiers() { jjtThis.setModifiers(modifiers); }
[TypeParameters()]
<IDENTIFIER> { jjtThis.setImage(token.image); }
[ "throws" NameList() ]
"{" ( BlockStatement() )* "}"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public void innerRecords() {
Assert.assertTrue(complex.isNested());
Assert.assertEquals(0, complex.getRecordComponents().get(0).findChildrenOfType(ASTAnnotation.class).size());
Assert.assertEquals(1, complex.getRecordComponents().get(1).findChildrenOfType(ASTAnnotation.class).size());
Assert.assertTrue(complex.getChild(1).getChild(0).getChild(1) instanceof ASTConstructorDeclaration);

ASTRecordDeclaration nested = recordDecls.get(1);
Assert.assertEquals("Nested", nested.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,26 @@
public class Records {

@Target(ElementType.TYPE_USE)
@interface Nullable {
}
@interface Nullable { }

@Target({ElementType.CONSTRUCTOR, ElementType.PARAMETER})
@interface MyAnnotation { }

public record MyComplex(int real, @Deprecated int imaginary) {
// explicit declaration of a canonical constructor
@MyAnnotation
public MyComplex(@MyAnnotation int real, int imaginary) {
if (real > 100) throw new IllegalArgumentException("too big");
this.real = real;
this.imaginary = imaginary;
}
public record Nested(int a) {};
}


public record Range(int lo, int hi) {
// compact record constructor
@MyAnnotation
public Range {
if (lo > hi) /* referring here to the implicit constructor parameters */
throw new IllegalArgumentException(String.format("(%d,%d)", lo, hi));
Expand Down

0 comments on commit 7d3df99

Please sign in to comment.