@@ -118,59 +118,67 @@ private ArgumentClinicData(ArgumentClinic annotation, int index, Set<PrimitiveTy
118118 this .imports = imports ;
119119 }
120120
121- public static ArgumentClinicData create (ArgumentClinic annotation , TypeElement type , BuiltinAnnotation builtinAnnotation , int index , ConverterFactory factory ) throws ProcessingError {
122- if (annotation == null ) {
123- return new ArgumentClinicData (null , index , new HashSet <>(Arrays .asList (PrimitiveType .values ())), null , Collections .emptySet ());
124- }
121+ private static ConverterFactory getFactory (ArgumentClinic annotation , TypeElement type , ConverterFactory factory ) throws ProcessingError {
125122 if (factory == null && annotation .args ().length != 0 ) {
126123 throw new ProcessingError (type , "No conversionClass specified but arguments were provided" );
127124 }
128- PrimitiveType [] acceptedPrimitives = new PrimitiveType [0 ];
129- String castNodeFactory ;
130- Set <String > imports = new HashSet <>();
131- if (annotation .customConversion ().isEmpty ()) {
132- if (factory == null ) {
133- if (annotation .conversion () == ClinicConversion .None && annotation .defaultValue ().isEmpty ()) {
134- throw new ProcessingError (type , "ArgumentClinic annotation must declare either builtin conversion or custom conversion." );
135- }
136- factory = ConverterFactory .getBuiltin (annotation );
137- }
138- if (annotation .args ().length != factory .paramCount ) {
139- throw new ProcessingError (type , "Conversion %s.%s expects %d arguments" , factory .fullClassName , factory .methodName , factory .paramCount );
140- }
141- String args = Stream .concat (
142- Arrays .stream (factory .clinicArgs ).map (ca -> {
143- switch (ca ) {
144- case BuiltinName :
145- return String .format ("\" %s\" " , builtinAnnotation .name );
146- case ArgumentIndex :
147- return String .valueOf (index );
148- case ArgumentName :
149- return String .format ("\" %s\" " , builtinAnnotation .argumentNames [index ]);
150- case DefaultValue :
151- return annotation .defaultValue ();
152- case UseDefaultForNone :
153- return String .valueOf (annotation .useDefaultForNone ());
154- default :
155- throw new IllegalStateException ("Unsupported ClinicArgument: " + ca );
156- }
157- }),
158- Arrays .stream (annotation .args ())).collect (Collectors .joining (", " ));
159- castNodeFactory = String .format ("%s.%s(%s)" , factory .className , factory .methodName , args );
160- imports .add (factory .fullClassName );
161- acceptedPrimitives = factory .acceptedPrimitiveTypes ;
162- if (annotation .defaultValue ().startsWith ("PNone." )) {
163- imports .add ("com.oracle.graal.python.builtins.objects.PNone" );
164- }
165- } else {
125+ if (!annotation .customConversion ().isEmpty ()) {
166126 if (factory != null ) {
167127 throw new ProcessingError (type , "Cannot specify both conversionClass and customConversion" );
168128 }
169- castNodeFactory = type .getQualifiedName ().toString () + '.' + annotation .customConversion () + "()" ;
129+ return ConverterFactory .forCustomConversion (type , annotation .customConversion ());
130+ }
131+ if (factory != null ) {
132+ return factory ;
170133 }
134+ if (annotation .conversion () == ClinicConversion .None && annotation .defaultValue ().isEmpty ()) {
135+ throw new ProcessingError (type , "ArgumentClinic annotation must declare either builtin conversion or custom conversion." );
136+ }
137+ return ConverterFactory .getBuiltin (annotation );
138+ }
139+
140+ public static ArgumentClinicData create (ArgumentClinic annotation , TypeElement type , BuiltinAnnotation builtinAnnotation , int index , ConverterFactory ofactory ) throws ProcessingError {
141+ if (annotation == null ) {
142+ return new ArgumentClinicData (null , index , new HashSet <>(Arrays .asList (PrimitiveType .values ())), null , Collections .emptySet ());
143+ }
144+ ConverterFactory factory = getFactory (annotation , type , ofactory );
145+ if (annotation .args ().length != factory .paramCount ) {
146+ throw new ProcessingError (type , "Conversion %s.%s expects %d arguments" , factory .fullClassName , factory .methodName , factory .paramCount );
147+ }
148+
149+ PrimitiveType [] acceptedPrimitives ;
171150 if (annotation .shortCircuitPrimitive ().length > 0 ) {
172151 acceptedPrimitives = annotation .shortCircuitPrimitive ();
152+ } else {
153+ acceptedPrimitives = factory .acceptedPrimitiveTypes ;
154+ }
155+
156+ String args = Stream .concat (
157+ Arrays .stream (factory .clinicArgs ).map (ca -> {
158+ switch (ca ) {
159+ case BuiltinName :
160+ return String .format ("\" %s\" " , builtinAnnotation .name );
161+ case ArgumentIndex :
162+ return String .valueOf (index );
163+ case ArgumentName :
164+ return String .format ("\" %s\" " , builtinAnnotation .argumentNames [index ]);
165+ case DefaultValue :
166+ return annotation .defaultValue ();
167+ case UseDefaultForNone :
168+ return String .valueOf (annotation .useDefaultForNone ());
169+ default :
170+ throw new IllegalStateException ("Unsupported ClinicArgument: " + ca );
171+ }
172+ }),
173+ Arrays .stream (annotation .args ())).collect (Collectors .joining (", " ));
174+ String castNodeFactory = String .format ("%s.%s(%s)" , factory .className , factory .methodName , args );
175+
176+ Set <String > imports = new HashSet <>();
177+ imports .add (factory .fullClassName );
178+ if (annotation .defaultValue ().startsWith ("PNone." )) {
179+ imports .add ("com.oracle.graal.python.builtins.objects.PNone" );
173180 }
181+
174182 return new ArgumentClinicData (annotation , index , new HashSet <>(Arrays .asList (acceptedPrimitives )), castNodeFactory , imports );
175183 }
176184 }
0 commit comments