提交 5be0362d 编写于 作者: Y Yiming Liu

Fix some code warning

上级 0ac426a6
...@@ -28,19 +28,10 @@ ...@@ -28,19 +28,10 @@
<groupId>mysql</groupId> <groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<profiles>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>
</project> </project>
...@@ -23,7 +23,7 @@ public class TitanEntityManager { ...@@ -23,7 +23,7 @@ public class TitanEntityManager {
Object obj = clazz.newInstance(); Object obj = clazz.newInstance();
Method method = clazz.getMethod("createDataSource", new Class[] {String.class, String.class}); Method method = clazz.getMethod("createDataSource", new Class[] {String.class, String.class});
return ((DataSource) method.invoke(obj, return ((DataSource) method.invoke(obj,
new String[] {settings.getTitanDbname(), settings.getTitanUrl()})); new Object[] {settings.getTitanDbname(), settings.getTitanUrl()}));
} }
} }
...@@ -36,12 +36,12 @@ public class ViewService { ...@@ -36,12 +36,12 @@ public class ViewService {
public List<Cluster> findClusters(String appId) { public List<Cluster> findClusters(String appId) {
if (Strings.isNullOrEmpty(appId)) { if (Strings.isNullOrEmpty(appId)) {
return Collections.EMPTY_LIST; return Collections.emptyList();
} }
List<Cluster> clusters = clusterRepository.findByAppId(appId); List<Cluster> clusters = clusterRepository.findByAppId(appId);
if (clusters == null) { if (clusters == null) {
return Collections.EMPTY_LIST; return Collections.emptyList();
} }
return clusters; return clusters;
} }
...@@ -49,7 +49,7 @@ public class ViewService { ...@@ -49,7 +49,7 @@ public class ViewService {
public List<Namespace> findNamespaces(String appId, String clusterName) { public List<Namespace> findNamespaces(String appId, String clusterName) {
List<Namespace> groups = namespaceRepository.findByAppIdAndClusterName(appId, clusterName); List<Namespace> groups = namespaceRepository.findByAppIdAndClusterName(appId, clusterName);
if (groups == null) { if (groups == null) {
return Collections.EMPTY_LIST; return Collections.emptyList();
} }
return groups; return groups;
} }
...@@ -60,14 +60,14 @@ public class ViewService { ...@@ -60,14 +60,14 @@ public class ViewService {
if (group != null) { if (group != null) {
return findItems(group.getId()); return findItems(group.getId());
} else { } else {
return Collections.EMPTY_LIST; return Collections.emptyList();
} }
} }
public List<Item> findItems(Long namespaceId) { public List<Item> findItems(Long namespaceId) {
List<Item> items = itemRepository.findByNamespaceIdOrderByLineNumAsc(namespaceId); List<Item> items = itemRepository.findByNamespaceIdOrderByLineNumAsc(namespaceId);
if (items == null) { if (items == null) {
return Collections.EMPTY_LIST; return Collections.emptyList();
} }
return items; return items;
} }
...@@ -76,7 +76,7 @@ public class ViewService { ...@@ -76,7 +76,7 @@ public class ViewService {
List<Release> releases = releaseRepository.findByAppIdAndClusterNameAndNamespaceName(appId, List<Release> releases = releaseRepository.findByAppIdAndClusterNameAndNamespaceName(appId,
clusterName, namespaceName); clusterName, namespaceName);
if (releases == null) { if (releases == null) {
return Collections.EMPTY_LIST; return Collections.emptyList();
} }
return releases; return releases;
} }
......
package com.ctrip.apollo.common.utils; package com.ctrip.apollo.common.utils;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
...@@ -22,9 +23,9 @@ public class BeanUtils { ...@@ -22,9 +23,9 @@ public class BeanUtils {
* List<UserDTO> userDTOs = BeanUtil.batchTransform(UserDTO.class, userBeans); * List<UserDTO> userDTOs = BeanUtil.batchTransform(UserDTO.class, userBeans);
* </pre> * </pre>
*/ */
public static <T> List<T> batchTransform(final Class<T> clazz, List srcList) { public static <T> List<T> batchTransform(final Class<T> clazz, List<? extends Object> srcList) {
if (CollectionUtils.isEmpty(srcList)) { if (CollectionUtils.isEmpty(srcList)) {
return Collections.EMPTY_LIST; return Collections.emptyList();
} }
List<T> result = new ArrayList<>(srcList.size()); List<T> result = new ArrayList<>(srcList.size());
...@@ -56,12 +57,12 @@ public class BeanUtils { ...@@ -56,12 +57,12 @@ public class BeanUtils {
return instance; return instance;
} }
static String[] getNullPropertyNames(Object source) { private static String[] getNullPropertyNames(Object source) {
final BeanWrapper src = new BeanWrapperImpl(source); final BeanWrapper src = new BeanWrapperImpl(source);
java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors(); PropertyDescriptor[] pds = src.getPropertyDescriptors();
Set<String> emptyNames = new HashSet<String>(); Set<String> emptyNames = new HashSet<String>();
for (java.beans.PropertyDescriptor pd : pds) { for (PropertyDescriptor pd : pds) {
Object srcValue = src.getPropertyValue(pd.getName()); Object srcValue = src.getPropertyValue(pd.getName());
if (srcValue == null) emptyNames.add(pd.getName()); if (srcValue == null) emptyNames.add(pd.getName());
} }
...@@ -74,20 +75,21 @@ public class BeanUtils { ...@@ -74,20 +75,21 @@ public class BeanUtils {
* *
* <pre> * <pre>
* List<UserDTO> userList = userService.queryUsers(); * List<UserDTO> userList = userService.queryUsers();
* Map<Integer, userDTO> userIdToUser = BeanUtil.mapByKey("userId", Integer.class, userList, * Map<Integer, userDTO> userIdToUser = BeanUtil.mapByKey("userId", userList);
* UserDTO.class);
* </pre> * </pre>
* *
* @param key 属性名 * @param key 属性名
*/ */
public static <K, V> Map<K, V> mapByKey(String key, List list) { @SuppressWarnings("unchecked")
public static <K, V> Map<K, V> mapByKey(String key, List<? extends Object> list) {
Map<K, V> map = new HashMap<K, V>(); Map<K, V> map = new HashMap<K, V>();
if (CollectionUtils.isEmpty(list)) { if (CollectionUtils.isEmpty(list)) {
return map; return map;
} }
try { try {
Class clazz = list.get(0).getClass(); Class<? extends Object> clazz = list.get(0).getClass();
Field field = deepFindField(clazz, key); Field field = deepFindField(clazz, key);
if (field == null) throw new IllegalArgumentException("Could not find the key");
field.setAccessible(true); field.setAccessible(true);
for (Object o : list) { for (Object o : list) {
map.put((K) field.get(o), (V) o); map.put((K) field.get(o), (V) o);
...@@ -106,14 +108,16 @@ public class BeanUtils { ...@@ -106,14 +108,16 @@ public class BeanUtils {
* Map<Integer, List<ShopDTO>> city2Shops = BeanUtil.aggByKeyToList("cityId", shopList); * Map<Integer, List<ShopDTO>> city2Shops = BeanUtil.aggByKeyToList("cityId", shopList);
* </pre> * </pre>
*/ */
public static <K, V> Map<K, List<V>> aggByKeyToList(String key, List list) { @SuppressWarnings("unchecked")
public static <K, V> Map<K, List<V>> aggByKeyToList(String key, List<? extends Object> list) {
Map<K, List<V>> map = new HashMap<K, List<V>>(); Map<K, List<V>> map = new HashMap<K, List<V>>();
if (CollectionUtils.isEmpty(list)) {// 防止外面传入空list if (CollectionUtils.isEmpty(list)) {// 防止外面传入空list
return map; return map;
} }
try { try {
Class clazz = list.get(0).getClass(); Class<? extends Object> clazz = list.get(0).getClass();
Field field = deepFindField(clazz, key); Field field = deepFindField(clazz, key);
if (field == null) throw new IllegalArgumentException("Could not find the key");
field.setAccessible(true); field.setAccessible(true);
for (Object o : list) { for (Object o : list) {
K k = (K) field.get(o); K k = (K) field.get(o);
...@@ -136,20 +140,19 @@ public class BeanUtils { ...@@ -136,20 +140,19 @@ public class BeanUtils {
* Set<Integer> userIds = BeanUtil.toPropertySet("userId", userList); * Set<Integer> userIds = BeanUtil.toPropertySet("userId", userList);
* </pre> * </pre>
*/ */
public static Set toPropertySet(String key, List list) { @SuppressWarnings("unchecked")
Set set = new HashSet(); public static <K> Set<K> toPropertySet(String key, List<? extends Object> list) {
Set<K> set = new HashSet<K>();
if (CollectionUtils.isEmpty(list)) {// 防止外面传入空list if (CollectionUtils.isEmpty(list)) {// 防止外面传入空list
return set; return set;
} }
try { try {
Class clazz = list.get(0).getClass(); Class<? extends Object> clazz = list.get(0).getClass();
Field field = deepFindField(clazz, key); Field field = deepFindField(clazz, key);
if (field == null) { if (field == null) throw new IllegalArgumentException("Could not find the key");
return set;
}
field.setAccessible(true); field.setAccessible(true);
for (Object o : list) { for (Object o : list) {
set.add(field.get(o)); set.add((K)field.get(o));
} }
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
...@@ -158,7 +161,7 @@ public class BeanUtils { ...@@ -158,7 +161,7 @@ public class BeanUtils {
} }
private static Field deepFindField(Class clazz, String key) { private static Field deepFindField(Class<? extends Object> clazz, String key) {
Field field = null; Field field = null;
while (!clazz.getName().equals(Object.class.getName())) { while (!clazz.getName().equals(Object.class.getName())) {
try { try {
...@@ -204,10 +207,6 @@ public class BeanUtils { ...@@ -204,10 +207,6 @@ public class BeanUtils {
} }
} }
public static List toPropertyList(String key, List list) {
return new ArrayList(toPropertySet(key, list));
}
/** /**
* *
* @param source * @param source
...@@ -216,7 +215,7 @@ public class BeanUtils { ...@@ -216,7 +215,7 @@ public class BeanUtils {
public static void copyProperties(Object source, Object target, String... ignoreProperties) { public static void copyProperties(Object source, Object target, String... ignoreProperties) {
org.springframework.beans.BeanUtils.copyProperties(source, target, ignoreProperties); org.springframework.beans.BeanUtils.copyProperties(source, target, ignoreProperties);
} }
/** /**
* The copy will ignore <em>BaseEntity</em> field * The copy will ignore <em>BaseEntity</em> field
* *
...@@ -224,7 +223,9 @@ public class BeanUtils { ...@@ -224,7 +223,9 @@ public class BeanUtils {
* @param target * @param target
*/ */
public static void copyEntityProperties(Object source, Object target) { public static void copyEntityProperties(Object source, Object target) {
org.springframework.beans.BeanUtils.copyProperties(source, target, "id", "dataChangeCreatedBy", org.springframework.beans.BeanUtils.copyProperties(source, target, COPY_IGNORED_PROPERTIES);
"dataChangeCreatedTime", "dataChangeLastModifiedBy", "dataChangeLastModifiedTime");
} }
private static final String[] COPY_IGNORED_PROPERTIES = {"id", "dataChangeCreatedBy",
"dataChangeCreatedTime", "dataChangeLastModifiedBy", "dataChangeLastModifiedTime"};
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册