| // |
| // Licensed to the Apache Software Foundation (ASF) under one |
| // or more contributor license agreements. See the NOTICE file |
| // distributed with this work for additional information |
| // regarding copyright ownership. The ASF licenses this file |
| // to you 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. |
| // |
| |
| = 在 NetBeans IDE Java 编辑器中通过“检查并转换”重构 |
| :jbake-type: tutorial |
| :jbake-tags: tutorials |
| :jbake-status: published |
| :icons: font |
| :syntax: true |
| :source-highlighter: pygments |
| :toc: left |
| :toc-title: |
| :description: 在 NetBeans IDE Java 编辑器中通过“检查并转换”重构 - Apache NetBeans |
| :keywords: Apache NetBeans, Tutorials, 在 NetBeans IDE Java 编辑器中通过“检查并转换”重构 |
| |
| NetBeans IDE Java 编辑器具备“检查并转换”功能,这是一种重构工具,用于对选定的文件作用域同时运行各种检查和转换操作。它还允许用户定义定制检查。 |
| |
| 在本教程中,您将学习如何使用包含重构的检查和一组预定义检查来执行代码检查、管理配置并创建定制检查。 |
| |
| |
| |
| image::images/netbeans-stamp.png[title="此页上的内容适用于 NetBeans IDE 8.0"] |
| |
| |
| |
| == 要求 |
| |
| *要学完本教程,您需要具备以下软件和资源。* |
| |
| |=== |
| |软件或资源 |要求的版本 |
| |
| |link:http://netbeans.org/downloads/index.html[+NetBeans IDE+] |版本 8.0 |
| |
| |link:http://www.oracle.com/technetwork/java/javase/downloads/index.html[+Java 开发工具包+] (JDK) |版本 7 或 8 |
| |=== |
| |
| |
| == 设置教程环境 |
| |
| 除了上面列出的所需软件,您还应按如下方式将 link:https://netbeans.org/projects/samples/downloads/download/Samples/Java/inspecttransform.zip[+InspectTransform.zip+] 文件解压缩到 NetBeans 项目文件夹并在 NetBeans IDE 中打开: |
| |
| image::images/proj.png[] |
| |
| |
| == 运行检查 |
| |
| "Inspect and Transform"(检查并转换)对话框("Refactor"(重构)> "Inspect and Transform"(检查并转换))中的 "Single Inspection"(单个检查)选项提供了很多与特定重构转换相关的检查。单击 "Single Inspection"(单个检查)选项右侧的 "Browse"(浏览)按钮后,"Manage Inspections"(管理检查)对话框中将提供此类操作的完整列表。 |
| |
| [.feature] |
| -- |
| |
| image::images/manage-inspections-small.png[role="left", link="images/manage-inspections.png"] |
| |
| -- |
| |
| 通过 "Search"(搜索)字段,可以检查是否可以使用关键字查找执行所需的检查。例如,搜索 ``operator`` 时,可以找到多个检查。 |
| |
| [.feature] |
| -- |
| |
| image::images/search-small.png[role="left", link="images/search.png"] |
| |
| -- |
| |
| 提供的每个检查都在 "Manage Inspection"(管理检查)对话框的 "Description"(描述)字段中具有描述。 |
| |
| *运行检查并应用现有的检查:* |
| |
| 1. 在 "Projects"(项目)窗口中,选择 ``InspectTransform.java`` 文件。 |
| 2. 从 IDE 的菜单中选择 "Refactor"(重构)> "Inspect and Transform"(检查并转换)。 |
| 3. 在 "Inspect and Transform"(检查并转换)对话框中,选择 "Single Inspection"(单个检查)选项并单击 "Browse"(浏览)。 |
| 4. 在 "Manage Inspections"(管理检查)对话框的 "Assignment Issues"(赋值问题)节点中,选择 ``Assignment replaceable with operator-assignment`` (赋值可替换为运算符赋值)。 |
| 5. 单击 "OK"(确定)可关闭 "Manage Inspections"(管理检查)对话框。 |
| 6. 在 "Inspect and Transform"(检查并转换)对话框中单击 "Inspect"(检查)。 |
| 建议的重构更改显示在预览面板中。 |
| |
| [.feature] |
| -- |
| |
| image::images/single-inspection-small.png[role="left", link="images/single-inspection.png"] |
| |
| -- |
| |
| |
| |
| . 单击 "Do Refactoring"(执行重构)以应用所做的更改(如有必要)。 |
| |
| <<top,返回页首>> |
| |
| |
| == 使用配置 |
| |
| 组合在一起的一组重构构成了可以在您感兴趣的作用域(例如一个类或多个类、包或项目)上运行的配置。IDE 提供了下面两种预定义配置: |
| |
| * <<migrate5,迁移至 JDK 5>> |
| * <<convert,迁移至 JDK 7>> |
| * <<migrate8,迁移至 JDK 8>> |
| * <<organize,组织导入>> |
| |
| |
| === 迁移至 JDK 5 |
| |
| 默认情况下,IDE 中提供的 ``Migrate to JDK 5`` (迁移至 JDK 5)配置包含静态导入、JDK 5 for 循环用法、不必要的装箱和拆箱以及 switch over 字符串应用等重构操作。 |
| |
| [.feature] |
| -- |
| |
| image::images/jdk5-conf-small.png[role="left", link="images/jdk5-conf.png"] |
| |
| -- |
| |
| *注:*通过选择和取消选择 ``JDK Migration Support`` (JDK 迁移支持)节点下方的项,可以在 "Manage Inspections"(管理检查)对话框的 "Inspections"(检查)列表中修改配置中的重构列表。 |
| |
| |
| === 迁移至 JDK 7 |
| |
| 默认情况下,IDE 中提供的 ``Migrate to JDK 7`` (迁移至 JDK 7)配置包含菱形运算符使用、try-with-resources 转换、multicatch 使用、switch over 字符串应用等重构操作。 |
| |
| [.feature] |
| -- |
| |
| image::images/jdk7-conf-small.png[role="left", link="images/jdk7-conf.png"] |
| |
| -- |
| |
| *注:*通过选择和取消选择 ``JDK Migration Support`` (JDK 迁移支持)节点下方的项,可以在 "Manage Inspections"(管理检查)对话框的 "Inspections"(检查)列表中修改配置中的重构列表。 |
| |
| *运行并应用默认的 ``Migrate to JDK 7`` (迁移至 JDK 7)配置:* |
| |
| 1. 在 "Projects"(项目)窗口中,选择 ``PredefinedSet.java`` 文件。 |
| 2. 从 IDE 的菜单中选择 "Refactor"(重构)> "Inspect and Transform"(检查并转换)。 |
| 3. 在 "Inspect and Transform"(检查并转换)对话框中,选择 "Configuration"(配置)选项,然后从下拉列表中选择 ``Migrate to JDK 7`` (迁移至 JDK 7)检查集。 |
| 4. 单击 "Inspect"(检查)。 |
| 将代码转换为 JDK 7 语法的更改将显示在预览面板中。 |
| |
| [.feature] |
| -- |
| |
| image::images/jdk7-ref-small.png[role="left", link="images/jdk7-ref.png"] |
| |
| -- |
| |
| |
| |
| . 单击 "Do Refactoring"(执行重构)以应用所做的更改(如有必要)。 |
| |
| |
| === 迁移至 JDK 8 |
| |
| 默认情况下,IDE 中提供的 ``Migrate to JDK 8`` (迁移至 JDK 8)配置包含 Lambda 或成员引用转换、静态导入、multicatch 使用、switch over 字符串应用等重构操作。 |
| |
| [.feature] |
| -- |
| |
| image::images/jdk8-conf-small.png[role="left", link="images/jdk8-conf.png"] |
| |
| -- |
| |
| *注:*通过选择和取消选择 ``JDK Migration Support`` (JDK 迁移支持)节点下方的项,可以在 "Manage Inspections"(管理检查)对话框的 "Inspections"(检查)列表中修改配置中的重构列表。 |
| |
| |
| === 组织导入 |
| |
| 通过 ``Organize Imports`` (组织导入)配置,可以检查 import 语句在代码中的组织方式,并根据需要重构代码。默认情况下,其中包括用于检查 import 语句是否与指定代码样式规则相对应的单个检查。 |
| |
| *注:*要针对 import 语句配置代码样式规则,请执行以下操作: |
| |
| 1. 在 IDE 的主工具栏中,选择 "Tools"(工具)> "Options"(选项)> "Editor"(编辑器)> "Formatting"(格式设置)。 |
| 2. 在 "Language"(语言)下拉列表中,选择 "Java"。 |
| 3. 在 "Category"(类别)下拉列表中,选择 "Imports"(导入)。 |
| 4. 根据需要指定可用选项。 |
| |
| [.feature] |
| -- |
| |
| image::images/org-imports-small.png[role="left", link="images/org-imports.png"] |
| |
| -- |
| |
| |
| |
| . 单击 "OK"(确定)保存所做的编辑。 |
| |
| *运行并应用默认的 ``Organize Imports`` (组织导入)配置:* |
| |
| 1. 在 "Projects"(项目)窗口中,选择 ``Imports.java`` 文件。 |
| 2. 从 IDE 的菜单中选择 "Refactor"(重构)> "Inspect and Transform"(检查并转换)。 |
| 3. 在 "Inspect and Transform"(检查并转换)对话框中,选择 "Configuration"(配置)选项,然后选择 ``Organize Imports`` (组织导入)项。 |
| 4. 单击 "Inspect"(检查)。 |
| 预览面板将显示一个针对 ``Imports.java`` 文件的 "Import"(导入)部分建议的实例,以使其与指定的代码样式规则相符。 |
| |
| [.feature] |
| -- |
| |
| image::images/imports-ref-small.png[role="left", link="images/imports-ref.png"] |
| |
| -- |
| |
| |
| |
| . 单击 "Do Refactoring"(执行重构)以应用所做的更改(如有必要)。 |
| |
| <<top,返回页首>> |
| |
| |
| == 管理定制检查 |
| |
| 可以创建定制检查,以指示 IDE 查找哪些代码结构以及如何对其进行转换。 |
| |
| *注:*为了避免将重复检查添加到 IDE 中,请从主菜单中选择 "Refactor"(重构)> "Inspect and Transform"(检查并转换),单击 "Manage"(管理)或 "Browse"(浏览),然后在 "Manage Inspections"(管理检查)对话框中使用 "Search"(搜索)字段查找所需的检查,然后再创建新的检查。 |
| |
| *创建定制检查:* |
| |
| 1. 从 IDE 的菜单中选择 "Refactor"(重构)> "Inspect and Transform"(检查并转换)。 |
| 2. 在 "Inspect and Transform"(检查并转换)对话框中,单击 "Manage"(管理)或 "Browse"(浏览)。 |
| 3. 在 "Manage Inspections"(管理检查)对话框中,单击 "New"(新建)。 |
| 此时,将在 "Inspections"(检查)列表中创建 ``"Custom"(定制)> "Inspection"(检查)`` 节点。 |
| |
| [.feature] |
| -- |
| |
| image::images/custom-hint-small.png[role="left", link="images/custom-hint.png"] |
| |
| -- |
| |
| |
| |
| . (可选)右键单击 ``Inspection`` (检查),从弹出式菜单中选择 "Rename"(重命名),指定定制检查所需的名称(例如 ``MyCustomInspection`` ),然后按 Enter 键。 |
| |
| |
| . 单击 "Edit Script"(编缉脚本)。显示 "Script"(脚本)文本区域。 |
| |
| [.feature] |
| -- |
| |
| image::images/script-small.png[role="left", link="images/script.png"] |
| |
| -- |
| |
| |
| |
| . 在 "Script"(脚本)文本区域中键入检查描述和代码,或者在编辑器中单击 "Open"(打开),并在 ``MyCustomInspection.hint`` 文件中指定相同的内容。 |
| |
| [.feature] |
| -- |
| |
| image::images/hint-file-small.png[role="left", link="images/hint-file.png"] |
| |
| -- |
| |
| |
| |
| . 单击 "Script"(脚本)文本区域下方的 "Save"(保存),或在编辑器中按 Ctrl-S 组合键保存所做的编辑。 |
| |
| |
| . 单击 "OK"(确定)以关闭 "Manage Inspections"(管理检查)对话框,或在编辑器中关闭 ``MyCustomInspection.hint`` 文件。 |
| 您的定制检查已完成并准备进行应用。 |
| |
| *运行已创建的定制检查:* |
| |
| 1. 从 IDE 的菜单中选择 "Refactor"(重构)> "Inspect and Transform"(检查并转换)。 |
| 2. 在 "Inspect and Transform"(检查并转换)对话框的 "Inspect"(检查)列表中,指定要检查的文件、包或项目。此外,也可以单击右侧的按钮打开 "Custom Scope"(定制作用域)对话框,然后指定要检查的定制代码。 |
| 3. 选择 "Single Inspection"(单个检查)选项,然后选择 ``MyCustomInspection`` 检查。 |
| |
| [.feature] |
| -- |
| |
| image::images/mycustomhint-small.png[role="left", link="images/mycustomhint.png"] |
| |
| -- |
| |
| |
| |
| . 单击 "Inspect"(检查)。 |
| 建议的重构更改显示在预览面板中。 |
| |
| |
| . 单击 "Do Refactoring"(执行重构)以应用所做的更改(如有必要)。 |
| |
| <<top,返回页首>> |
| |
| |
| == 小结 |
| |
| 本教程介绍了“检查并转换”功能最常见的用法。请注意,通过“检查并转换”功能,您还可以对项目作用域执行定制重构,或者对 IDE 中打开的多个项目应用特定的重构配置,等等。 |
| |
| <<top,返回页首>> |
| |
| link:/about/contact_form.html?to=3&subject=Feedback:%20Refactoring%20with%20Inspect%20and%20Transform%20in%20the%20NetBeans%20IDE%20Java%20Editor[+发送有关此教程的反馈意见+] |
| |
| |
| |
| == 另请参见 |
| |
| 相关资料请参见以下文档: |
| |
| * _使用 NetBeans IDE 开发应用程序_中的link:http://www.oracle.com/pls/topic/lookup?ctx=nb8000&id=NBDAG613[+在源代码分析和重构中使用提示+] |
| * link:http://wiki.netbeans.org/Java_Hints[+NetBeans Java 提示的完整列表+] |
| * link:http://wiki.netbeans.org/JavaDeclarativeHintsDescriptionSketch[+NetBeans Java 声明提示的说明+] |
| * link:code-inspect.html[+NetBeans IDE Java 编辑器中的静态代码分析+] |
| * link:http://wiki.netbeans.org/Refactoring[+简化重构+] |
| * link:http://platform.netbeans.org/tutorials/nbm-java-hint.html[+NetBeans Java 提示模块教程+] |
| |
| <<top,返回页首>> |
| |