Formatter: Add wrapping support for parameter/argument lists

#KT-6032 Fixed
This commit is contained in:
Alexey Sedunov
2014-11-26 17:42:55 +03:00
parent c6541ccc6f
commit 8f2c543f7e
22 changed files with 612 additions and 21 deletions
@@ -120,6 +120,7 @@ public class JetBlock extends AbstractBlock {
List<Block> blocks = new ArrayList<Block>();
NodeAlignmentStrategy childrenAlignmentStrategy = getChildrenAlignmentStrategy();
WrappingStrategy wrappingStrategy = getWrappingStrategy();
for (ASTNode child = myNode.getFirstChildNode(); child != null; child = child.getTreeNext()) {
IElementType childType = child.getElementType();
@@ -130,23 +131,28 @@ public class JetBlock extends AbstractBlock {
continue;
}
blocks.add(buildSubBlock(child, childrenAlignmentStrategy));
blocks.add(buildSubBlock(child, childrenAlignmentStrategy, wrappingStrategy));
}
return Collections.unmodifiableList(blocks);
}
@NotNull
private Block buildSubBlock(@NotNull ASTNode child, NodeAlignmentStrategy alignmentStrategy) {
private Block buildSubBlock(
@NotNull ASTNode child,
NodeAlignmentStrategy alignmentStrategy,
@NotNull WrappingStrategy wrappingStrategy) {
Wrap wrap = wrappingStrategy.getWrap(child.getElementType());
// Skip one sub-level for operators, so type of block node is an element type of operator
if (child.getElementType() == OPERATION_REFERENCE) {
ASTNode operationNode = child.getFirstChildNode();
if (operationNode != null) {
return new JetBlock(operationNode, alignmentStrategy, createChildIndent(child), null, mySettings, mySpacingBuilder);
return new JetBlock(operationNode, alignmentStrategy, createChildIndent(child), wrap, mySettings, mySpacingBuilder);
}
}
return new JetBlock(child, alignmentStrategy, createChildIndent(child), null, mySettings, mySpacingBuilder);
return new JetBlock(child, alignmentStrategy, createChildIndent(child), wrap, mySettings, mySpacingBuilder);
}
private static ASTNode getPrevWithoutWhitespace(ASTNode node) {
@@ -225,6 +231,36 @@ public class JetBlock extends AbstractBlock {
return myNode.getFirstChildNode() == null;
}
@NotNull
private static WrappingStrategy getWrappingStrategyForItemList(int wrapType, @NotNull final IElementType itemType) {
final Wrap itemWrap = Wrap.createWrap(wrapType, false);
return new WrappingStrategy() {
@Nullable
@Override
public Wrap getWrap(@NotNull IElementType childElementType) {
return childElementType == itemType ? itemWrap : null;
}
};
}
@NotNull
private WrappingStrategy getWrappingStrategy() {
CommonCodeStyleSettings commonSettings = mySettings.getCommonSettings(JetLanguage.INSTANCE);
IElementType elementType = myNode.getElementType();
if (elementType == VALUE_ARGUMENT_LIST) {
return getWrappingStrategyForItemList(commonSettings.CALL_PARAMETERS_WRAP, VALUE_ARGUMENT);
}
if (elementType == VALUE_PARAMETER_LIST) {
IElementType parentElementType = myNode.getTreeParent().getElementType();
if (parentElementType == FUN || parentElementType == CLASS) {
return getWrappingStrategyForItemList(commonSettings.METHOD_PARAMETERS_WRAP, VALUE_PARAMETER);
}
}
return WrappingStrategy.NoWrapping.INSTANCE$;
}
private NodeAlignmentStrategy getChildrenAlignmentStrategy() {
final CommonCodeStyleSettings jetCommonSettings = mySettings.getCommonSettings(JetLanguage.INSTANCE);
JetCodeStyleSettings jetSettings = mySettings.getCustomSettings(JetCodeStyleSettings.class);
@@ -38,9 +38,7 @@ public class JetLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSetti
case WRAPPING_AND_BRACES_SETTINGS:
return
"public class ThisIsASampleClass {\n" +
" private fun foo1(i1: Int,\n" +
" i2: Int,\n" +
" i3: Int) : Int {\n" +
" fun foo1(i1: Int, i2: Int, i3: Int) : Int {\n" +
" when (i1) {\n" +
" is Number -> 0\n" +
" else -> 1\n" +
@@ -49,10 +47,7 @@ public class JetLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSetti
" }\n" +
" private fun foo2():Int {\n" +
" try {" +
" return foo1(12,\n" +
" 13,\n" +
" 14\n" +
" )\n" +
" return foo1(12, 13, 14)\n" +
" }" +
" catch (e: Exception) {" +
" return 0" +
@@ -160,7 +155,9 @@ public class JetLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSetti
"ELSE_ON_NEW_LINE",
"WHILE_ON_NEW_LINE",
"CATCH_ON_NEW_LINE",
"FINALLY_ON_NEW_LINE"
"FINALLY_ON_NEW_LINE",
"CALL_PARAMETERS_WRAP",
"METHOD_PARAMETERS_WRAP"
);
consumer.renameStandardOption(CodeStyleSettingsCustomizable.WRAPPING_SWITCH_STATEMENT, "'when' statements");
consumer.showCustomOption(JetCodeStyleSettings.class, "ALIGN_IN_COLUMNS_CASE_BRANCH", "Align in columns 'case' branches",
@@ -0,0 +1,30 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.plugin.formatter
import com.intellij.formatting.Wrap
import com.intellij.lang.ASTNode
import com.intellij.psi.tree.IElementType
import com.intellij.formatting.WrapType
public trait WrappingStrategy {
fun getWrap(childElementType: IElementType): Wrap?
object NoWrapping: WrappingStrategy {
override fun getWrap(childElementType: IElementType): Wrap? = null
}
}
@@ -0,0 +1,21 @@
// SET_INT: CALL_PARAMETERS_WRAP = 4
// SET_TRUE: ALIGN_MULTILINE_PARAMETERS_IN_CALLS
// RIGHT_MARGIN: 30
fun foo() {
testtest()
testtesttesttesttesttesttesttesttest()
testtest(foofoo)
testtesttesttest(
foofoofoofoofoofoofoofoofoofoofoofoo)
testtest(foobar, barfoo)
testtesttesttest(foofoo,
barbar,
foobar,
barfoo)
}
@@ -0,0 +1,17 @@
// SET_INT: CALL_PARAMETERS_WRAP = 4
// SET_TRUE: ALIGN_MULTILINE_PARAMETERS_IN_CALLS
// RIGHT_MARGIN: 30
fun foo() {
testtest()
testtesttesttesttesttesttesttesttest()
testtest(foofoo)
testtesttesttest(foofoofoofoofoofoofoofoofoofoofoofoo)
testtest(foobar, barfoo)
testtesttesttest(foofoo, barbar, foobar, barfoo)
}
@@ -0,0 +1,17 @@
// SET_INT: CALL_PARAMETERS_WRAP = 0
// SET_TRUE: ALIGN_MULTILINE_PARAMETERS_IN_CALLS
// RIGHT_MARGIN: 30
fun foo() {
testtest()
testtesttesttesttesttesttesttesttest()
testtest(foofoo)
testtesttesttest(foofoofoofoofoofoofoofoofoofoofoofoo)
testtest(foobar, barfoo)
testtesttesttest(foofoo, barbar, foobar, barfoo)
}
@@ -0,0 +1,17 @@
// SET_INT: CALL_PARAMETERS_WRAP = 0
// SET_TRUE: ALIGN_MULTILINE_PARAMETERS_IN_CALLS
// RIGHT_MARGIN: 30
fun foo() {
testtest()
testtesttesttesttesttesttesttesttest()
testtest(foofoo)
testtesttesttest(foofoofoofoofoofoofoofoofoofoofoofoo)
testtest(foobar, barfoo)
testtesttesttest(foofoo, barbar, foobar, barfoo)
}
@@ -0,0 +1,21 @@
// SET_INT: CALL_PARAMETERS_WRAP = 2
// SET_TRUE: ALIGN_MULTILINE_PARAMETERS_IN_CALLS
// RIGHT_MARGIN: 30
fun foo() {
testtest()
testtesttesttesttesttesttesttesttest()
testtest(foofoo)
testtesttesttest(foofoofoofoofoofoofoofoofoofoofoofoo)
testtest(foobar,
barfoo)
testtesttesttest(foofoo,
barbar,
foobar,
barfoo)
}
@@ -0,0 +1,17 @@
// SET_INT: CALL_PARAMETERS_WRAP = 2
// SET_TRUE: ALIGN_MULTILINE_PARAMETERS_IN_CALLS
// RIGHT_MARGIN: 30
fun foo() {
testtest()
testtesttesttesttesttesttesttesttest()
testtest(foofoo)
testtesttesttest(foofoofoofoofoofoofoofoofoofoofoofoo)
testtest(foobar, barfoo)
testtesttesttest(foofoo, barbar, foobar, barfoo)
}
@@ -0,0 +1,21 @@
// SET_INT: CALL_PARAMETERS_WRAP = 1
// SET_TRUE: ALIGN_MULTILINE_PARAMETERS_IN_CALLS
// RIGHT_MARGIN: 30
fun foo() {
testtest()
testtesttesttesttesttesttesttesttest()
testtest(foofoo)
testtesttesttest(
foofoofoofoofoofoofoofoofoofoofoofoo)
testtest(foobar, barfoo)
testtesttesttest(foofoo,
barbar,
foobar,
barfoo)
}
@@ -0,0 +1,17 @@
// SET_INT: CALL_PARAMETERS_WRAP = 1
// SET_TRUE: ALIGN_MULTILINE_PARAMETERS_IN_CALLS
// RIGHT_MARGIN: 30
fun foo() {
testtest()
testtesttesttesttesttesttesttesttest()
testtest(foofoo)
testtesttesttest(foofoofoofoofoofoofoofoofoofoofoofoo)
testtest(foobar, barfoo)
testtesttesttest(foofoo, barbar, foobar, barfoo)
}
@@ -0,0 +1,36 @@
// SET_INT: METHOD_PARAMETERS_WRAP = 4
// SET_TRUE: ALIGN_MULTILINE_PARAMETERS
// RIGHT_MARGIN: 30
fun testtest()
fun testtesttesttesttesttesttesttesttest()
fun testtest(foofoo: Int)
fun testtesttesttest(foofoo: Int)
fun test(foo: Int, bar: Int)
fun testtesttesttest(foofoo: Int,
barbar: Int,
foobar: Int,
barfoo: Int)
fun test() {
for (foo: Int in bar) {
}
for (foofoofoofoofoofoo: Int in bar) {
}
try {
} catch (bar: Exception) {
} catch (barbarbarbarbar: Exception) {
}
}
@@ -0,0 +1,33 @@
// SET_INT: METHOD_PARAMETERS_WRAP = 4
// SET_TRUE: ALIGN_MULTILINE_PARAMETERS
// RIGHT_MARGIN: 30
fun testtest()
fun testtesttesttesttesttesttesttesttest()
fun testtest(foofoo: Int)
fun testtesttesttest(foofoo: Int)
fun test(foo: Int, bar: Int)
fun testtesttesttest(foofoo: Int, barbar: Int, foobar: Int, barfoo: Int)
fun test() {
for (foo: Int in bar) {
}
for (foofoofoofoofoofoo: Int in bar) {
}
try {
} catch (bar: Exception) {
} catch (barbarbarbarbar: Exception) {
}
}
@@ -0,0 +1,33 @@
// SET_INT: METHOD_PARAMETERS_WRAP = 0
// SET_TRUE: ALIGN_MULTILINE_PARAMETERS
// RIGHT_MARGIN: 30
fun testtest()
fun testtesttesttesttesttesttesttesttest()
fun testtest(foofoo: Int)
fun testtesttesttest(foofoo: Int)
fun test(foo: Int, bar: Int)
fun testtesttesttest(foofoo: Int, barbar: Int, foobar: Int, barfoo: Int)
fun test() {
for (foo: Int in bar) {
}
for (foofoofoofoofoofoo: Int in bar) {
}
try {
} catch (bar: Exception) {
} catch (barbarbarbarbar: Exception) {
}
}
@@ -0,0 +1,33 @@
// SET_INT: METHOD_PARAMETERS_WRAP = 0
// SET_TRUE: ALIGN_MULTILINE_PARAMETERS
// RIGHT_MARGIN: 30
fun testtest()
fun testtesttesttesttesttesttesttesttest()
fun testtest(foofoo: Int)
fun testtesttesttest(foofoo: Int)
fun test(foo: Int, bar: Int)
fun testtesttesttest(foofoo: Int, barbar: Int, foobar: Int, barfoo: Int)
fun test() {
for (foo: Int in bar) {
}
for (foofoofoofoofoofoo: Int in bar) {
}
try {
} catch (bar: Exception) {
} catch (barbarbarbarbar: Exception) {
}
}
@@ -0,0 +1,37 @@
// SET_INT: METHOD_PARAMETERS_WRAP = 2
// SET_TRUE: ALIGN_MULTILINE_PARAMETERS
// RIGHT_MARGIN: 30
fun testtest()
fun testtesttesttesttesttesttesttesttest()
fun testtest(foofoo: Int)
fun testtesttesttest(foofoo: Int)
fun test(foo: Int,
bar: Int)
fun testtesttesttest(foofoo: Int,
barbar: Int,
foobar: Int,
barfoo: Int)
fun test() {
for (foo: Int in bar) {
}
for (foofoofoofoofoofoo: Int in bar) {
}
try {
} catch (bar: Exception) {
} catch (barbarbarbarbar: Exception) {
}
}
@@ -0,0 +1,33 @@
// SET_INT: METHOD_PARAMETERS_WRAP = 2
// SET_TRUE: ALIGN_MULTILINE_PARAMETERS
// RIGHT_MARGIN: 30
fun testtest()
fun testtesttesttesttesttesttesttesttest()
fun testtest(foofoo: Int)
fun testtesttesttest(foofoo: Int)
fun test(foo: Int, bar: Int)
fun testtesttesttest(foofoo: Int, barbar: Int, foobar: Int, barfoo: Int)
fun test() {
for (foo: Int in bar) {
}
for (foofoofoofoofoofoo: Int in bar) {
}
try {
} catch (bar: Exception) {
} catch (barbarbarbarbar: Exception) {
}
}
@@ -0,0 +1,38 @@
// SET_INT: METHOD_PARAMETERS_WRAP = 1
// SET_TRUE: ALIGN_MULTILINE_PARAMETERS
// RIGHT_MARGIN: 30
fun testtest()
fun testtesttesttesttesttesttesttesttest()
fun testtest(foofoo: Int)
fun testtesttesttest(
foofoo: Int)
fun test(foo: Int, bar: Int)
fun testtesttesttest(
foofoo: Int,
barbar: Int,
foobar: Int,
barfoo: Int)
fun test() {
for (foo: Int in bar) {
}
for (foofoofoofoofoofoo: Int in bar) {
}
try {
} catch (bar: Exception) {
} catch (barbarbarbarbar: Exception) {
}
}
@@ -0,0 +1,33 @@
// SET_INT: METHOD_PARAMETERS_WRAP = 1
// SET_TRUE: ALIGN_MULTILINE_PARAMETERS
// RIGHT_MARGIN: 30
fun testtest()
fun testtesttesttesttesttesttesttesttest()
fun testtest(foofoo: Int)
fun testtesttesttest(foofoo: Int)
fun test(foo: Int, bar: Int)
fun testtesttesttest(foofoo: Int, barbar: Int, foobar: Int, barfoo: Int)
fun test() {
for (foo: Int in bar) {
}
for (foofoofoofoofoofoo: Int in bar) {
}
try {
} catch (bar: Exception) {
} catch (barbarbarbarbar: Exception) {
}
}
@@ -27,11 +27,14 @@ import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.testFramework.LightIdeaTestCase;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.InTextDirectivesUtils;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.plugin.JetLanguage;
import org.jetbrains.jet.plugin.PluginTestCaseBase;
import org.jetbrains.jet.testing.SettingsConfigurator;
@@ -138,16 +141,23 @@ public abstract class AbstractJetFormatterTest extends LightIdeaTestCase {
String testFileName = expectedFileNameWithExtension.substring(0, expectedFileNameWithExtension.indexOf("."));
String testFileExtension = expectedFileNameWithExtension.substring(expectedFileNameWithExtension.lastIndexOf("."));
String originalFileText = FileUtil.loadFile(new File(testFileName + testFileExtension), true);
SettingsConfigurator configurator = JetFormatSettingsUtil.createConfigurator(originalFileText, JetFormatSettingsUtil.getSettings());
CodeStyleSettings codeStyleSettings = JetFormatSettingsUtil.getSettings();
Integer rightMargin = InTextDirectivesUtils.getPrefixedInt(originalFileText, "// RIGHT_MARGIN: ");
if (rightMargin != null) {
codeStyleSettings.setRightMargin(JetLanguage.INSTANCE, rightMargin);
}
SettingsConfigurator configurator = JetFormatSettingsUtil.createConfigurator(originalFileText, codeStyleSettings);
if (!inverted) {
configurator.configureSettings();
}
else {
configurator.configureInvertedSettings();
}
doTextTest(originalFileText, new File(expectedFileNameWithExtension), testFileExtension);
JetFormatSettingsUtil.getSettings().clearCodeStyleSettings();
codeStyleSettings.clearCodeStyleSettings();
}
}
@@ -33,7 +33,7 @@ import java.util.regex.Pattern;
public class JetFormatterTestGenerated extends AbstractJetFormatterTest {
@TestMetadata("idea/testData/formatter")
@TestDataPath("$PROJECT_ROOT")
@InnerTestClasses({Formatter.ModifierList.class})
@InnerTestClasses({Formatter.ModifierList.class, Formatter.ParameterList.class})
@RunWith(JUnit3RunnerWithInners.class)
public static class Formatter extends AbstractJetFormatterTest {
public void testAllFilesPresentInFormatter() throws Exception {
@@ -612,6 +612,63 @@ public class JetFormatterTestGenerated extends AbstractJetFormatterTest {
doTest(fileName);
}
}
@TestMetadata("idea/testData/formatter/parameterList")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ParameterList extends AbstractJetFormatterTest {
public void testAllFilesPresentInParameterList() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/formatter/parameterList"), Pattern.compile("^([^\\.]+)\\.after\\.kt.*$"), true);
}
@TestMetadata("ArgumentListChopAsNeeded.after.kt")
public void testArgumentListChopAsNeeded() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/parameterList/ArgumentListChopAsNeeded.after.kt");
doTest(fileName);
}
@TestMetadata("ArgumentListDoNotWrap.after.kt")
public void testArgumentListDoNotWrap() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/parameterList/ArgumentListDoNotWrap.after.kt");
doTest(fileName);
}
@TestMetadata("ArgumentListWrapAlways.after.kt")
public void testArgumentListWrapAlways() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/parameterList/ArgumentListWrapAlways.after.kt");
doTest(fileName);
}
@TestMetadata("ArgumentListWrapAsNeeded.after.kt")
public void testArgumentListWrapAsNeeded() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/parameterList/ArgumentListWrapAsNeeded.after.kt");
doTest(fileName);
}
@TestMetadata("ParameterListChopAsNeeded.after.kt")
public void testParameterListChopAsNeeded() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/parameterList/ParameterListChopAsNeeded.after.kt");
doTest(fileName);
}
@TestMetadata("ParameterListDoNotWrap.after.kt")
public void testParameterListDoNotWrap() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/parameterList/ParameterListDoNotWrap.after.kt");
doTest(fileName);
}
@TestMetadata("ParameterListWrapAlways.after.kt")
public void testParameterListWrapAlways() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/parameterList/ParameterListWrapAlways.after.kt");
doTest(fileName);
}
@TestMetadata("ParameterListWrapAsNeeded.after.kt")
public void testParameterListWrapAsNeeded() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/formatter/parameterList/ParameterListWrapAsNeeded.after.kt");
doTest(fileName);
}
}
}
@TestMetadata("idea/testData/formatter")
@@ -16,7 +16,13 @@
package org.jetbrains.jet.testing;
import com.intellij.lang.Language;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
import kotlin.Pair;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.InTextDirectivesUtils;
import org.jetbrains.jet.plugin.JetLanguage;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
@@ -26,20 +32,35 @@ import java.util.Arrays;
public class SettingsConfigurator {
public static final String SET_TRUE_DIRECTIVE = "SET_TRUE:";
public static final String SET_FALSE_DIRECTIVE = "SET_FALSE:";
public static final String SET_INT_DIRECTIVE = "SET_INT:";
private final String[] settingsToTrue;
private final String[] settingsToFalse;
private final Pair<String, Integer>[] settingsToIntValue;
private final Object[] objects;
public SettingsConfigurator(String fileText, Object... objects) {
settingsToTrue = InTextDirectivesUtils.findArrayWithPrefixes(fileText, SET_TRUE_DIRECTIVE);
settingsToFalse = InTextDirectivesUtils.findArrayWithPrefixes(fileText, SET_FALSE_DIRECTIVE);
//noinspection unchecked
settingsToIntValue = ContainerUtil.map2Array(
InTextDirectivesUtils.findArrayWithPrefixes(fileText, SET_INT_DIRECTIVE),
Pair.class,
new Function<String, Pair>() {
@SuppressWarnings("unchecked")
@Override
public Pair fun(String s) {
String[] tokens = s.split("=");
return new Pair(tokens[0].trim(), Integer.valueOf(tokens[1].trim()));
}
}
);
this.objects = objects;
}
public static void setBooleanSetting(String settingName, boolean value, Object... objects) {
public static void setSettingValue(String settingName, Object value, Class<?> valueType, Object... objects) {
for (Object object : objects) {
if (setSettingWithField(settingName, object, value) || setSettingWithMethod(settingName, object, value)) {
if (setSettingWithField(settingName, object, value) || setSettingWithMethod(settingName, object, value, valueType)) {
return;
}
}
@@ -48,6 +69,14 @@ public class SettingsConfigurator {
"There's no property or method with name '%s' in given objects: %s", settingName, Arrays.toString(objects)));
}
public static void setBooleanSetting(String setting, Boolean value, Object... objects) {
setSettingValue(setting, value, boolean.class, objects);
}
public static void setIntSetting(String setting, Integer value, Object... objects) {
setSettingValue(setting, value, int.class, objects);
}
public void configureSettings() {
for (String trueSetting : settingsToTrue) {
setBooleanSetting(trueSetting, true, objects);
@@ -56,6 +85,10 @@ public class SettingsConfigurator {
for (String falseSetting : settingsToFalse) {
setBooleanSetting(falseSetting, false, objects);
}
for (Pair<String, Integer> setting : settingsToIntValue) {
setIntSetting(setting.getFirst(), setting.getSecond(), objects);
}
}
public void configureInvertedSettings() {
@@ -66,12 +99,16 @@ public class SettingsConfigurator {
for (String falseSetting : settingsToFalse) {
setBooleanSetting(falseSetting, true, objects);
}
for (Pair<String, Integer> setting : settingsToIntValue) {
setIntSetting(setting.getFirst(), setting.getSecond(), objects);
}
}
private static boolean setSettingWithField(String settingName, Object object, boolean value) {
private static boolean setSettingWithField(String settingName, Object object, Object value) {
try {
Field field = object.getClass().getDeclaredField(settingName);
field.setBoolean(object, value);
field.set(object, value);
return true;
}
catch (IllegalAccessException e) {
@@ -84,9 +121,9 @@ public class SettingsConfigurator {
return false;
}
private static boolean setSettingWithMethod(String setterName, Object object, boolean value) {
private static boolean setSettingWithMethod(String setterName, Object object, Object value, Class<?> valueType) {
try {
Method method = object.getClass().getMethod(setterName, boolean.class);
Method method = object.getClass().getMethod(setterName, valueType);
method.invoke(object, value);
return true;
}