Eclipse创建一个项目后,都会为该项目创建一个.settings目录,可能很少有人会用到这个目录,所以它往往被大家忽略。最近为了在Eclipse自动创建的setter方法和构造函数的参数前面加个前缀,搜索了大量资料之后,最终在.settings目录下找到了解决方案。因此,特地花时间研究了一下该目录的功能。
从目录的命名就可以看出,它是与项目设置相关的。该目录主要包含两个文件:org.eclipse.jdt.core.prefs和org.eclipse.jdt.ui.prefs,利用NotePad++等软件可以查看和编辑这些文件。
1. org.eclipse.jdt.core.prefs
该文件是与Compile相关的配置,一般情况下的内容如下:
[plain] view plain copy
- eclipse.preferences.version=1
- org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
- org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
- org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
- org.eclipse.jdt.core.compiler.compliance=1.6
- org.eclipse.jdt.core.compiler.debug.lineNumber=generate
- org.eclipse.jdt.core.compiler.debug.localVariable=generate
- org.eclipse.jdt.core.compiler.debug.sourceFile=generate
- org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
- org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
- org.eclipse.jdt.core.compiler.source=1.6
可以手动修改该文件,添加一些其他的配置,然后重启Eclipse后就可以生效。例如,前面提到的在Eclipse自动创建的setter方法和构造函数的参数前面加个前缀,只需要在该文件末尾加上org.eclipse.jdt.core.codeComplete.argumentPrefixes=new,就可以在自动创建的方法的所有参数前面加上new前缀。其实,还可以进行其他类似的设置,例如:
[plain] view plain copy
- org.eclipse.jdt.core.codeComplete.argumentPrefixes=
- org.eclipse.jdt.core.codeComplete.argumentSuffixes=
- org.eclipse.jdt.core.codeComplete.fieldPrefixes=
- org.eclipse.jdt.core.codeComplete.fieldSuffixes=
- org.eclipse.jdt.core.codeComplete.localPrefixes=
- org.eclipse.jdt.core.codeComplete.localSuffixes=
- org.eclipse.jdt.core.codeComplete.staticFieldPrefixes=
- org.eclipse.jdt.core.codeComplete.staticFieldSuffixes=
该文件能够进行的配置非常多,没有搜索到具体能够进行哪些配置的详细文档,但是Eclipse的官网上找到一个稍微全面一点的示例文档,可供大家参考。
2. org.eclipse.jdt.ui.prefs
该文件是与code style相关的配置,用来记录通过Eclipse的UI界面对项目的一些格式方面的设置,一般情况下的内容如下:
[plain] view plain copy
- eclipse.preferences.version=1
- editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true
- org.eclipse.jdt.ui.javadoc=true
- org.eclipse.jdt.ui.text.custom_code_templates=<?xml version\="1.0" encoding\="UTF-8"?><templates><template autoinsert\="false" context\="filecomment_context" deleted\="false" description\="Comment for created Java files" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.filecomment" name\="filecomment">/*\r\n * Licensed Materials - Property of IBM\r\n *\r\n * OCO Source Materials\r\n *\r\n * (C) Copyright IBM Corp. ${year} All Rights Reserved\r\n *\r\n * The source code for this program is not published or other-\r\n * wise divested of its trade secrets, irrespective of what has\r\n * been deposited with the U.S. Copyright Office.\r\n */</template><template autoinsert\="false" context\="newtype_context" deleted\="false" description\="Newly created files" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.newtype" name\="newtype">${filecomment}\r\n${package_declaration}\r\n\r\nimport com.ibm.director.fvt.common.Copyright;\r\n\r\n${typecomment}\r\n@Copyright(Copyright.HEADER + "${year}" + Copyright.FOOTER)\r\n${type_declaration}</template><template autoinsert\="false" context\="classbody_context" deleted\="false" description\="Code in new class type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.classbody" name\="classbody"/><template autoinsert\="false" context\="interfacebody_context" deleted\="false" description\="Code in new interface type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.interfacebody" name\="interfacebody"/><template autoinsert\="false" context\="enumbody_context" deleted\="false" description\="Code in new enum type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.enumbody" name\="enumbody"/><template autoinsert\="false" context\="annotationbody_context" deleted\="false" description\="Code in new annotation type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.annotationbody" name\="annotationbody"/><template autoinsert\="true" context\="gettercomment_context" deleted\="false" description\="Comment for getter method" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.gettercomment" name\="gettercomment">/**\r\n * @return the ${bare_field_name}\r\n */</template><template autoinsert\="true" context\="settercomment_context" deleted\="false" description\="Comment for setter method" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.settercomment" name\="settercomment">/**\r\n * @param ${param} the ${bare_field_name} to set\r\n */</template><template autoinsert\="true" context\="constructorcomment_context" deleted\="false" description\="Comment for created constructors" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.constructorcomment" name\="constructorcomment">/**\r\n * ${tags}\r\n */</template><template autoinsert\="false" context\="typecomment_context" deleted\="false" description\="Comment for created types" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.typecomment" name\="typecomment">/**\r\n * @author yuejming@cn.ibm.com\r\n *\r\n * ${tags}\r\n */</template><template autoinsert\="true" context\="fieldcomment_context" deleted\="false" description\="Comment for fields" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.fieldcomment" name\="fieldcomment">/**\r\n * \r\n */</template><template autoinsert\="true" context\="methodcomment_context" deleted\="false" description\="Comment for non-overriding methods" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.methodcomment" name\="methodcomment">/**\r\n * ${tags}\r\n */</template><template autoinsert\="true" context\="overridecomment_context" deleted\="false" description\="Comment for overriding methods" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.overridecomment" name\="overridecomment">/* (non-Javadoc)\r\n * ${see_to_overridden}\r\n */</template><template autoinsert\="true" context\="delegatecomment_context" deleted\="false" description\="Comment for delegate methods" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.delegatecomment" name\="delegatecomment">/**\r\n * ${tags}\r\n * ${see_to_target}\r\n */</template><template autoinsert\="true" context\="catchblock_context" deleted\="false" description\="Code in new catch blocks" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.catchblock" name\="catchblock">// ${todo} Auto-generated catch block\r\n${exception_var}.printStackTrace();</template><template autoinsert\="true" context\="methodbody_context" deleted\="false" description\="Code in created method stubs" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.methodbody" name\="methodbody">// ${todo} Auto-generated method stub\r\n${body_statement}</template><template autoinsert\="true" context\="constructorbody_context" deleted\="false" description\="Code in created constructor stubs" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.constructorbody" name\="constructorbody">${body_statement}\r\n// ${todo} Auto-generated constructor stub</template><template autoinsert\="true" context\="getterbody_context" deleted\="false" description\="Code in created getters" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.getterbody" name\="getterbody">return ${field};</template><template autoinsert\="true" context\="setterbody_context" deleted\="false" description\="Code in created setters" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.setterbody" name\="setterbody">${field} \= ${param};</template></templates>
- sp_cleanup.add_default_serial_version_id=true
- sp_cleanup.add_generated_serial_version_id=false
- sp_cleanup.add_missing_annotations=true
- sp_cleanup.add_missing_deprecated_annotations=true
- sp_cleanup.add_missing_methods=false
- sp_cleanup.add_missing_nls_tags=false
- sp_cleanup.add_missing_override_annotations=true
- sp_cleanup.add_missing_override_annotations_interface_methods=true
- sp_cleanup.add_serial_version_id=false
- sp_cleanup.always_use_blocks=true
- sp_cleanup.always_use_parentheses_in_expressions=false
- sp_cleanup.always_use_this_for_non_static_field_access=false
- sp_cleanup.always_use_this_for_non_static_method_access=false
- sp_cleanup.convert_to_enhanced_for_loop=false
- sp_cleanup.correct_indentation=false
- sp_cleanup.format_source_code=false
- sp_cleanup.format_source_code_changes_only=true
- sp_cleanup.make_local_variable_final=false
- sp_cleanup.make_parameters_final=true
- sp_cleanup.make_private_fields_final=false
- sp_cleanup.make_type_abstract_if_missing_method=false
- sp_cleanup.make_variable_declarations_final=true
- sp_cleanup.never_use_blocks=false
- sp_cleanup.never_use_parentheses_in_expressions=true
- sp_cleanup.on_save_use_additional_actions=true
- sp_cleanup.organize_imports=true
- sp_cleanup.qualify_static_field_accesses_with_declaring_class=false
- sp_cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true
- sp_cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true
- sp_cleanup.qualify_static_member_accesses_with_declaring_class=false
- sp_cleanup.qualify_static_method_accesses_with_declaring_class=false
- sp_cleanup.remove_private_constructors=true
- sp_cleanup.remove_trailing_whitespaces=false
- sp_cleanup.remove_trailing_whitespaces_all=true
- sp_cleanup.remove_trailing_whitespaces_ignore_empty=false
- sp_cleanup.remove_unnecessary_casts=false
- sp_cleanup.remove_unnecessary_nls_tags=false
- sp_cleanup.remove_unused_imports=false
- sp_cleanup.remove_unused_local_variables=false
- sp_cleanup.remove_unused_private_fields=true
- sp_cleanup.remove_unused_private_members=false
- sp_cleanup.remove_unused_private_methods=true
- sp_cleanup.remove_unused_private_types=true
- sp_cleanup.sort_members=false
- sp_cleanup.sort_members_all=false
- sp_cleanup.use_blocks=true
- sp_cleanup.use_blocks_only_for_return_and_throw=false
- sp_cleanup.use_parentheses_in_expressions=false
- sp_cleanup.use_this_for_non_static_field_access=false
- sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true
- sp_cleanup.use_this_for_non_static_method_access=false
- sp_cleanup.use_this_for_non_static_method_access_only_if_necessary=true
上面的示例中主要有javadoc、template和cleanup相关的配置,在Eclipse的UI界面上对它们的设置在《玩转Eclipse — 自动代码规范化》一文中有详细介绍。这个文件就没有太大必要去手动配置,如果真的不知道在UI界面的哪里进行配置,需要手动配置的话,可以Eclipse的官网关于该文件的示例文档。