Skip to content

Commit

Permalink
revise how exceptions are dealt with in MethodLikeDeclaratio and Meth…
Browse files Browse the repository at this point in the history
…odUsage
  • Loading branch information
ftomassetti committed Aug 25, 2017
1 parent ce33044 commit 88a7987
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 31 deletions.
Expand Up @@ -17,7 +17,7 @@
package com.github.javaparser.symbolsolver.javaparsermodel.declarations;

import com.github.javaparser.symbolsolver.model.declarations.*;
import com.github.javaparser.symbolsolver.model.typesystem.ReferenceType;
import com.github.javaparser.symbolsolver.model.typesystem.Type;

import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -72,7 +72,7 @@ public int getNumberOfSpecifiedExceptions() {
}

@Override
public ReferenceType getSpecifiedException(int index) {
public Type getSpecifiedException(int index) {
throw new UnsupportedOperationException("The default constructor does not throw exceptions");
}
}
Expand Up @@ -20,6 +20,7 @@
import com.github.javaparser.symbolsolver.model.declarations.*;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.model.typesystem.ReferenceType;
import com.github.javaparser.symbolsolver.model.typesystem.Type;

import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -88,13 +89,12 @@ public int getNumberOfSpecifiedExceptions() {
}

@Override
public ReferenceType getSpecifiedException(int index) {
public Type getSpecifiedException(int index) {
if (index < 0 || index >= getNumberOfSpecifiedExceptions()) {
throw new IllegalArgumentException(String.format("No exception with index %d. Number of exceptions: %d",
index, getNumberOfSpecifiedExceptions()));
}
return JavaParserFacade.get(typeSolver)
.convert(wrappedNode.getThrownExceptions().get(index), wrappedNode)
.asReferenceType();
.convert(wrappedNode.getThrownExceptions().get(index), wrappedNode);
}
}
Expand Up @@ -311,7 +311,7 @@ public int getNumberOfSpecifiedExceptions() {
}

@Override
public ReferenceType getSpecifiedException(int index) {
public Type getSpecifiedException(int index) {
throw new UnsupportedOperationException("The values method of an enum does not throw any exception");
}
}
Expand Down
Expand Up @@ -154,12 +154,12 @@ public int getNumberOfSpecifiedExceptions() {
}

@Override
public ReferenceType getSpecifiedException(int index) {
public Type getSpecifiedException(int index) {
if (index < 0 || index >= getNumberOfSpecifiedExceptions()) {
throw new IllegalArgumentException(String.format("No exception with index %d. Number of exceptions: %d",
index, getNumberOfSpecifiedExceptions()));
}
return JavaParserFacade.get(typeSolver).convert(wrappedNode.getThrownExceptions()
.get(index), wrappedNode).asReferenceType();
.get(index), wrappedNode);
}
}
Expand Up @@ -18,7 +18,7 @@

import com.github.javaparser.symbolsolver.model.declarations.*;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.model.typesystem.ReferenceType;
import com.github.javaparser.symbolsolver.model.typesystem.Type;
import javassist.CtConstructor;
import javassist.NotFoundException;
import javassist.bytecode.BadBytecode;
Expand Down Expand Up @@ -131,13 +131,13 @@ public int getNumberOfSpecifiedExceptions() {
}

