Skip to content

Commit

Permalink
Methods with "throws" support in inference generators
Browse files Browse the repository at this point in the history
  • Loading branch information
skapral committed Nov 18, 2021
1 parent 906cd4a commit 63e86a3
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ${this.name} implements ${interface.name} {

<#list methods as method>
@Override
public final ${method.returns.name} ${method.name}(<#list method.args as arg>${arg.type.name} ${arg.name}</#list>) {
public final ${method.declaration} {
<#if method.returns.name != "void">
return inference.inferredInstance().${method.name}(<#list method.args as arg>${arg.name}</#list>);
<#else>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ public InferredImplementationGeneratorTest() {
new SourceFile("com/test/inferred4/OperationInferred.java")
)
)
),
new TestCase(
"Generator takes into account 'throws' in method signatures",
new AssertAnnotationProcessorGeneratesFiles(
new InferredImplementationGenerator(),
List.of(
new SourceFile("com/test/inferred5/Assertion.java"),
new SourceFile("com/test/inferred5/package-info.java")
),
List.of(
new SourceFile("com/test/inferred5/AssertionInferred.java")
)
)
)
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.test.inferred5;

public interface Assertion {
void check() throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.test.inferred5;

import com.pragmaticobjects.oo.inference.api.Inference;

public class AssertionInferred implements Assertion {
private final Inference<Assertion> inference;

public AssertionInferred(Inference<Assertion> inference) {
this.inference = inference;
}

@Override
public final void check() throws Exception {
inference.inferredInstance().check();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@GenerateInferred(Assertion.class)
package com.test.inferred5;

import com.pragmaticobjects.oo.inference.api.GenerateInferred;
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ public interface Method extends ImportsProvider {
Type returns();
String name();
Iterable<Argument> args();
Iterable<Type> throwsExceptions();
String declaration();
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,26 @@
*/
package com.pragmaticobjects.oo.meta.model;

import io.vavr.collection.List;

import java.util.Collection;
import java.util.HashSet;
import java.util.stream.Collectors;

/**
*
* @author skapral
*/
public class MethodFixed implements Method {
private final Type returns;
private final String name;
private final Iterable<Argument> args;
private final Iterable<Type> exceptions;

public MethodFixed(Type returns, String name, Iterable<Argument> args) {
public MethodFixed(Type returns, String name, Iterable<Argument> args, Iterable<Type> exceptions) {
this.returns = returns;
this.name = name;
this.args = args;
this.exceptions = exceptions;
}

@Override
Expand All @@ -58,13 +62,39 @@ public final Iterable<Argument> args() {
return args;
}

@Override
public final Iterable<Type> throwsExceptions() {
return exceptions;
}

@Override
public final Collection<Type> getImports() {
HashSet<Type> types = new HashSet<>();
types.add(returns);
for(Argument arg : args) {
for (Argument arg : args) {
types.add(arg.type());
}
for (Type ex : exceptions) {
types.add(ex);
}
return types;
}

public final String declaration() {
StringBuilder sb = new StringBuilder();
sb.append(returns().name()).append(" ");
sb.append(name());
sb.append("(");
sb.append(List.ofAll(args())
.map(arg -> arg.type().name() + " " + arg.name())
.collect(Collectors.joining(", ")));
sb.append(")");
List<Type> exceptions = List.ofAll(throwsExceptions());
if(!exceptions.isEmpty()) {
sb.append(" ").append("throws").append(" ");
String exceptionsDecl = exceptions.map(e -> e.name()).collect(Collectors.joining(", "));
sb.append(exceptionsDecl);
}
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package com.pragmaticobjects.oo.meta.model;

import io.vavr.collection.List;

import javax.lang.model.element.ExecutableElement;

/**
Expand All @@ -47,7 +48,10 @@ public final Method inferredInstance() {
.map(tm -> new ArgumentFixed(
new TypeFromTypeMirror(tm.asType()),
tm.getSimpleName().toString()
))
)),
List.of(t)
.flatMap(_t -> _t.getThrownTypes())
.map(TypeFromTypeMirror::new)
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ public final Iterable<Argument> args() {
return inference.inferredInstance().args();
}

@Override
public final Iterable<Type> throwsExceptions() {
return inference.inferredInstance().throwsExceptions();
}

@Override
public final String declaration() {
return inference.inferredInstance().declaration();
}

@Override
public final Collection<Type> getImports() {
return inference.inferredInstance().getImports();
Expand Down

0 comments on commit 63e86a3

Please sign in to comment.