cleanup code with findbugs plugin

This commit is contained in:
jinyu 2015-07-14 17:03:11 +08:00
parent 9c5c411587
commit 7ca050e952
15 changed files with 63 additions and 53 deletions

View File

@ -1,6 +1,5 @@
package com.foxinmy.weixin4j.api; package com.foxinmy.weixin4j.api;
import java.util.MissingResourceException;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -50,21 +49,15 @@ public abstract class BaseApi {
/** /**
* 默认使用weixin4j.properties文件中的公众号信息 * 默认使用weixin4j.properties文件中的公众号信息
*/ */
public static WeixinAccount DEFAULT_WEIXIN_ACCOUNT; public final static WeixinAccount DEFAULT_WEIXIN_ACCOUNT;
/** /**
* 默认token使用File的方式存储 * 默认token使用File的方式存储
*/ */
public static TokenStorager DEFAULT_TOKEN_STORAGER; public final static TokenStorager DEFAULT_TOKEN_STORAGER;
static { static {
try { DEFAULT_WEIXIN_ACCOUNT = ConfigUtil.getWeixinAccount();
DEFAULT_WEIXIN_ACCOUNT = ConfigUtil.getWeixinAccount();
} catch (MissingResourceException e) {
System.err
.println("'account' key not found in weixin4j.properties file.");
; // error
}
DEFAULT_TOKEN_STORAGER = new FileTokenStorager(ConfigUtil.getValue( DEFAULT_TOKEN_STORAGER = new FileTokenStorager(ConfigUtil.getValue(
"token_path", Weixin4jConst.DEFAULT_TOKEN_PATH)); "token_path", Weixin4jConst.DEFAULT_TOKEN_PATH));
} }

View File

@ -4,10 +4,11 @@ import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.Calendar; import java.util.Calendar;
@ -331,10 +332,9 @@ public class Pay3Api {
BufferedReader reader = null; BufferedReader reader = null;
BufferedWriter writer = null; BufferedWriter writer = null;
FileWriter fw = null;
try { try {
fw = new FileWriter(file); writer = new BufferedWriter(new OutputStreamWriter(
writer = new BufferedWriter(fw); new FileOutputStream(file), Consts.GBK));
reader = new BufferedReader(new InputStreamReader( reader = new BufferedReader(new InputStreamReader(
new ByteArrayInputStream(response.getContent()), new ByteArrayInputStream(response.getContent()),
com.foxinmy.weixin4j.model.Consts.GBK)); com.foxinmy.weixin4j.model.Consts.GBK));
@ -352,7 +352,6 @@ public class Pay3Api {
} }
if (writer != null) { if (writer != null) {
writer.close(); writer.close();
fw.close();
} }
} catch (IOException ignore) { } catch (IOException ignore) {
; ;

View File

@ -16,7 +16,7 @@ public class StringEntity implements HttpEntity {
} }
public StringEntity(String body, ContentType contentType) { public StringEntity(String body, ContentType contentType) {
this.content = body.getBytes(); this.content = body.getBytes(contentType.getCharset());
this.contentType = contentType; this.contentType = contentType;
} }

View File

@ -1,5 +1,6 @@
package com.foxinmy.weixin4j.payment.mch; package com.foxinmy.weixin4j.payment.mch;
import java.io.Serializable;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -229,7 +230,10 @@ public class RedpacketRecord extends XmlResult {
@XmlRootElement @XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD) @XmlAccessorType(XmlAccessType.FIELD)
public static class RedpacketReceiver { public static class RedpacketReceiver implements Serializable {
private static final long serialVersionUID = 1L;
/** /**
* 领取红包的Openid * 领取红包的Openid
*/ */

View File

@ -99,7 +99,15 @@ public class ConfigUtil {
} }
public static WeixinAccount getWeixinAccount() { public static WeixinAccount getWeixinAccount() {
String text = getValue("account"); WeixinAccount account = null;
return JSON.parseObject(text, WeixinAccount.class); try {
String text = getValue("account");
account = JSON.parseObject(text, WeixinAccount.class);
} catch (MissingResourceException e) {
System.err
.println("'account' key not found in weixin4j.properties file.");
; // error
}
return account;
} }
} }

View File

@ -148,11 +148,10 @@ public class ObjectId implements Comparable<ObjectId>, java.io.Serializable {
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) if (this == o)
return true; return true;
if (!(o instanceof ObjectId)) {
ObjectId other = (ObjectId) o;
if (other == null)
return false; return false;
}
ObjectId other = (ObjectId) o;
return _time == other._time && _machine == other._machine return _time == other._time && _machine == other._machine
&& _inc == other._inc; && _inc == other._inc;
} }
@ -310,8 +309,7 @@ public class ObjectId implements Comparable<ObjectId>, java.io.Serializable {
} }
ClassLoader loader = ObjectId.class.getClassLoader(); ClassLoader loader = ObjectId.class.getClassLoader();
int loaderId = loader != null int loaderId = loader != null ? System.identityHashCode(loader)
? System.identityHashCode(loader)
: 0; : 0;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();

