Skip to content

Commit 127352f

Browse files
author
Martin
committed
fixed #1: now all annotations are processed
1 parent fc7fe2d commit 127352f

File tree

13 files changed

+277
-20
lines changed

13 files changed

+277
-20
lines changed

.idea/modules/enum-mapper-lib/enum-mapper-lib_main.iml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules/enum-mapper-lib/enum-mapper-lib_test.iml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules/enum-mapper-processor/enum-mapper-processor_main.iml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules/enum-mapper-processor/enum-mapper-processor_test.iml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules/lib/enum-mapper-lib.iml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules/processor/enum-mapper-processor.iml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# enum-mapper
66
The main use is an annotation processor that builds an enum mapper which causes a compile-time error when you forget to map an enum.
77

8-
## Simple Example
8+
## Quick Introduction
99
Just annotate your enum with the `@EnumMapper` annotation:
1010
```java
1111
@EnumMapper
@@ -26,8 +26,28 @@ String germanSummer = germanSeasons.getValue(Seasons.SUMMER); // returns "Sommer
2626
```
2727
Should you forget to map any enum-constant or change the enumeration (e.g. add or remove an enum-constant), you will get a compile error.
2828

29-
## Build configuration
29+
Detailed usage examples are also available on the [github enum-mapper-example project](
30+
https://github.com/tmtron/enum-mapper-example)
31+
32+
## Build Configuration
3033
TODO: add info when the project is published
3134

35+
## Full Enum Mapper
36+
37+
### Alternatives
38+
39+
### Pros and Cons
40+
41+
## Partial Enum Mapper
42+
The project also includes a partial enum-mapper class which you may want to use instead of a switch statement.
43+
The partial enum mapper does not need the annotation processor.
44+
45+
46+
47+
### Pros and Cons
48+
49+
50+
51+
3252
## License
3353
This plugin is under the [Apache 2.0 license](http://www.apache.org/licenses/LICENSE-2.0.html). Copyright 2017, Martin Trummer

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,18 @@ public Set<Element> process(SetMultimap<Class<? extends Annotation>, Element> el
5555
// the annotations() method tells the framework which annotations to accept
5656
if (elementsByAnnotation.keys().size() > 2) throw new RuntimeException("Too many annotations");
5757

58-
Class<? extends Annotation> annotation = elementsByAnnotation.keys().iterator().next();
59-
// the elements which are annotated with the annotation
60-
// e.g. the EnumMappers annotation may be present on multiple classes of packages
61-
Set<Element> annotatedElements = elementsByAnnotation.get(annotation);
62-
String annotationName = annotation.getName();
63-
if (annotationName.equals(EnumMappers.class.getCanonicalName())) {
64-
processEnumMappersAnnotation(annotatedElements);
65-
} else if (annotationName.equals(EnumMapper.class.getCanonicalName())) {
66-
processEnumMapperAnnotation(annotatedElements);
67-
} else {
68-
throw new RuntimeException("Unexpected annotation class found: '" + annotationName + "'");
69-
58+
for (Class<? extends Annotation> annotation : elementsByAnnotation.keys()) {
59+
// the elements which are annotated with the annotation
60+
// e.g. the EnumMappers annotation may be present on multiple classes of packages
61+
Set<Element> annotatedElements = elementsByAnnotation.get(annotation);
62+
String annotationName = annotation.getName();
63+
if (annotationName.equals(EnumMappers.class.getCanonicalName())) {
64+
processEnumMappersAnnotation(annotatedElements);
65+
} else if (annotationName.equals(EnumMapper.class.getCanonicalName())) {
66+
processEnumMapperAnnotation(annotatedElements);
67+
} else {
68+
throw new RuntimeException("Unexpected annotation class found: '" + annotationName + "'");
69+
}
7070
}
7171
} catch (Exception e) {
7272
processingEnvironment.getMessager().printMessage(Diagnostic.Kind.ERROR,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
/**
21+
* test for <a href="https://github.com/tmtron/enum-mapper/issues/1">
22+
* Issue 1: Cannot use @EnumMapper and @EnumMappers in the same project</a>
23+
*/
24+
public class TestMappers4AllAnnotations extends AnnotationProcessorTest {
25+
@Test
26+
public void test() throws Exception {
27+
assertAboutEnumsProcessing(getJfoResource("AllAnnotations_Source.java"))
28+
.compilesWithoutWarnings()
29+
.and()
30+
.generatesSources(
31+
getJfoResource("Seasons_MapperFull.java"),
32+
getJfoResource("BoolEnum_MapperFull.java"),
33+
getJfoResource("ColorEnum_MapperFull.java"));
34+
}
35+
36+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
// comment
19+
20+
import com.tmtron.enums.EnumMapper;
21+
import com.tmtron.enums.EnumMappers;
22+
23+
@EnumMappers({AllAnnotations_Source.BoolEnum.class, AllAnnotations_Source.ColorEnum.class})
24+
public class AllAnnotations_Source {
25+
enum BoolEnum {OFF, ON}
26+
27+
enum ColorEnum {RED, BLUE}
28+
29+
@EnumMapper
30+
public enum Seasons {
31+
SPRING, SUMMER, FALL, WINTER
32+
}
33+
}

0 commit comments

Comments
 (0)