Skip to content

Commit

Permalink
Make more constructor tools
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid committed Mar 10, 2017
1 parent af62675 commit b27adec
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 15 deletions.
Expand Up @@ -38,6 +38,10 @@
import com.github.javaparser.ast.visitor.VoidVisitor;
import java.util.*;
import static com.github.javaparser.utils.Utils.assertNotNull;
import static java.util.Collections.unmodifiableList;
import static java.util.stream.Collectors.toCollection;
import static java.util.stream.Collectors.toList;

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.visitor.CloneVisitor;
import com.github.javaparser.metamodel.ClassOrInterfaceDeclarationMetaModel;
Expand Down Expand Up @@ -156,6 +160,59 @@ public Optional<ConstructorDeclaration> getDefaultConstructor() {
return getMembers().stream().filter(bd -> bd instanceof ConstructorDeclaration).map(bd -> (ConstructorDeclaration) bd).filter(cd -> cd.getParameters().isEmpty()).findFirst();
}

/**
* Adds a constructor to this
*
* @param modifiers the modifiers like {@link Modifier#PUBLIC}
* @return the {@link MethodDeclaration} created
*/
public ConstructorDeclaration addConstructor(Modifier... modifiers) {
ConstructorDeclaration constructorDeclaration = new ConstructorDeclaration();
constructorDeclaration.setModifiers(Arrays.stream(modifiers)
.collect(toCollection(() -> EnumSet.noneOf(Modifier.class))));
constructorDeclaration.setName(getName());
getMembers().add(constructorDeclaration);
return constructorDeclaration;
}

/**
* Find all constructors for this class.
*
* @return the constructors found. This list is immutable.
*/
public List<ConstructorDeclaration> getConstructors() {
return unmodifiableList(getMembers().stream()
.filter(m -> m instanceof ConstructorDeclaration)
.map(m -> (ConstructorDeclaration) m)
.collect(toList()));
}

/**
* Try to find a {@link MethodDeclaration} by its parameters types
*
* @param paramTypes the types of parameters like "Map&lt;Integer,String&gt;","int" to match<br> void
* foo(Map&lt;Integer,String&gt; myMap,int number)
* @return the methods found (multiple in case of overloading)
*/
public Optional<ConstructorDeclaration> getConstructorByParameterTypes(String... paramTypes) {
return getConstructors().stream()
.filter(m -> m.hasParametersOfType(paramTypes))
.findFirst();
}

/**
* Try to find a {@link MethodDeclaration} by its parameters types
*
* @param paramTypes the types of parameters like "Map&lt;Integer,String&gt;","int" to match<br> void
* foo(Map&lt;Integer,String&gt; myMap,int number)
* @return the methods found (multiple in case of overloading)
*/
public Optional<ConstructorDeclaration> getConstructorByParameterTypes(Class<?>... paramTypes) {
return getConstructors().stream()
.filter(m -> m.hasParametersOfType(paramTypes))
.findFirst();
}

@Override
public List<NodeList<?>> getNodeLists() {
return Arrays.asList(getExtendedTypes(), getImplementedTypes(), getTypeParameters(), getMembers(), getAnnotations());
Expand Down
Expand Up @@ -193,21 +193,6 @@ default MethodDeclaration addMethod(String methodName, Modifier... modifiers) {
return methodDeclaration;
}

/**
* Adds a constructor to this
*
* @param modifiers the modifiers like {@link Modifier#PUBLIC}
* @return the {@link MethodDeclaration} created
*/
default ConstructorDeclaration addConstructor(Modifier... modifiers) {
ConstructorDeclaration constructorDeclaration = new ConstructorDeclaration();
constructorDeclaration.setModifiers(Arrays.stream(modifiers)
.collect(toCollection(() -> EnumSet.noneOf(Modifier.class))));
constructorDeclaration.setName(((TypeDeclaration<?>) this).getName());
getMembers().add(constructorDeclaration);
return constructorDeclaration;
}

default BlockStmt addInitializer() {
BlockStmt block = new BlockStmt();
InitializerDeclaration initializerDeclaration = new InitializerDeclaration(false, block);
Expand Down

0 comments on commit b27adec

Please sign in to comment.