View File

@ -74,7 +74,11 @@ public class ListsuffixResultDeserializer {
T t = XmlStream.fromXML(content, clazz); T t = XmlStream.fromXML(content, clazz);
Map<Field, String[]> listsuffixFields = getListsuffixFields(clazz); Map<Field, String[]> listsuffixFields = getListsuffixFields(clazz);
if (!listsuffixFields.isEmpty()) { if (!listsuffixFields.isEmpty()) {
for (Field field : listsuffixFields.keySet()) { Iterator<Entry<Field, String[]>> it = listsuffixFields.entrySet()
.iterator();
while (it.hasNext()) {
Entry<Field, String[]> entry = it.next();
Field field = entry.getKey();
Type type = field.getGenericType(); Type type = field.getGenericType();
Class<?> wrapperClazz = null; Class<?> wrapperClazz = null;
if (type instanceof ParameterizedType) { if (type instanceof ParameterizedType) {
@ -84,7 +88,7 @@ public class ListsuffixResultDeserializer {
continue; continue;
} }
ListWrapper<?> listWrapper = deserializeToListWrapper(content, ListWrapper<?> listWrapper = deserializeToListWrapper(content,
wrapperClazz, listsuffixFields.get(field)); wrapperClazz, entry.getValue());
if (listWrapper != null) { if (listWrapper != null) {
try { try {
field.setAccessible(true); field.setAccessible(true);

View File

@ -6,10 +6,10 @@ import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.security.KeyStore; import java.security.KeyStore;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.security.cert.CertificateFactory; import java.security.cert.CertificateFactory;
@ -209,14 +209,18 @@ public class Pay2Api extends MpApi {
.getInstance(com.foxinmy.weixin4j.model.Consts.JKS); .getInstance(com.foxinmy.weixin4j.model.Consts.JKS);
ks.load(null, null); ks.load(null, null);
ks.setCertificateEntry("tenpay", cert); ks.setCertificateEntry("tenpay", cert);
ks.store(new FileOutputStream(jksFile), jksPwd.toCharArray()); FileOutputStream os = new FileOutputStream(jksFile);
ks.store(os, jksPwd.toCharArray());
os.close();
} }
// load jks ca // load jks ca
TrustManagerFactory tmf = TrustManagerFactory TrustManagerFactory tmf = TrustManagerFactory
.getInstance(com.foxinmy.weixin4j.model.Consts.SunX509); .getInstance(com.foxinmy.weixin4j.model.Consts.SunX509);
ks = KeyStore.getInstance(com.foxinmy.weixin4j.model.Consts.JKS); ks = KeyStore.getInstance(com.foxinmy.weixin4j.model.Consts.JKS);
ks.load(new FileInputStream(jksFile), jksPwd.toCharArray()); FileInputStream is = new FileInputStream(jksFile);
ks.load(is, jksPwd.toCharArray());
tmf.init(ks); tmf.init(ks);
is.close();
// load pfx ca // load pfx ca
KeyManagerFactory kmf = KeyManagerFactory KeyManagerFactory kmf = KeyManagerFactory
.getInstance(com.foxinmy.weixin4j.model.Consts.SunX509); .getInstance(com.foxinmy.weixin4j.model.Consts.SunX509);
@ -373,10 +377,9 @@ public class Pay2Api extends MpApi {
WeixinResponse response = weixinClient.get(downloadbill_uri, map); WeixinResponse response = weixinClient.get(downloadbill_uri, map);
BufferedReader reader = null; BufferedReader reader = null;
BufferedWriter writer = null; BufferedWriter writer = null;
FileWriter fw = null;
try { try {
fw = new FileWriter(file); writer = new BufferedWriter(new OutputStreamWriter(
writer = new BufferedWriter(fw); new FileOutputStream(file), Consts.GBK));
reader = new BufferedReader(new InputStreamReader( reader = new BufferedReader(new InputStreamReader(
new ByteArrayInputStream(response.getContent()), new ByteArrayInputStream(response.getContent()),
com.foxinmy.weixin4j.model.Consts.GBK)); com.foxinmy.weixin4j.model.Consts.GBK));
@ -394,7 +397,6 @@ public class Pay2Api extends MpApi {
} }
if (writer != null) { if (writer != null) {
writer.close(); writer.close();
fw.close();
} }
} catch (IOException ignore) { } catch (IOException ignore) {
; ;

View File

@ -81,7 +81,7 @@ public class AgentApi extends QyApi {
return response.getAsJsonResult(); return response.getAsJsonResult();
} }
public static ValueFilter typeFilter; public final static ValueFilter typeFilter;
static { static {
typeFilter = new ValueFilter() { typeFilter = new ValueFilter() {
@Override @Override

View File

@ -75,7 +75,7 @@ public class BatchApi extends QyApi {
* @param callback * @param callback
* 接收任务执行结果的回调地址等信息 * 接收任务执行结果的回调地址等信息
* @return 异步任务id最大长度为64字符 * @return 异步任务id最大长度为64字符
* @see {@link com.foxinmy.weixin4j.qy.api.MediaApi#batchUploadUsers(List)} * @see {@link com.foxinmy.weixin4j.qy.api.MediaApi#batchUploadUsers(java.util.List)}
* @see com.foxinmy.weixin4j.qy.model.Callback * @see com.foxinmy.weixin4j.qy.model.Callback
* @see <a * @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1%E6%8E%A5%E5%8F%A3#.E5.A2.9E.E9.87.8F.E6.9B.B4.E6.96.B0.E6.88.90.E5.91.98">批量更新成员</a> * href="http://qydev.weixin.qq.com/wiki/index.php?title=%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1%E6%8E%A5%E5%8F%A3#.E5.A2.9E.E9.87.8F.E6.9B.B4.E6.96.B0.E6.88.90.E5.91.98">批量更新成员</a>
@ -113,7 +113,7 @@ public class BatchApi extends QyApi {
* @param callback * @param callback
* 接收任务执行结果的回调地址等信息 * 接收任务执行结果的回调地址等信息
* @return 异步任务id最大长度为64字符 * @return 异步任务id最大长度为64字符
* @see {@link com.foxinmy.weixin4j.qy.api.MediaApi#batchUploadUsers(List)} * @see {@link com.foxinmy.weixin4j.qy.api.MediaApi#batchUploadUsers(java.util.List)}
* @see com.foxinmy.weixin4j.qy.model.Callback * @see com.foxinmy.weixin4j.qy.model.Callback
* @see <a * @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1%E6%8E%A5%E5%8F%A3#.E5.85.A8.E9.87.8F.E8.A6.86.E7.9B.96.E6.88.90.E5.91.98">批量覆盖成员</a> * href="http://qydev.weixin.qq.com/wiki/index.php?title=%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1%E6%8E%A5%E5%8F%A3#.E5.85.A8.E9.87.8F.E8.A6.86.E7.9B.96.E6.88.90.E5.91.98">批量覆盖成员</a>
@ -138,7 +138,7 @@ public class BatchApi extends QyApi {
* @param callback * @param callback
* 接收任务执行结果的回调地址等信息 * 接收任务执行结果的回调地址等信息
* @return 异步任务id最大长度为64字符 * @return 异步任务id最大长度为64字符
* @see {@link com.foxinmy.weixin4j.qy.api.MediaApi#batchUploadParties(List)} * @see {@link com.foxinmy.weixin4j.qy.api.MediaApi#batchUploadParties(java.util.List)}
* @see com.foxinmy.weixin4j.qy.model.Callback * @see com.foxinmy.weixin4j.qy.model.Callback
* @see <a * @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1%E6%8E%A5%E5%8F%A3#.E5.85.A8.E9.87.8F.E8.A6.86.E7.9B.96.E9.83.A8.E9.97.A8">批量覆盖部门</a> * href="http://qydev.weixin.qq.com/wiki/index.php?title=%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1%E6%8E%A5%E5%8F%A3#.E5.85.A8.E9.87.8F.E8.A6.86.E7.9B.96.E9.83.A8.E9.97.A8">批量覆盖部门</a>

View File

@ -16,7 +16,7 @@ import com.foxinmy.weixin4j.util.AesToken;
* @since JDK 1.7 * @since JDK 1.7
* @see * @see
*/ */
public class WeixinRequest implements Serializable, Cloneable { public class WeixinRequest implements Serializable {
private static final long serialVersionUID = -9157395300510879866L; private static final long serialVersionUID = -9157395300510879866L;

View File

@ -89,7 +89,7 @@ public class NewsResponse implements WeixinResponse {
* @since JDK 1.7 * @since JDK 1.7
* @see * @see
*/ */
public class Article { public static class Article {
/** /**
* 图文消息标题 * 图文消息标题
*/ */

View File

@ -67,11 +67,11 @@ public class WeixinMessageDecoder extends
String weixinId = parameters.containsKey("weixin_id") ? parameters.get( String weixinId = parameters.containsKey("weixin_id") ? parameters.get(
"weixin_id").get(0) : null; "weixin_id").get(0) : null;
AesToken aesToken = aesTokenMap.get(weixinId); AesToken aesToken = aesTokenMap.get(weixinId);
if (aesToken == null) { // /*if (aesToken == null) {
AesToken _aesToken = aesTokenMap.get(null); AesToken _aesToken = aesTokenMap.get(null);
aesToken = new AesToken(weixinId, _aesToken.getToken(), aesToken = new AesToken(weixinId, _aesToken.getToken(),
_aesToken.getAesKey()); _aesToken.getAesKey());
} }*/
String originalContent = content; String originalContent = content;
String encryptContent = null; String encryptContent = null;
if (!content.isEmpty() && encryptType == EncryptType.AES) { if (!content.isEmpty() && encryptType == EncryptType.AES) {

View File

@ -11,7 +11,7 @@ import java.io.Serializable;
* @since JDK 1.7 * @since JDK 1.7
* @see * @see
*/ */
public class AesToken implements Serializable, Cloneable { public class AesToken implements Serializable {
private static final long serialVersionUID = -6001008896414323534L; private static final long serialVersionUID = -6001008896414323534L;

View File

@ -72,16 +72,18 @@ public final class ClassUtil {
return file.isDirectory() || file.getName().endsWith(".class"); return file.isDirectory() || file.getName().endsWith(".class");
} }
}); });
for (File file : files) { if (files != null) {
if (file.isDirectory()) { for (File file : files) {
classes.addAll(findClassesByFile(file, if (file.isDirectory()) {
packageName + "." + file.getName())); classes.addAll(findClassesByFile(file, packageName + "."
} else { + file.getName()));
try { } else {
classes.add(Class.forName(packageName + "." try {
+ file.getName().replace(".class", ""))); classes.add(Class.forName(packageName + "."
} catch (ClassNotFoundException e) { + file.getName().replace(".class", "")));
; } catch (ClassNotFoundException e) {
;
}
} }
} }
} }