Skip to content

Commit

Permalink
Merge pull request #106 from ftomassetti/lazyinit
Browse files Browse the repository at this point in the history
Avoid returning null lists
  • Loading branch information
ftomassetti committed Sep 6, 2015
2 parents b258f63 + 2b6ef36 commit defc804
Show file tree
Hide file tree
Showing 36 changed files with 216 additions and 88 deletions.
Expand Up @@ -48,6 +48,8 @@
import java.util.ArrayList;
import java.util.List;

import static com.github.javaparser.ast.internal.Utils.*;

/**
* This class helps to construct new nodes.
*
Expand Down Expand Up @@ -168,7 +170,7 @@ public static VariableDeclarationExpr createVariableDeclarationExpr(Type type, S
*/
public static void addParameter(MethodDeclaration method, Parameter parameter) {
List<Parameter> parameters = method.getParameters();
if (parameters == null) {
if (isNullOrEmpty(parameters)) {
parameters = new ArrayList<Parameter>();
method.setParameters(parameters);
}
Expand All @@ -186,7 +188,7 @@ public static void addParameter(MethodDeclaration method, Parameter parameter) {
*/
public static void addArgument(MethodCallExpr call, Expression arg) {
List<Expression> args = call.getArgs();
if (args == null) {
if (isNullOrEmpty(args)) {
args = new ArrayList<Expression>();
call.setArgs(args);
}
Expand All @@ -204,7 +206,7 @@ public static void addArgument(MethodCallExpr call, Expression arg) {
*/
public static void addTypeDeclaration(CompilationUnit cu, TypeDeclaration type) {
List<TypeDeclaration> types = cu.getTypes();
if (types == null) {
if (isNullOrEmpty(types)) {
types = new ArrayList<TypeDeclaration>();
cu.setTypes(types);
}
Expand Down Expand Up @@ -247,7 +249,7 @@ public static ReferenceType createReferenceType(PrimitiveType type, int arrayCou
*/
public static void addStmt(BlockStmt block, Statement stmt) {
List<Statement> stmts = block.getStmts();
if (stmts == null) {
if (isNullOrEmpty(stmts)) {
stmts = new ArrayList<Statement>();
block.setStmts(stmts);
}
Expand Down Expand Up @@ -276,7 +278,7 @@ public static void addStmt(BlockStmt block, Expression expr) {
*/
public static void addMember(TypeDeclaration type, BodyDeclaration decl) {
List<BodyDeclaration> members = type.getMembers();
if (members == null) {
if (isNullOrEmpty(members)) {
members = new ArrayList<BodyDeclaration>();
type.setMembers(members);
}
Expand Down
Expand Up @@ -24,8 +24,6 @@
import static com.github.javaparser.PositionUtils.areInOrder;
import static com.github.javaparser.PositionUtils.sortByBeginPosition;

import com.github.javaparser.ASTParser;
import com.github.javaparser.ParseException;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.ImportDeclaration;
import com.github.javaparser.ast.Node;
Expand Down
Expand Up @@ -33,6 +33,8 @@

import java.util.List;

import static com.github.javaparser.ast.internal.Utils.*;

/**
* <p>
* This class represents the entire compilation unit. Each java file denotes a
Expand Down Expand Up @@ -105,6 +107,7 @@ public List<Comment> getComments() {
* @return the list of imports or <code>null</code> if there is no import
*/
public List<ImportDeclaration> getImports() {
imports = ensureNotNull(imports);
return imports;
}

Expand All @@ -130,6 +133,7 @@ public PackageDeclaration getPackage() {
* @see EnumDeclaration
*/
public List<TypeDeclaration> getTypes() {
types = ensureNotNull(types);
return types;
}

Expand Down
Expand Up @@ -23,9 +23,12 @@

import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.internal.Utils;
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
Expand Down Expand Up @@ -82,6 +85,7 @@ public <A> void accept(VoidVisitor<A> v, A arg) {
* @return list of annotations or <code>null</code>
*/
public List<AnnotationExpr> getAnnotations() {
annotations = Utils.ensureNotNull(annotations);
return annotations;
}

Expand Down
Expand Up @@ -28,6 +28,8 @@

import java.util.List;

import static com.github.javaparser.ast.internal.Utils.ensureNotNull;

/**
* <p>
* This class represents the declaration of a generics argument.
Expand Down Expand Up @@ -95,7 +97,8 @@ public String getName() {
* @return list of types that this paramente extends or <code>null</code>
*/
public List<ClassOrInterfaceType> getTypeBound() {
return typeBound;
typeBound = ensureNotNull(typeBound);
return typeBound;
}

/**
Expand All @@ -120,6 +123,7 @@ public void setTypeBound(final List<ClassOrInterfaceType> typeBound) {
}

public List<AnnotationExpr> getAnnotations() {
annotations = ensureNotNull(annotations);
return annotations;
}

Expand Down
Expand Up @@ -26,6 +26,8 @@

import java.util.List;

import static com.github.javaparser.ast.internal.Utils.*;

public abstract class BaseParameter extends Node {
private int modifiers;

Expand Down Expand Up @@ -58,7 +60,11 @@ public BaseParameter(int beginLine, int beginColumn, int endLine, int endColumn,
setId(id);
}

/**
* @return the list returned could be immutable (in that case it will be empty)
*/
public List<AnnotationExpr> getAnnotations() {
annotations = ensureNotNull(annotations);
return annotations;
}

Expand All @@ -76,6 +82,10 @@ public int getModifiers() {
return modifiers;
}

/**
* @param annotations a null value is currently treated as an empty list. This behavior could change
* in the future, so please avoid passing null
*/
public void setAnnotations(List<AnnotationExpr> annotations) {
this.annotations = annotations;
setAsParentNodeOf(this.annotations);
Expand Down
Expand Up @@ -23,8 +23,8 @@

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.internal.Utils;

import java.util.ArrayList;
import java.util.List;

/**
Expand All @@ -47,12 +47,15 @@ public BodyDeclaration(int beginLine, int beginColumn, int endLine, int endColum
}

public final List<AnnotationExpr> getAnnotations() {
if (annotations==null){
annotations = new ArrayList<AnnotationExpr>();
}
annotations = Utils.ensureNotNull(annotations);
return annotations;
}

/**
*
* @param annotations a null value is currently treated as an empty list. This behavior could change
* in the future, so please avoid passing null
*/
public final void setAnnotations(List<AnnotationExpr> annotations) {
this.annotations = annotations;
setAsParentNodeOf(this.annotations);
Expand Down
Expand Up @@ -31,6 +31,8 @@

import java.util.List;

import static com.github.javaparser.ast.internal.Utils.ensureNotNull;

/**
* @author Julio Vilmar Gesser
*/
Expand Down Expand Up @@ -85,26 +87,39 @@ public ClassOrInterfaceDeclaration(final int beginLine, final int beginColumn, f
}

public List<ClassOrInterfaceType> getExtends() {
return extendsList;
extendsList = ensureNotNull(extendsList);
return extendsList;
}

public List<ClassOrInterfaceType> getImplements() {
return implementsList;
implementsList = ensureNotNull(implementsList);
return implementsList;
}

public List<TypeParameter> getTypeParameters() {
return typeParameters;
typeParameters = ensureNotNull(typeParameters);
return typeParameters;
}

public boolean isInterface() {
return interface_;
}

/**
*
* @param extendsList a null value is currently treated as an empty list. This behavior could change
* in the future, so please avoid passing null
*/
public void setExtends(final List<ClassOrInterfaceType> extendsList) {
this.extendsList = extendsList;
setAsParentNodeOf(this.extendsList);
}

/**
*
* @param implementsList a null value is currently treated as an empty list. This behavior could change
* in the future, so please avoid passing null
*/
public void setImplements(final List<ClassOrInterfaceType> implementsList) {
this.implementsList = implementsList;
setAsParentNodeOf(this.implementsList);
Expand All @@ -114,6 +129,11 @@ public void setInterface(final boolean interface_) {
this.interface_ = interface_;
}

/**
*
* @param typeParameters a null value is currently treated as an empty list. This behavior could change
* in the future, so please avoid passing null
*/
public void setTypeParameters(final List<TypeParameter> typeParameters) {
this.typeParameters = typeParameters;
setAsParentNodeOf(this.typeParameters);
Expand Down
Expand Up @@ -22,7 +22,6 @@
package com.github.javaparser.ast.body;

import java.util.List;
import java.util.ArrayList;

import com.github.javaparser.ast.AccessSpecifier;
import com.github.javaparser.ast.DocumentableNode;
Expand All @@ -35,6 +34,8 @@
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;

import static com.github.javaparser.ast.internal.Utils.*;

/**
* @author Julio Vilmar Gesser
*/
Expand Down Expand Up @@ -117,20 +118,17 @@ public NameExpr getNameExpr() {
}

public List<Parameter> getParameters() {
if (parameters == null) {
parameters = new ArrayList<Parameter>();
}
parameters = ensureNotNull(parameters);
return parameters;
}

public List<NameExpr> getThrows() {
if (throws_ == null) {
throws_ = new ArrayList<NameExpr>();
}
throws_ = ensureNotNull(throws_);
return throws_;
}

public List<TypeParameter> getTypeParameters() {
typeParameters = ensureNotNull(typeParameters);
return typeParameters;
}

Expand Down Expand Up @@ -163,7 +161,7 @@ public void setThrows(List<NameExpr> throws_) {

public void setTypeParameters(List<TypeParameter> typeParameters) {
this.typeParameters = typeParameters;
setAsParentNodeOf(this.typeParameters);
setAsParentNodeOf(this.typeParameters);
}

/**
Expand Down
Expand Up @@ -31,6 +31,8 @@

import java.util.List;

import static com.github.javaparser.ast.internal.Utils.ensureNotNull;

/**
* @author Julio Vilmar Gesser
*/
Expand Down Expand Up @@ -74,10 +76,12 @@ public <A> void accept(VoidVisitor<A> v, A arg) {
}

public List<Expression> getArgs() {
args = ensureNotNull(args);
return args;
}

public List<BodyDeclaration> getClassBody() {
classBody = ensureNotNull(classBody);
return classBody;
}

Expand Down
Expand Up @@ -30,6 +30,8 @@

import java.util.List;

import static com.github.javaparser.ast.internal.Utils.*;

/**
* @author Julio Vilmar Gesser
*/
Expand Down Expand Up @@ -70,10 +72,12 @@ public <A> void accept(VoidVisitor<A> v, A arg) {
}

public List<EnumConstantDeclaration> getEntries() {
entries = ensureNotNull(entries);
return entries;
}

public List<ClassOrInterfaceType> getImplements() {
implementsList = ensureNotNull(implementsList);
return implementsList;
}

Expand Down
Expand Up @@ -31,6 +31,8 @@
import java.util.ArrayList;
import java.util.List;

import static com.github.javaparser.ast.internal.Utils.*;

/**
* @author Julio Vilmar Gesser
*/
Expand Down Expand Up @@ -98,6 +100,7 @@ public Type getType() {
}

public List<VariableDeclarator> getVariables() {
variables = ensureNotNull(variables);
return variables;
}

Expand Down

0 comments on commit defc804

Please sign in to comment.