Add support for multi-value SuppressWarnings

This commit is contained in:
Ruben Dijkstra 2015-08-24 17:19:40 +02:00
parent 7c85a8522c
commit 791aeeeeaa
7 changed files with 64 additions and 1 deletions

1
.gitignore vendored
View File

@ -10,3 +10,4 @@ test-output
/.metadata
.cache
derby.log
nb-configuration.xml

View File

@ -109,4 +109,6 @@ public interface CodeWriter extends Appendable {
CodeWriter suppressWarnings(String type) throws IOException;
CodeWriter suppressWarnings(String... types) throws IOException;
}

View File

@ -481,6 +481,11 @@ public final class JavaWriter extends AbstractCodeWriter<JavaWriter> {
return line("@SuppressWarnings(\"" + type + "\")");
}
@Override
public CodeWriter suppressWarnings(String... types) throws IOException {
return annotation(new MultiSuppressWarnings(types));
}
private <T> Parameter[] transform(Collection<T> parameters,
Function<T, Parameter> transformer) {
Parameter[] rv = new Parameter[parameters.size()];

View File

@ -0,0 +1,40 @@
/*
* Copyright 2015, The Querydsl Team (http://www.querydsl.com/team)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mysema.codegen;
import java.lang.annotation.Annotation;
import java.util.Arrays;
@SuppressWarnings("AnnotationAsSuperInterface") // Internal helper class
class MultiSuppressWarnings implements SuppressWarnings {
private final String[] values;
public MultiSuppressWarnings(String... values) {
this.values = Arrays.copyOf(values, values.length);
}
@Override
@SuppressWarnings("ReturnOfCollectionOrArrayField")
public String[] value() {
return values;
}
@Override
public Class<? extends Annotation> annotationType() {
return SuppressWarnings.class;
}
}

View File

@ -579,6 +579,11 @@ public class ScalaWriter extends AbstractCodeWriter<ScalaWriter> {
return line("@SuppressWarnings(\"" + type + "\")");
}
@Override
public CodeWriter suppressWarnings(String... types) throws IOException {
return annotation(new MultiSuppressWarnings(types));
}
private <T> Parameter[] transform(Collection<T> parameters,
Function<T, Parameter> transformer) {
Parameter[] rv = new Parameter[parameters.size()];

View File

@ -45,7 +45,7 @@ public class JavaWriterTest {
private static void match(String resource, String text) throws IOException {
// TODO : try to compile ?
String expected = Resources.toString(JavaWriterTest.class.getResource(resource), Charsets.UTF_8)
String expected = Resources.toString(JavaWriterTest.class.getResource(resource), Charsets.UTF_8)
.replace("\r\n", "\n").trim();
String actual = text.trim();
assertEquals(expected, actual);
@ -335,4 +335,12 @@ public class JavaWriterTest {
match("/testSuppressWarnings", w.toString());
}
@Test
public void SuppressWarnings2() throws IOException {
writer.suppressWarnings("all", "unused");
writer.privateField(Types.STRING, "test");
match("/testSuppressWarnings2", w.toString());
}
}

View File

@ -0,0 +1,2 @@
@SuppressWarnings({"all", "unused"})
private String test;