Skip to content

Commit c302e89

Browse files
author
Martin
committed
SetMultimap in MapAllEnumsProcessingStep.java was used incorrectly.
Thus, using the @EnumMappers annotation more than once in your project caused a compilation error. This fixes #2
1 parent 952c587 commit c302e89

File tree

5 files changed

+174
-2
lines changed

5 files changed

+174
-2
lines changed

enum-mapper-processor/src/main/java/com/tmtron/enums/processor/MapAllEnumsProcessingStep.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import com.tmtron.enums.EnumMappers;
2424

2525
import java.lang.annotation.Annotation;
26+
import java.util.Collection;
2627
import java.util.Collections;
28+
import java.util.Map;
2729
import java.util.Set;
2830

2931
import javax.annotation.processing.ProcessingEnvironment;
@@ -52,10 +54,12 @@ public Set<? extends Class<? extends Annotation>> annotations() {
5254
@Override
5355
public Set<Element> process(SetMultimap<Class<? extends Annotation>, Element> elementsByAnnotation) {
5456
try {
57+
Map<Class<? extends Annotation>, Collection<Element>> annotationsMap =
58+
elementsByAnnotation.asMap();
5559
// the annotations() method tells the framework which annotations to accept
56-
if (elementsByAnnotation.keys().size() > 2) throw new RuntimeException("Too many annotations");
60+
if (annotationsMap.keySet().size() > 2) throw new RuntimeException("Too many annotations");
5761

58-
for (Class<? extends Annotation> annotation : elementsByAnnotation.keys()) {
62+
for (Class<? extends Annotation> annotation : annotationsMap.keySet()) {
5963
// the elements which are annotated with the annotation
6064
// e.g. the EnumMappers annotation may be present on multiple classes of packages
6165
Set<Element> annotatedElements = elementsByAnnotation.get(annotation);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright © 2017 Martin Trummer (martin.trummer@tmtron.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.tmtron.enums.processor;
17+
18+
import org.junit.Test;
19+
20+
public class TestMappersOn2classes extends AnnotationProcessorTest {
21+
@Test
22+
public void test() throws Exception {
23+
assertAboutEnumsProcessing(getJfoResource("TwoClasses_Source.java"))
24+
.compilesWithoutWarnings()
25+
.and()
26+
.generatesSources(
27+
getJfoResource("BoolEnum_MapperFull.java"),
28+
getJfoResource("ColorEnum_MapperFull.java"));
29+
}
30+
31+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright © 2017 Martin Trummer (martin.trummer@tmtron.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
// GENERATED
17+
package com.test;
18+
19+
import com.tmtron.enums.EnumMapperFull;
20+
21+
import javax.annotation.Generated;
22+
23+
@Generated(
24+
value = "com.tmtron.enums.processor.EnumsAnnotationProcessor",
25+
date = "1976-12-14T15:16:17.234+02:00",
26+
comments = "origin=com.test.TwoClasses_Source.AnnotatedClassA"
27+
)
28+
public final class BoolEnum_MapperFull {
29+
public static <V> IsetON<V> setOFF(V value) {
30+
StagedBuilder<V> result = new StagedBuilder<>();
31+
result.enumMapperBuilder.put(TwoClasses_Source.BoolEnum.OFF, value);
32+
return result;
33+
}
34+
35+
public interface IsetON<V> {
36+
EnumMapperFull<TwoClasses_Source.BoolEnum, V> setON(V value);
37+
}
38+
39+
private static class StagedBuilder<V> implements IsetON<V> {
40+
private final EnumMapperFull.Builder<TwoClasses_Source.BoolEnum, V> enumMapperBuilder = EnumMapperFull.builder
41+
(TwoClasses_Source.BoolEnum.class);
42+
43+
@Override
44+
public EnumMapperFull<TwoClasses_Source.BoolEnum, V> setON(V value) {
45+
enumMapperBuilder.put(TwoClasses_Source.BoolEnum.ON, value);
46+
return enumMapperBuilder.build();
47+
}
48+
}
49+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright © 2017 Martin Trummer (martin.trummer@tmtron.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.test;
17+
18+
import com.tmtron.enums.EnumMapperFull;
19+
20+
import javax.annotation.Generated;
21+
22+
@Generated(
23+
value = "com.tmtron.enums.processor.EnumsAnnotationProcessor",
24+
date = "1976-12-14T15:16:17.234+02:00",
25+
comments = "origin=com.test.TwoClasses_Source.AnnotatedClassB"
26+
)
27+
public final class ColorEnum_MapperFull {
28+
public static <V> IsetBLUE<V> setRED(V value) {
29+
StagedBuilder<V> result = new StagedBuilder<>();
30+
result.enumMapperBuilder.put(TwoClasses_Source.ColorEnum.RED, value);
31+
return result;
32+
}
33+
34+
public interface IsetBLUE<V> {
35+
EnumMapperFull<TwoClasses_Source.ColorEnum, V> setBLUE(V value);
36+
}
37+
38+
private static class StagedBuilder<V> implements IsetBLUE<V> {
39+
private final EnumMapperFull.Builder<TwoClasses_Source.ColorEnum, V> enumMapperBuilder = EnumMapperFull.builder
40+
(TwoClasses_Source.ColorEnum.class);
41+
42+
@Override
43+
public EnumMapperFull<TwoClasses_Source.ColorEnum, V> setBLUE(V value) {
44+
enumMapperBuilder.put(TwoClasses_Source.ColorEnum.BLUE, value);
45+
return enumMapperBuilder.build();
46+
}
47+
}
48+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright © 2017 Martin Trummer (martin.trummer@tmtron.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.test;
17+
18+
/*
19+
* Test if we can put the @EnumMappers annotation on 2 different classes
20+
* did not work in V1.0.1
21+
*/
22+
23+
import com.tmtron.enums.EnumMappers;
24+
25+
public class TwoClasses_Source {
26+
enum BoolEnum {OFF, ON}
27+
28+
enum ColorEnum {RED, BLUE}
29+
30+
@EnumMappers({TwoClasses_Source.BoolEnum.class})
31+
public static class AnnotatedClassA {
32+
33+
}
34+
35+
@EnumMappers({TwoClasses_Source.ColorEnum.class})
36+
public static class AnnotatedClassB {
37+
38+
}
39+
40+
}

0 commit comments

Comments
 (0)