Skip to content

Commit

Permalink
introduce ConstructorDeclaration
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomassetti committed Oct 15, 2016
1 parent afcc4e9 commit 3c05bf3
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 62 deletions.
Expand Up @@ -4,6 +4,7 @@
import me.tomassetti.symbolsolver.AbstractTest;
import me.tomassetti.symbolsolver.model.declarations.AccessLevel;
import me.tomassetti.symbolsolver.model.declarations.FieldDeclaration;
import me.tomassetti.symbolsolver.model.declarations.MethodDeclaration;
import me.tomassetti.symbolsolver.model.resolution.TypeSolver;
import me.tomassetti.symbolsolver.model.resolution.UnsolvedSymbolException;
import me.tomassetti.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
Expand All @@ -14,6 +15,7 @@

import java.io.File;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -521,4 +523,45 @@ public void testGetDeclaredFields() {
fieldDeclaration = allFields.get(5);
assertEquals("body", fieldDeclaration.getName());
}

///
/// Test methods
///

@Test
public void testGetDeclaredMethods() {
JavaParserClassDeclaration constructorDeclaration = (JavaParserClassDeclaration) typeSolverNewCode.solveType("com.github.javaparser.ast.body.ConstructorDeclaration");

Set<MethodDeclaration> allMethods = constructorDeclaration.getDeclaredMethods();
assertEquals(20, allMethods.size());

List<MethodDeclaration> sortedMethods = allMethods.stream()
.sorted((o1, o2) -> o1.getQualifiedSignature().compareTo(o2.getQualifiedSignature()))
.collect(Collectors.toList());

assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration.accept(com.github.javaparser.ast.visitor.GenericVisitor<R, A>, A)", sortedMethods.get(0).getQualifiedSignature());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration.accept(com.github.javaparser.ast.visitor.VoidVisitor<A>, A)", sortedMethods.get(1).getQualifiedSignature());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration.getBody()", sortedMethods.get(2).getQualifiedSignature());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration.getDeclarationAsString()", sortedMethods.get(3).getQualifiedSignature());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration.getDeclarationAsString(boolean, boolean)", sortedMethods.get(4).getQualifiedSignature());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration.getDeclarationAsString(boolean, boolean, boolean)", sortedMethods.get(5).getQualifiedSignature());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration.getJavaDoc()", sortedMethods.get(6).getQualifiedSignature());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration.getModifiers()", sortedMethods.get(7).getQualifiedSignature());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration.getName()", sortedMethods.get(8).getQualifiedSignature());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration.getNameExpr()", sortedMethods.get(9).getQualifiedSignature());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration.getParameters()", sortedMethods.get(10).getQualifiedSignature());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration.getThrows()", sortedMethods.get(11).getQualifiedSignature());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration.getTypeParameters()", sortedMethods.get(12).getQualifiedSignature());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration.setBody(com.github.javaparser.ast.stmt.BlockStmt)", sortedMethods.get(13).getQualifiedSignature());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration.setModifiers(java.util.EnumSet<com.github.javaparser.ast.Modifier>)", sortedMethods.get(14).getQualifiedSignature());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration.setName(java.lang.String)", sortedMethods.get(15).getQualifiedSignature());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration.setNameExpr(com.github.javaparser.ast.expr.NameExpr)", sortedMethods.get(16).getQualifiedSignature());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration.setParameters(java.util.List<com.github.javaparser.ast.body.Parameter>)", sortedMethods.get(17).getQualifiedSignature());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration.setThrows(java.util.List<com.github.javaparser.ast.type.ReferenceType>)", sortedMethods.get(18).getQualifiedSignature());
assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration.setTypeParameters(java.util.List<com.github.javaparser.ast.type.TypeParameter>)", sortedMethods.get(19).getQualifiedSignature());
}

///
/// Test constructors
///
}
Expand Up @@ -50,4 +50,13 @@ default boolean isClass() {
*/
List<ReferenceType> getAllInterfaces();


///
/// Constructors
///

default List<ConstructorDeclaration> getConstructors() {
throw new UnsupportedOperationException();
}

}
@@ -0,0 +1,11 @@
package me.tomassetti.symbolsolver.model.declarations;

