33 * SPDX-License-Identifier: GPL-2.0-only */
44package de .uka .ilkd .key .testgen ;
55
6- import com .squareup .javapoet .*;
7- import de .uka .ilkd .key .logic .sort .Sort ;
8-
9- import javax .lang .model .element .Modifier ;
106import java .util .HashMap ;
117import java .util .HashSet ;
8+ import javax .lang .model .element .Modifier ;
9+
10+ import de .uka .ilkd .key .logic .sort .Sort ;
11+
12+ import com .squareup .javapoet .*;
1213
1314/**
1415 * Creates the RFL.java file, that provides setter and getter methods using the reflection API as
@@ -28,10 +29,10 @@ public class ReflectionClassCreator {
2829
2930 // setter and getter methods will be created for these types.
3031 private static final String [] PRIMITIVE_TYPES =
31- { "int" , "long" , "byte" , "char" , "boolean" , "float" , "double" };
32+ { "int" , "long" , "byte" , "char" , "boolean" , "float" , "double" };
3233
3334 // Default values for primitive types
34- private static final String [] PRIM_TYP_DEF_VAL = {"0" , "0" , "0" , "' '" , "false" , "0" , "0" };
35+ private static final String [] PRIM_TYP_DEF_VAL = { "0" , "0" , "0" , "' '" , "false" , "0" , "0" };
3536
3637 private final HashSet <Sort > usedObjectSorts ;
3738 private final HashSet <String > usedObjectSortsStrings ;
@@ -110,21 +111,23 @@ private HashSet<String> sortsToString() {
110111 private TypeSpec .Builder classDecl (boolean staticClass ) {
111112 var tb = TypeSpec .classBuilder (NAME_OF_CLASS );
112113 tb .addModifiers (Modifier .PUBLIC );
113- if (staticClass ) tb .addModifiers (Modifier .STATIC );
114- tb .addJavadoc ("""
115- // This file was generated by KeY Version %s (www.key-project.org).
116- /** This class enables the test suite to read and write protected and private
117- * fields of other classes. It can also simulate ghost fields using a hashmap.
118- * Ghostfields are implicit fields that exist in the specification but not in the
119- * actual Java class. Futhermore, this class also enables to create an object of
120- * any class even if it has no default constructor. To create objects the
121- * the objenesis library is required and must be provided when compiling and
122- * executing the test suite. * @see http://docs.oracle.com/javase/tutorial/reflect/member/ctorInstance.html
123- * @see http://code.google.com/p/objenesis
124- * @see http://objenesis.org/
125- * @author gladisch").append(NEW_LINE);
126- * @author mbender").append(NEW_LINE);
127- */""" );
114+ if (staticClass )
115+ tb .addModifiers (Modifier .STATIC );
116+ tb .addJavadoc (
117+ """
118+ // This file was generated by KeY Version %s (www.key-project.org).
119+ /** This class enables the test suite to read and write protected and private
120+ * fields of other classes. It can also simulate ghost fields using a hashmap.
121+ * Ghostfields are implicit fields that exist in the specification but not in the
122+ * actual Java class. Futhermore, this class also enables to create an object of
123+ * any class even if it has no default constructor. To create objects the
124+ * the objenesis library is required and must be provided when compiling and
125+ * executing the test suite. * @see http://docs.oracle.com/javase/tutorial/reflect/member/ctorInstance.html
126+ * @see http://code.google.com/p/objenesis
127+ * @see http://objenesis.org/
128+ * @author gladisch").append(NEW_LINE);
129+ * @author mbender").append(NEW_LINE);
130+ */""" );
128131 return tb ;
129132 }
130133
@@ -137,14 +140,15 @@ private void ghostMapDecls(TypeSpec.Builder clazz) {
137140 clazz .addField (String .class , "NoSuchFieldExceptionText" , Modifier .PUBLIC , Modifier .STATIC );
138141 clazz .addField (Boolean .TYPE , "ghostMapActive" , Modifier .PUBLIC , Modifier .STATIC );
139142 clazz .addField (ParameterizedTypeName .get (HashMap .class , Integer .class , Object .class ),
140- "ghostModelFields" ,
141- Modifier .PUBLIC , Modifier .STATIC );
143+ "ghostModelFields" ,
144+ Modifier .PUBLIC , Modifier .STATIC );
142145 clazz .addMethod (MethodSpec .methodBuilder ("getHash" )
143146 .addModifiers (Modifier .PUBLIC , Modifier .STATIC )
144147 .addParameter (Class .class , "c" )
145148 .addParameter (Object .class , "obj" )
146149 .addParameter (String .class , "attr" )
147- .addStatement ("return c.hashCode() * (obj!=null?obj.hashCode():1) * attr.hashCode();" )
150+ .addStatement (
151+ "return c.hashCode() * (obj!=null?obj.hashCode():1) * attr.hashCode();" )
148152 .build ());
149153 }
150154
@@ -158,7 +162,8 @@ private void instanceMethod(TypeSpec.Builder clazz) {
158162 .returns (Object .class )
159163 .addParameter (Class .class , "c" )
160164 .addException (Exception .class )
161- .addJavadoc ("The Objenesis library can create instances of classes that have no default constructor." )
165+ .addJavadoc (
166+ "The Objenesis library can create instances of classes that have no default constructor." )
162167 .addStatement ("Object res=objenesis.newInstance(c);" )
163168 .beginControlFlow ("if (res==null)" )
164169 .addStatement ("throw new Exception($S+c);" , "Couldn't create instance of class: " )
@@ -169,24 +174,25 @@ private void instanceMethod(TypeSpec.Builder clazz) {
169174
170175 private void staticInitializer (boolean ghostMapActive , TypeSpec .Builder clazz ) {
171176 clazz .addStaticBlock (
172- CodeBlock .builder ()
173- .addStatement ("objenesis = new org.objenesis.ObjenesisStd();" )
174- .add ("ghostMapActive = $L;" , ghostMapActive )
175- .add ("ghostModelFields = new java.util.HashMap<Integer,Object>();" )
176- .add ("NoSuchFieldExceptionText =" )
177- .addStatement ("/*This exception occurs when ghost fields or model fields are used in " +
178- "the code or if mock objects are used that have different fields, than the real objects." +
179- "The tester should extend the handling of such fields in this generated utility class RFL.java.*/;)"
180- ).build ()
181- );
177+ CodeBlock .builder ()
178+ .addStatement ("objenesis = new org.objenesis.ObjenesisStd();" )
179+ .add ("ghostMapActive = $L;" , ghostMapActive )
180+ .add ("ghostModelFields = new java.util.HashMap<Integer,Object>();" )
181+ .add ("NoSuchFieldExceptionText =" )
182+ .addStatement (
183+ "/*This exception occurs when ghost fields or model fields are used in " +
184+ "the code or if mock objects are used that have different fields, than the real objects."
185+ +
186+ "The tester should extend the handling of such fields in this generated utility class RFL.java.*/;)" )
187+ .build ());
182188 }
183189
184190 /**
185191 * All calls to create objects for the given sorts
186192 */
187193 private void instances (final HashSet <String > sorts , TypeSpec .Builder clazz ) {
188- //r.append(NEW_LINE).append(" // ---The methods for object creation---").append(NEW_LINE)
189- // .append(NEW_LINE);
194+ // r.append(NEW_LINE).append(" // ---The methods for object creation---").append(NEW_LINE)
195+ // .append(NEW_LINE);
190196 for (final String sort : sorts ) {
191197 clazz .addMethod (newRef (sort ));
192198 }
@@ -236,8 +242,8 @@ private MethodSpec newInstance(final String sort) {
236242 .returns (returnType )
237243 .addException (RuntimeException .class )
238244 .addStatement ("try{ return ($N) newInstance($N.class); } " +
239- "catch (java.lang.Throwable e) { throw new java.lang.RuntimeException(e); }" ,
240- returnType )
245+ "catch (java.lang.Throwable e) { throw new java.lang.RuntimeException(e); }" ,
246+ returnType )
241247 .build ();
242248 }
243249
@@ -287,15 +293,16 @@ private MethodSpec declareSetter(final String sort, final boolean prim) {
287293
288294 if (prim )
289295 ms .addStatement ("f.set" + Character .toUpperCase (sort .charAt (0 )) + sort .substring (1 )
290- + "(obj, val);" );
296+ + "(obj, val);" );
291297 else
292298 ms .addStatement ("f.set(obj, val);" );
293299
294300 ms .nextControlFlow ("catch(NoSuchFieldException e)" )
295301 .beginControlFlow ("if(ghostMapActive)" )
296302 .addStatement ("ghostModelFields.put(getHash(c,obj,attr), val);" )
297303 .nextControlFlow ("else" )
298- .addStatement ("throw new RuntimeException(e.toString() + NoSuchFieldExceptionText);" )
304+ .addStatement (
305+ "throw new RuntimeException(e.toString() + NoSuchFieldExceptionText);" )
299306 .endControlFlow ()
300307 .nextControlFlow ("catch(Exception e)" )
301308 .addStatement ("throw new RuntimeException(e);" )
@@ -327,7 +334,7 @@ private MethodSpec declareGetter(final String sort, final String def, final bool
327334
328335 if (prim )
329336 ms .addStatement ("return f.get" + Character .toUpperCase (sort .charAt (0 ))
330- + sort .substring (1 ) + "(obj);" );
337+ + sort .substring (1 ) + "(obj);" );
331338 else
332339 ms .addStatement ("return ($N) f.get(obj);" , retType );
333340
0 commit comments