- POCメモ
【参照情報】
https://docs.aws.amazon.com/codeguru/latest/reviewer-ug/tutorial-github-reviewer.html
マネジメントコンソールにログインしてリポジトリの関連付けの設定。
ローカルにソースコードをクローンし、ブランチ作成してテスト用のソースコードをコミットしてpushする。
C:\Users\gtoru>git clone https://github.com/t-hashi/amazon-codeguru-reviewer-sample-app.git
Cloning into 'amazon-codeguru-reviewer-sample-app'...
remote: Enumerating objects: 48, done.
remote: Counting objects: 100% (48/48), done.
remote: Compressing objects: 100% (35/35), done.
remote: Total 48 (delta 8), reused 38 (delta 5), pack-reused 0
Unpacking objects: 100% (48/48), 333.27 KiB | 233.00 KiB/s, done.
C:\Users\gtoru>cd amazon-codeguru-reviewer-sample-app
C:\Users\gtoru\amazon-codeguru-reviewer-sample-app>git switch -c develop
Switched to a new branch 'develop'
C:\Users\gtoru\amazon-codeguru-reviewer-sample-app>git add --all
エクスプローラでファイルコピー
コピーファイル: src/main/java/com/shipmentEvents/handlers/EventHandler.java
コピー先:src/main/java/com/shipmentEvents/demo/
C:\Users\gtoru\amazon-codeguru-reviewer-sample-app>git commit -m "new demo file"
[develop 5172a56] new demo file
1 file changed, 174 insertions(+)
create mode 100644 src/main/java/com/shipmentEvents/demo/EventHandler.java
C:\Users\gtoru\amazon-codeguru-reviewer-sample-app>git push --set-upstream origin develop
Enumerating objects: 16, done.
Counting objects: 100% (16/16), done.
Delta compression using up to 8 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (9/9), 2.95 KiB | 1006.00 KiB/s, done.
Total 9 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote:
remote: Create a pull request for 'develop' on GitHub by visiting:
remote: https://github.com/t-hashi/amazon-codeguru-reviewer-sample-app/pull/new/develop
remote:
To https://github.com/t-hashi/amazon-codeguru-reviewer-sample-app.git
* [new branch] develop -> develop
Branch 'develop' set up to track remote branch 'develop' from 'origin'.
gitでpullリクエストしてしばらくするとレビューが完了する。
しばらくすると
- AWS SDK以外のコード例
- リソースリーク(DB)
package com.shipmentEvents.handlers;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class TestBean {
public void doBusiness() {
Connection con = null;
try {
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/oracle");
con = ds.getConnection();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select col1 from test where col1 = 1");
int recordCount = 0;
while (rs.next()) {
System.out.println(rs.getInt("col1"));
recordCount++;
}
System.out.println("recordCount:" + recordCount);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
//con.close(); リソースリークさせるコードのため、わざと処理コードを省略している。
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
※finallyで処理コード漏れした点についてレコメンデーションしてくれた。
※実際にApache Tomcat/9.0.31で設定したコネクションプールでコネクション上限数以上は待ち状態となりSQL実行されない。
- AWS SDK以外のコード例
- リソースリーク(ファイル)
package com.shipmentEvents.handlers;
import java.io.File;
import java.io.FileWriter;
public class TestBean2 {
public void doBusiness() {
try {
File file = new File("c:¥¥temp¥¥test.txt");
FileWriter fw = new FileWriter(file);
fw.write("test");
//fw.close();
} catch (Exception ex) {
}
}
}
※ファイルのリソースハンドリングについてのレコメンデーション。
※ただしこのレベルであれば次のようにEclipseで教えてくれる。