@Override
public ReferenceType getSpecifiedException(int index) {
public Type getSpecifiedException(int index) {
if (index < 0 || index >= getNumberOfSpecifiedExceptions()) {
throw new IllegalArgumentException(String.format("No exception with index %d. Number of exceptions: %d",
index, getNumberOfSpecifiedExceptions()));
}
try {
return JavassistFactory.typeUsageFor(ctConstructor.getExceptionTypes()[index], typeSolver).asReferenceType();
return JavassistFactory.typeUsageFor(ctConstructor.getExceptionTypes()[index], typeSolver);
} catch (NotFoundException e) {
throw new RuntimeException(e);
}
Expand Down
Expand Up @@ -181,13 +181,13 @@ public int getNumberOfSpecifiedExceptions() {
}

@Override
public ReferenceType getSpecifiedException(int index) {
public Type getSpecifiedException(int index) {
if (index < 0 || index >= getNumberOfSpecifiedExceptions()) {
throw new IllegalArgumentException(String.format("No exception with index %d. Number of exceptions: %d",
index, getNumberOfSpecifiedExceptions()));
}
try {
return JavassistFactory.typeUsageFor(ctMethod.getExceptionTypes()[index], typeSolver).asReferenceType();
return JavassistFactory.typeUsageFor(ctMethod.getExceptionTypes()[index], typeSolver);
} catch (NotFoundException e) {
throw new RuntimeException(e);
}
Expand Down
Expand Up @@ -18,7 +18,7 @@

import com.github.javaparser.symbolsolver.model.declarations.*;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.model.typesystem.ReferenceType;
import com.github.javaparser.symbolsolver.model.typesystem.Type;

import java.lang.reflect.Constructor;
import java.util.Arrays;
Expand Down Expand Up @@ -82,10 +82,10 @@ public int getNumberOfSpecifiedExceptions() {
}

@Override
public ReferenceType getSpecifiedException(int index) {
public Type getSpecifiedException(int index) {
if (index < 0 || index >= getNumberOfSpecifiedExceptions()) {
throw new IllegalArgumentException();
}
return ReflectionFactory.typeUsageFor(this.constructor.getExceptionTypes()[index], typeSolver).asReferenceType();
return ReflectionFactory.typeUsageFor(this.constructor.getExceptionTypes()[index], typeSolver);
}
}
Expand Up @@ -139,10 +139,10 @@ public int getNumberOfSpecifiedExceptions() {
}

@Override
public ReferenceType getSpecifiedException(int index) {
public Type getSpecifiedException(int index) {
if (index < 0 || index >= getNumberOfSpecifiedExceptions()) {
throw new IllegalArgumentException();
}
return ReflectionFactory.typeUsageFor(this.method.getExceptionTypes()[index], typeSolver).asReferenceType();
return ReflectionFactory.typeUsageFor(this.method.getExceptionTypes()[index], typeSolver);
}
}
Expand Up @@ -17,7 +17,11 @@
package com.github.javaparser.symbolsolver.model.declarations;

import com.github.javaparser.symbolsolver.model.typesystem.ReferenceType;
import com.github.javaparser.symbolsolver.model.typesystem.Type;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

/**
Expand Down Expand Up @@ -134,5 +138,17 @@ default Optional<TypeParameterDeclaration> findTypeParameter(String name) {
* getNumberOfSpecifiedExceptions
* @throws UnsupportedOperationException for those types of methods of constructor that do not declare exceptions
*/
ReferenceType getSpecifiedException(int index);
Type getSpecifiedException(int index);

default List<Type> getSpecifiedExceptions() {
if (getNumberOfSpecifiedExceptions() == 0) {
return Collections.emptyList();
} else {
List<Type> exceptions = new LinkedList<>();
for (int i=0;i<getNumberOfSpecifiedExceptions();i++) {
exceptions.add(getSpecifiedException(i));
}
return exceptions;
}
}
}
Expand Up @@ -37,6 +37,7 @@
public class MethodUsage implements TypeParametrized {
private MethodDeclaration declaration;
private List<Type> paramTypes = new ArrayList<>();
private List<Type> exceptionTypes = new ArrayList<>();
private Type returnType;
private TypeParametersMap typeParametersMap;

Expand All @@ -46,17 +47,27 @@ public MethodUsage(MethodDeclaration declaration) {
for (int i = 0; i < declaration.getNumberOfParams(); i++) {
paramTypes.add(declaration.getParam(i).getType());
}
for (int i = 0; i < declaration.getNumberOfSpecifiedExceptions(); i++) {
exceptionTypes.add(declaration.getSpecifiedException(i));
}
returnType = declaration.getReturnType();
}

public MethodUsage(MethodDeclaration declaration, List<Type> paramTypes, Type returnType) {
this(declaration, paramTypes, returnType, TypeParametersMap.empty());
this(declaration, paramTypes, returnType, declaration.getSpecifiedExceptions(), TypeParametersMap.empty());
}

public MethodUsage(MethodDeclaration declaration, List<Type> paramTypes, Type returnType,
List<Type> exceptionTypes) {
this(declaration, paramTypes, returnType, exceptionTypes, TypeParametersMap.empty());
}

private MethodUsage(MethodDeclaration declaration, List<Type> paramTypes, Type returnType, TypeParametersMap typeParametersMap) {
private MethodUsage(MethodDeclaration declaration, List<Type> paramTypes, Type returnType,
List<Type> exceptionTypes, TypeParametersMap typeParametersMap) {
this.declaration = declaration;
this.paramTypes = paramTypes;
this.returnType = returnType;
this.exceptionTypes = exceptionTypes;
this.typeParametersMap = typeParametersMap;
}

Expand Down Expand Up @@ -89,19 +100,34 @@ public List<Type> getParamTypes() {
}

public MethodUsage replaceParamType(int i, Type replaced) {
if (i < 0 || i >= getNoParams()) {
throw new IllegalArgumentException();
}
if (paramTypes.get(i) == replaced) {
return this;
}
List<Type> newParams = new LinkedList<>(paramTypes);
newParams.set(i, replaced);
return new MethodUsage(declaration, newParams, returnType, typeParametersMap);
return new MethodUsage(declaration, newParams, returnType, exceptionTypes, typeParametersMap);
}

public MethodUsage replaceExceptionType(int i, Type replaced) {
if (i < 0 || i >= exceptionTypes.size()) {
throw new IllegalArgumentException();
}
if (exceptionTypes.get(i) == replaced) {
return this;
}
List<Type> newTypes = new LinkedList<>(exceptionTypes);
newTypes.set(i, replaced);
return new MethodUsage(declaration, paramTypes, returnType, newTypes, typeParametersMap);
}

public MethodUsage replaceReturnType(Type returnType) {
if (returnType == this.returnType) {
return this;
} else {
return new MethodUsage(declaration, paramTypes, returnType, typeParametersMap);
return new MethodUsage(declaration, paramTypes, returnType, exceptionTypes, typeParametersMap);
}
}

Expand All @@ -125,14 +151,20 @@ public MethodUsage replaceTypeParameter(TypeParameterDeclaration typeParameter,
}

// TODO if the method declaration has a type param with that name ignore this call
MethodUsage res = new MethodUsage(declaration, paramTypes, returnType, typeParametersMap.toBuilder().setValue(typeParameter, type).build());
MethodUsage res = new MethodUsage(declaration, paramTypes, returnType, exceptionTypes,
typeParametersMap.toBuilder().setValue(typeParameter, type).build());

Map<TypeParameterDeclaration, Type> inferredTypes = new HashMap<>();
for (int i = 0; i < paramTypes.size(); i++) {
Type originalParamType = paramTypes.get(i);
Type newParamType = originalParamType.replaceTypeVariables(typeParameter, type, inferredTypes);
res = res.replaceParamType(i, newParamType);
}
for (int i = 0; i < exceptionTypes.size(); i++) {
Type originalType = exceptionTypes.get(i);
Type newType = originalType.replaceTypeVariables(typeParameter, type, inferredTypes);
res = res.replaceExceptionType(i, newType);
}
Type oldReturnType = res.returnType;
Type newReturnType = oldReturnType.replaceTypeVariables(typeParameter, type, inferredTypes);
res = res.replaceReturnType(newReturnType);
Expand All @@ -149,12 +181,7 @@ public String getQualifiedSignature() {
return this.getDeclaration().getQualifiedSignature();
}

public List<ReferenceType> exceptionTypes() {
// FIXME
List<ReferenceType> res = new LinkedList<>();
for (int i=0;i<this.getDeclaration().getNumberOfSpecifiedExceptions();i++) {
res.add(this.getDeclaration().getSpecifiedException(i));
}
return res;
public List<Type> exceptionTypes() {
return exceptionTypes;
}
}

0 comments on commit 88a7987

Please sign in to comment.