Correct java settings cloning

This commit is contained in:
Rustam Vishnyakov
2017-08-14 13:08:51 +03:00
parent 0ce97c1005
commit 719c65aec8
2 changed files with 85 additions and 6 deletions
@@ -50,10 +50,10 @@ public class JavaCodeStyleSettings extends CustomCodeStyleSettings implements Im
public String VISIBILITY = "public";
public final CodeStyleSettings.TypeToNameMap FIELD_TYPE_TO_NAME = new CodeStyleSettings.TypeToNameMap();
public final CodeStyleSettings.TypeToNameMap STATIC_FIELD_TYPE_TO_NAME = new CodeStyleSettings.TypeToNameMap();
@NonNls public final CodeStyleSettings.TypeToNameMap PARAMETER_TYPE_TO_NAME = new CodeStyleSettings.TypeToNameMap();
public final CodeStyleSettings.TypeToNameMap LOCAL_VARIABLE_TYPE_TO_NAME = new CodeStyleSettings.TypeToNameMap();
public CodeStyleSettings.TypeToNameMap FIELD_TYPE_TO_NAME = new CodeStyleSettings.TypeToNameMap();
public CodeStyleSettings.TypeToNameMap STATIC_FIELD_TYPE_TO_NAME = new CodeStyleSettings.TypeToNameMap();
public CodeStyleSettings.TypeToNameMap PARAMETER_TYPE_TO_NAME = new CodeStyleSettings.TypeToNameMap();
public CodeStyleSettings.TypeToNameMap LOCAL_VARIABLE_TYPE_TO_NAME = new CodeStyleSettings.TypeToNameMap();
public boolean USE_EXTERNAL_ANNOTATIONS;
public boolean INSERT_OVERRIDE_ANNOTATION = true;
@@ -108,8 +108,8 @@ public class JavaCodeStyleSettings extends CustomCodeStyleSettings implements Im
public boolean INSERT_INNER_CLASS_IMPORTS;
public int CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND = 5;
public int NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND = 3;
public final PackageEntryTable PACKAGES_TO_USE_IMPORT_ON_DEMAND = new PackageEntryTable();
public final PackageEntryTable IMPORT_LAYOUT_TABLE = new PackageEntryTable();
public PackageEntryTable PACKAGES_TO_USE_IMPORT_ON_DEMAND = new PackageEntryTable();
public PackageEntryTable IMPORT_LAYOUT_TABLE = new PackageEntryTable();
// region JavaDoc
public boolean ENABLE_JAVADOC_FORMATTING = true;
@@ -313,6 +313,26 @@ public class JavaCodeStyleSettings extends CustomCodeStyleSettings implements Im
JD_INDENT_ON_CONTINUATION = rootSettings.JD_INDENT_ON_CONTINUATION;
}
@Override
public Object clone() {
JavaCodeStyleSettings cloned = (JavaCodeStyleSettings)super.clone();
cloned.myRepeatAnnotations = new ArrayList<>();
cloned.setRepeatAnnotations(getRepeatAnnotations());
cloned.FIELD_TYPE_TO_NAME = new CodeStyleSettings.TypeToNameMap();
cloned.FIELD_TYPE_TO_NAME.copyFrom(FIELD_TYPE_TO_NAME);
cloned.STATIC_FIELD_TYPE_TO_NAME = new CodeStyleSettings.TypeToNameMap();
cloned.STATIC_FIELD_TYPE_TO_NAME.copyFrom(STATIC_FIELD_TYPE_TO_NAME);
cloned.PARAMETER_TYPE_TO_NAME = new CodeStyleSettings.TypeToNameMap();
cloned.PARAMETER_TYPE_TO_NAME.copyFrom(PARAMETER_TYPE_TO_NAME);
cloned.LOCAL_VARIABLE_TYPE_TO_NAME = new CodeStyleSettings.TypeToNameMap();
cloned.LOCAL_VARIABLE_TYPE_TO_NAME.copyFrom(LOCAL_VARIABLE_TYPE_TO_NAME);
cloned.PACKAGES_TO_USE_IMPORT_ON_DEMAND = new PackageEntryTable();
cloned.PACKAGES_TO_USE_IMPORT_ON_DEMAND.copyFrom(PACKAGES_TO_USE_IMPORT_ON_DEMAND);
cloned.IMPORT_LAYOUT_TABLE = new PackageEntryTable();
cloned.IMPORT_LAYOUT_TABLE.copyFrom(IMPORT_LAYOUT_TABLE);
return cloned;
}
@Override
public void readExternal(Element parentElement) throws InvalidDataException {
super.readExternal(parentElement);
@@ -0,0 +1,59 @@
/*
* Copyright 2000-2017 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 com.intellij.java.psi.codeStyle;
import com.intellij.ide.codeStyleSettings.CodeStyleTestCase;
import com.intellij.psi.codeStyle.JavaCodeStyleSettings;
import com.intellij.psi.codeStyle.PackageEntry;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;
public class JavaCodeStyleSettingsTest extends CodeStyleTestCase {
public void testSettingsClone() {
List<String> annotations = Arrays.asList("anno1", "anno2");
JavaCodeStyleSettings original = (JavaCodeStyleSettings)JavaCodeStyleSettings.getInstance(getProject()).clone();
original.getImportLayoutTable().addEntry(new PackageEntry(false, "test", true));
original.setRepeatAnnotations(annotations);
original.getPackagesToUseImportOnDemand().addEntry(new PackageEntry(false, "test2", true));
original.FIELD_TYPE_TO_NAME.addPair("foo", "bar");
original.STATIC_FIELD_TYPE_TO_NAME.addPair("one", "two");
JavaCodeStyleSettings copy = (JavaCodeStyleSettings)original.clone();
assertEquals(annotations, copy.getRepeatAnnotations());
assertEquals("Import tables do not match", original.getImportLayoutTable(), copy.getImportLayoutTable());
assertEquals("On demand packages do not match", original.getPackagesToUseImportOnDemand(), copy.getPackagesToUseImportOnDemand());
assertEquals("Field type-to-name maps don not match", original.FIELD_TYPE_TO_NAME, copy.FIELD_TYPE_TO_NAME);
assertEquals("Static field type-to-name maps don not match", original.STATIC_FIELD_TYPE_TO_NAME, copy.STATIC_FIELD_TYPE_TO_NAME);
}
public void testSettingsCloneNotReferencingOriginal() throws IllegalAccessException {
JavaCodeStyleSettings original = JavaCodeStyleSettings.getInstance(getProject());
JavaCodeStyleSettings copy = (JavaCodeStyleSettings)original.clone();
for (Field field : copy.getClass().getDeclaredFields()) {
if (!isPrimitiveOrString(field.getType()) && (field.getModifiers() & Modifier.PUBLIC) != 0) {
assertNotSame("Fields '" + field.getName() + "' reference the same value", field.get(original), field.get(copy));
}
}
}
private static boolean isPrimitiveOrString(Class type) {
return type.isPrimitive() || type.equals(String.class);
}
}