Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
MethodSpec factory for overriding an ExecutableElement.
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton committed Feb 16, 2015
1 parent 33bb07e commit 885ab84
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 10 deletions.
44 changes: 43 additions & 1 deletion src/main/java/com/squareup/javapoet/MethodSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@
import java.io.StringWriter;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeParameterElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.type.TypeVariable;

import static com.squareup.javapoet.Util.checkArgument;
import static com.squareup.javapoet.Util.checkNotNull;
Expand Down Expand Up @@ -150,6 +156,42 @@ public static Builder constructorBuilder() {
return new Builder(CONSTRUCTOR);
}

public static Builder overriding(ExecutableElement method) {
checkNotNull(method, "method == null");

Set<Modifier> modifiers = method.getModifiers();
if (modifiers.contains(Modifier.PRIVATE)
|| modifiers.contains(Modifier.FINAL)
|| modifiers.contains(Modifier.STATIC)) {
throw new IllegalArgumentException("cannot override method with modifiers: " + modifiers);
}

String methodName = method.getSimpleName().toString();
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(methodName);
methodBuilder.addAnnotation(Override.class);
methodBuilder.returns(TypeName.get(method.getReturnType()));

modifiers = new LinkedHashSet<>(modifiers); // Local copy so we can remove.
modifiers.remove(Modifier.ABSTRACT);
methodBuilder.addModifiers(modifiers.toArray(new Modifier[modifiers.size()]));

for (VariableElement parameter : method.getParameters()) {
methodBuilder.addParameter(TypeName.get(parameter.asType()),
parameter.getSimpleName().toString());
}

for (TypeMirror thrownType : method.getThrownTypes()) {
methodBuilder.addException(TypeName.get(thrownType));
}

for (TypeParameterElement typeParameterElement : method.getTypeParameters()) {
methodBuilder.addTypeVariable(
TypeVariableName.get((TypeVariable) typeParameterElement.asType()));
}

return methodBuilder;
}

public static final class Builder {
private final String name;

Expand Down
44 changes: 35 additions & 9 deletions src/test/java/com/squareup/javapoet/MethodSpecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
*/
package com.squareup.javapoet;

import com.google.common.collect.ImmutableSet;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import org.junit.Test;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public final class MethodSpecTest {

Expand All @@ -27,8 +32,7 @@ public final class MethodSpecTest {
MethodSpec.methodBuilder("doSomething").addAnnotations(null);
fail();
} catch (IllegalArgumentException expected) {
assertThat(expected.getMessage())
.isEqualTo("annotationSpecs == null");
assertThat(expected).hasMessage("annotationSpecs == null");
}
}

Expand All @@ -37,8 +41,7 @@ public final class MethodSpecTest {
MethodSpec.methodBuilder("doSomething").addTypeVariables(null);
fail();
} catch (IllegalArgumentException expected) {
assertThat(expected.getMessage())
.isEqualTo("typeVariables == null");
assertThat(expected).hasMessage("typeVariables == null");
}
}

Expand All @@ -47,8 +50,7 @@ public final class MethodSpecTest {
MethodSpec.methodBuilder("doSomething").addParameters(null);
fail();
} catch (IllegalArgumentException expected) {
assertThat(expected.getMessage())
.isEqualTo("parameterSpecs == null");
assertThat(expected).hasMessage("parameterSpecs == null");
}
}

Expand All @@ -57,8 +59,32 @@ public final class MethodSpecTest {
MethodSpec.methodBuilder("doSomething").addExceptions(null);
fail();
} catch (IllegalArgumentException expected) {
assertThat(expected.getMessage())
.isEqualTo("exceptions == null");
assertThat(expected).hasMessage("exceptions == null");
}
}
}

@Test public void overrideInvalidModifiers() {
ExecutableElement method = mock(ExecutableElement.class);
when(method.getModifiers()).thenReturn(ImmutableSet.of(Modifier.FINAL));
try {
MethodSpec.overriding(method);
fail();
} catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("cannot override method with modifiers: [final]");
}
when(method.getModifiers()).thenReturn(ImmutableSet.of(Modifier.PRIVATE));
try {
MethodSpec.overriding(method);
fail();
} catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("cannot override method with modifiers: [private]");
}
when(method.getModifiers()).thenReturn(ImmutableSet.of(Modifier.STATIC));
try {
MethodSpec.overriding(method);
fail();
} catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("cannot override method with modifiers: [static]");
}
}
}

0 comments on commit 885ab84

Please sign in to comment.