Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,26 @@ public Builder setMethods(MethodDefinition... methods) {

public AnonymousClassExpr build() {
AnonymousClassExpr anonymousClassExpr = autoBuild();

// 1. The anonymous class expression should be reference types.
Preconditions.checkState(
TypeNode.isReferenceType(anonymousClassExpr.type()),
"Anonymous class expression must be reference types.");

// 2. Check that there are no null methods or statements.
String contextInfo =
String.format("anonymous class of type %s", anonymousClassExpr.type().reference().name());
NodeValidator.checkNoNullElements(anonymousClassExpr.methods(), "methods", contextInfo);
NodeValidator.checkNoNullElements(anonymousClassExpr.statements(), "statements", contextInfo);

for (MethodDefinition method : anonymousClassExpr.methods()) {
// 2. Static methods are not allowed in anonymous class.
// 3. Static methods are not allowed in anonymous class.
Preconditions.checkState(!method.isStatic(), "Anonymous class cannot have static methods.");
// 3. Anonymous class cannot have explicit constructors.
// 4. Anonymous class cannot have explicit constructors.
Preconditions.checkState(
!method.isConstructor(), "Anonymous class cannot have explicit constructors.");
}
// 4. Static variable expression is not allowed unless it is final.
// 5. Static variable expression is not allowed unless it is final.
for (Statement statement : anonymousClassExpr.statements()) {
if (statement instanceof ExprStatement) {
Expr expr = ((ExprStatement) statement).expression();
Expand Down