|
| 1 | +/* This file is part of KeY - https://key-project.org |
| 2 | + * KeY is licensed under the GNU General Public License Version 2 |
| 3 | + * SPDX-License-Identifier: GPL-2.0-only */ |
| 4 | +package de.uka.ilkd.key.ldt; |
| 5 | + |
| 6 | +import de.uka.ilkd.key.proof.init.InitConfig; |
| 7 | +import de.uka.ilkd.key.settings.ProofSettings; |
| 8 | +import org.jspecify.annotations.NonNull; |
| 9 | + |
| 10 | + |
| 11 | +/** |
| 12 | + * A little helper class to resolve the settings for treatment of final fields. |
| 13 | + * |
| 14 | + * During the generation of {@link de.uka.ilkd.key.proof.init.ProofOblInput}s, we need to know |
| 15 | + * which final-treatment is activated. Also during translation from JML to JavaDL this is needed. |
| 16 | + * Unfortunately, the settings are not directly available everywhere, so there is a mechanism to |
| 17 | + * remember the setting while it is available in a thread-local variable. This class provides a |
| 18 | + * simple interface to access this boolean variable. |
| 19 | + * |
| 20 | + * The alternative would be to make the settings available at more spots ... |
| 21 | + * |
| 22 | + * @author Mattias Ulbrich |
| 23 | + */ |
| 24 | +public class FinalHeapResolution { |
| 25 | + |
| 26 | + private static final ThreadLocal<Boolean> finalEnabledVariable = new ThreadLocal<>(); |
| 27 | + private static final String SETTING = "finalFields"; |
| 28 | + private static final String IMMUTABLE_OPTION = SETTING + ":immutable"; |
| 29 | + |
| 30 | + /** |
| 31 | + * Returns whether final fields are treated different from normal fields as immutable data. |
| 32 | + * |
| 33 | + * If initConfig does not have settings yet, the default settings are used. |
| 34 | + * |
| 35 | + * @param initConfig the configuration to read the settings from |
| 36 | + * @return true if final fields are treated as immutable |
| 37 | + */ |
| 38 | + public static boolean isFinalEnabled(@NonNull InitConfig initConfig) { |
| 39 | + ProofSettings settings = initConfig.getSettings(); |
| 40 | + if (settings == null) { |
| 41 | + settings = new ProofSettings(ProofSettings.DEFAULT_SETTINGS); |
| 42 | + } |
| 43 | + return isFinalEnabled(settings); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Returns whether final fields are treated different from normal fields as immutable data. |
| 48 | + * |
| 49 | + * @param settings the settings to read the settings from |
| 50 | + * @return true if final fields are treated as immutable |
| 51 | + */ |
| 52 | + public static boolean isFinalEnabled(@NonNull ProofSettings settings) { |
| 53 | + return settings.getChoiceSettings().getDefaultChoices().get(SETTING) |
| 54 | + .equals(IMMUTABLE_OPTION); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Remembers the final fields are treated different from normal fields as immutable data |
| 59 | + * in a thread-local variable that can be recalled later using {@link #recallIsFinalEnabled()}. |
| 60 | + * |
| 61 | + * @param initConfig the configuration to read the settings from |
| 62 | + */ |
| 63 | + public static void rememberIfFinalEnabled(InitConfig initConfig) { |
| 64 | + finalEnabledVariable.set(isFinalEnabled(initConfig)); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Recall a previously stored status regarding the treatment of final fields. |
| 69 | + * See {@link #rememberIfFinalEnabled(InitConfig)}. |
| 70 | + * |
| 71 | + * @return true if final fields are treated as immutable (as recorded earlier) |
| 72 | + * @throws IllegalStateException if the variable has not been set before |
| 73 | + */ |
| 74 | + |
| 75 | + public static boolean recallIsFinalEnabled() { |
| 76 | + Boolean bool = finalEnabledVariable.get(); |
| 77 | + if (bool == null) { |
| 78 | + throw new IllegalStateException("Unset final enabled variable"); |
| 79 | + } |
| 80 | + return bool.booleanValue(); |
| 81 | + } |
| 82 | +} |
0 commit comments