56
56
import javax .tools .Diagnostic .Kind ;
57
57
import javax .tools .JavaFileObject ;
58
58
59
+ import com .oracle .graal .python .annotations .GenerateEnumConstants ;
59
60
import com .oracle .graal .python .processor .CodeWriter .Block ;
60
61
61
62
@ SupportedAnnotationTypes ("com.oracle.graal.python.annotations.GenerateEnumConstants" )
@@ -67,59 +68,68 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
67
68
if (re .processingOver ()) {
68
69
return false ;
69
70
}
70
- for ( TypeElement annotation : annotations ) {
71
- Set <? extends Element > annotatedElements = re . getElementsAnnotatedWith ( annotation );
72
- for ( Element el : annotatedElements ) {
73
- if ( el . getKind () == ElementKind . ENUM ) {
74
- try {
75
- Element enc = el . getEnclosingElement ();
76
- int enclosingTypes = 0 ;
77
- OUTER : while ( enc != null ) {
78
- switch ( enc . getKind ()) {
79
- case CLASS :
80
- case ENUM :
81
- case INTERFACE :
82
- enclosingTypes ++;
83
- break ;
84
- default :
85
- break OUTER ;
86
- }
87
- enc . getEnclosingElement () ;
71
+ Set <? extends Element > annotatedElements = re . getElementsAnnotatedWith ( GenerateEnumConstants . class );
72
+ for ( Element el : annotatedElements ) {
73
+ GenerateEnumConstants annotation = el . getAnnotation ( GenerateEnumConstants . class );
74
+ boolean useByte = annotation . type () == GenerateEnumConstants . Type . BYTE ;
75
+ String elementType = useByte ? "byte" : "int" ;
76
+ if ( el . getKind () == ElementKind . ENUM ) {
77
+ try {
78
+ Element enc = el . getEnclosingElement ();
79
+ int enclosingTypes = 0 ;
80
+ OUTER : while ( enc != null ) {
81
+ switch ( enc . getKind ()) {
82
+ case CLASS :
83
+ case ENUM :
84
+ case INTERFACE :
85
+ enclosingTypes ++;
86
+ break ;
87
+ default :
88
+ break OUTER ;
88
89
}
89
- String qualName = ((TypeElement ) el ).getQualifiedName () + "Constants" ;
90
- String pkgName = qualName .substring (0 , qualName .lastIndexOf ('.' ));
91
- String className = qualName .substring (qualName .lastIndexOf ('.' ) + 1 );
90
+ enc .getEnclosingElement ();
91
+ }
92
+ String qualName = ((TypeElement ) el ).getQualifiedName () + "Constants" ;
93
+ String pkgName = qualName .substring (0 , qualName .lastIndexOf ('.' ));
94
+ String className = qualName .substring (qualName .lastIndexOf ('.' ) + 1 );
92
95
93
- while (enclosingTypes -- > 0 ) {
94
- className = pkgName .substring (qualName .lastIndexOf ('.' ) + 1 ) + className ;
95
- pkgName = pkgName .substring (0 , pkgName .lastIndexOf ('.' ));
96
- qualName = pkgName + "." + className ;
97
- }
96
+ while (enclosingTypes -- > 0 ) {
97
+ className = pkgName .substring (qualName .lastIndexOf ('.' ) + 1 ) + className ;
98
+ pkgName = pkgName .substring (0 , pkgName .lastIndexOf ('.' ));
99
+ qualName = pkgName + "." + className ;
100
+ }
98
101
99
- JavaFileObject file = processingEnv .getFiler ().createSourceFile (qualName );
100
- try (CodeWriter w = new CodeWriter (file .openWriter ())) {
101
- w .writeLn ("// CheckStyle: start generated" );
102
- w .writeLn ("// Auto generated by GenerateEnumConstantsProcessor at %s" , LocalDateTime .now ());
103
- w .writeLn ("package %s;" , pkgName );
104
- w .writeLn ();
105
- w .writeLn ("public final class %s {" , className );
106
- int i = 0 ;
107
- for (Element enumBit : el .getEnclosedElements ()) {
108
- if (enumBit .getKind () == ElementKind .ENUM_CONSTANT ) {
109
- String enumName = ((VariableElement ) enumBit ).getSimpleName ().toString ();
110
- try (Block b = w .newIndent ()) {
111
- w .writeLn ("public static final int %s = %d;" , enumName , i ++);
102
+ JavaFileObject file = processingEnv .getFiler ().createSourceFile (qualName );
103
+ try (CodeWriter w = new CodeWriter (file .openWriter ())) {
104
+ w .writeLn ("// CheckStyle: start generated" );
105
+ w .writeLn ("// Auto generated by GenerateEnumConstantsProcessor at %s" , LocalDateTime .now ());
106
+ w .writeLn ("package %s;" , pkgName );
107
+ w .writeLn ();
108
+ w .writeLn ("public final class %s {" , className );
109
+ int i = 0 ;
110
+ for (Element enumBit : el .getEnclosedElements ()) {
111
+ if (enumBit .getKind () == ElementKind .ENUM_CONSTANT ) {
112
+ String enumName = ((VariableElement ) enumBit ).getSimpleName ().toString ();
113
+ try (Block b = w .newIndent ()) {
114
+ int value = i ++;
115
+ if (useByte ) {
116
+ if (value <= 0xFF ) {
117
+ value = (byte ) value ;
118
+ } else {
119
+ throw new IllegalArgumentException ("Enum constant doesn't fit into byte" );
120
+ }
112
121
}
122
+ w .writeLn ("public static final %s %s = %d;" , elementType , enumName , value );
113
123
}
114
124
}
115
- w .writeLn ("}" );
116
125
}
117
- } catch (IOException e ) {
118
- processingEnv .getMessager ().printMessage (Kind .ERROR , e .getMessage (), el );
126
+ w .writeLn ("}" );
119
127
}
120
- } else {
121
- processingEnv .getMessager ().printMessage (Kind .ERROR , "Can only annotate enums with @GenerateEnumConstants" , el );
128
+ } catch ( IOException e ) {
129
+ processingEnv .getMessager ().printMessage (Kind .ERROR , e . getMessage () , el );
122
130
}
131
+ } else {
132
+ processingEnv .getMessager ().printMessage (Kind .ERROR , "Can only annotate enums with @GenerateEnumConstants" , el );
123
133
}
124
134
}
125
135
return true ;
0 commit comments