/**
* A declaration of a constructor.
*
* @author Federico Tomassetti
*/
public interface ConstructorDeclaration extends MethodLikeDeclaration {

boolean isDefaultMethod();
}
Expand Up @@ -7,64 +7,11 @@
*
* @author Federico Tomassetti
*/
public interface MethodDeclaration extends Declaration, TypeParametrizable, HasAccessLevel {

/**
* The type in which the method is declared.
*/
TypeDeclaration declaringType();
public interface MethodDeclaration extends MethodLikeDeclaration {

Type getReturnType();

int getNoParams();

ParameterDeclaration getParam(int i);

/**
* The last parameter can be variadic and sometimes it needs to be handled in a special way.
*/
default ParameterDeclaration getLastParam() {
if (getNoParams() == 0) {
throw new UnsupportedOperationException("This method has no typeParametersValues, therefore it has no a last parameter");
}
return getParam(getNoParams() - 1);
}

/**
* Note that when a method has a variadic parameter it should have an array type.
* @return
*/
default boolean hasVariadicParameter() {
if (getNoParams() == 0) {
return false;
} else {
return getParam(getNoParams() - 1).isVariadic();
}
}

boolean isAbstract();

default String getQualifiedName() {
return declaringType().getQualifiedName()+ "." + this.getName();
}

default String getSignature() {
StringBuffer sb = new StringBuffer();
sb.append(getName());
sb.append("(");
for (int i=0; i<getNoParams(); i++) {
if (i != 0) {
sb.append(", ");
}
sb.append(getParam(i).describeType());
}
sb.append(")");
return sb.toString();
}

default String getQualifiedSignature() {
return declaringType().getQualifiedName()+ "." + this.getSignature();
}

boolean isDefaultMethod();
}
@@ -0,0 +1,60 @@
package me.tomassetti.symbolsolver.model.declarations;

/**
* @author Federico Tomassetti
*/
public interface MethodLikeDeclaration extends Declaration, TypeParametrizable, HasAccessLevel {

default String getQualifiedName() {
return declaringType().getQualifiedName()+ "." + this.getName();
}

default String getSignature() {
StringBuffer sb = new StringBuffer();
sb.append(getName());
sb.append("(");
for (int i=0; i<getNoParams(); i++) {
if (i != 0) {
sb.append(", ");
}
sb.append(getParam(i).describeType());
}
sb.append(")");
return sb.toString();
}

default String getQualifiedSignature() {
return declaringType().getQualifiedName()+ "." + this.getSignature();
}

/**
* The type in which the method is declared.
*/
TypeDeclaration declaringType();

int getNoParams();

ParameterDeclaration getParam(int i);

/**
* The last parameter can be variadic and sometimes it needs to be handled in a special way.
*/
default ParameterDeclaration getLastParam() {
if (getNoParams() == 0) {
throw new UnsupportedOperationException("This method has no typeParametersValues, therefore it has no a last parameter");
}
return getParam(getNoParams() - 1);
}

/**
* Note that when a method has a variadic parameter it should have an array type.
* @return
*/
default boolean hasVariadicParameter() {
if (getNoParams() == 0) {
return false;
} else {
return getParam(getNoParams() - 1).isVariadic();
}
}
}
Expand Up @@ -118,6 +118,14 @@ default List<FieldDeclaration> getDeclaredFields() {
return getAllFields().stream().filter(it ->it.declaringType().getQualifiedName().equals(getQualifiedName())).collect(Collectors.toList());
}

///
/// Methods
///

Set<MethodDeclaration> getDeclaredMethods();

Set<MethodUsage> getAllMethods();

///
/// Resolution
///
Expand Down Expand Up @@ -147,14 +155,6 @@ default boolean canBeAssignedTo(TypeDeclaration other) {

boolean isAssignableBy(TypeDeclaration other);

///
/// Methods
///

Set<MethodDeclaration> getDeclaredMethods();

Set<MethodUsage> getAllMethods();

///
/// Annotations
///
Expand Down

0 comments on commit 3c05bf3

Please sign in to comment.