java分享平台代码_java 代码分享

以下代码可以在有“代码混淆器,混淆后正常工作”,希望分享给有可以使用到的人,(*^__^*) 嘻嘻

/*

* HashMapToPo 转换为集合对象

*/

public static ArrayList HashMapToPoList(

final ArrayList> hmList,

final Class clazz, boolean encode, boolean isEncode)

throws ForerDealArgumentException {

if (hmList == null || clazz == null) {

String msg = className

+ ".HashMapToPoList(final ArrayList> hmList,final Class> clazz,boolean encode) Args is not null";

logger.error(msg);

throw new ForerDealArgumentException(msg);

}

final HashMap dic = GetObjectPropertyName(clazz);

final HashMap dic2 = GetObjectMethodName(clazz);

final ArrayList lt = new ArrayList();

for (HashMap hm : hmList) {

T o = null;

try {

o = clazz.newInstance();

} catch (final Exception e) {

e.printStackTrace();

}

for (Map.Entry entry : hm.entrySet()) {

String key = entry.getKey().toUpperCase().replace("_", "");

Object value = entry.getValue();

if (dic.containsKey(key) && value != null) {

try {

String str = dic.get(key);

String temp = str.substring(0, 1);

str = str.replaceFirst(temp, temp.toUpperCase());

str = "set" + str;

Class> paraType = dic2.get(str).getParameterTypes()[0];

if (paraType != value.getClass())// 类型一致

value = StringToObject(paraType, value.toString());

if (isEncode) {

if (encode) {// iso8859-1 编码

if (paraType == String.class) {

value = CharsetConvert.charsetConvert(value

.toString());

}

} else {// GBK

if (paraType == String.class) {

value = CharsetConvert

.ISO_8859_1ToGBK(value.toString());

}

}

}

try {

dic2.get(str).invoke(o, value);

} catch (Exception e) {

logger.error("paraType:" + paraType + ";valueType:"

+ value.getClass() + "[" + str + ":"

+ value + "]");

}

} catch (Exception e) {

e.printStackTrace();

}

} else if (!dic.containsKey(key)) {

String msg = clazz.getName()

+ "Object does not contain the '" + key + "' field";

logger.debug(msg);

}

}

lt.add(o);

}

return lt;

}

/*

* 字段类型转换

*/

private static Object StringToObject(Class> clazz, String str) {

Object o = str;

if (clazz == Date.class && str != null && str != "") {

DateFormat dt1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

try {

o = dt1.parse(str);

} catch (ParseException e1) {

DateFormat dt2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

try {

o = dt2.parse(str);

} catch (ParseException e2) {

DateFormat dt3 = new SimpleDateFormat("yyyyMMdd HH:mm:ss");

try {

o = dt3.parse(str);

} catch (ParseException e3) {

DateFormat dt4 = new SimpleDateFormat("yyyyMMdd");

try {

o = dt4.parse(str);

} catch (ParseException e4) {

e4.printStackTrace();

}

}

}

}

} else if (clazz == BigDecimal.class) {

o = new BigDecimal(str);

} else if (clazz == Long.class) {

o = new Long(str);

} else if (clazz == Integer.class) {

o = new Integer(str);

} else if (clazz == int.class) {

o = Integer.parseInt(str);

} else if (clazz == float.class) {

o = Float.parseFloat(str);

} else if (clazz == boolean.class) {

o = Boolean.parseBoolean(str);

} else if (clazz == byte.class) {

o = Byte.parseByte(str);

}

return o;

}

/*

* HashMap转换为单一对象

*/

public static T HashMapToSinglePo(final HashMap hm,

final Class clazz, boolean encode, boolean isEncode)

throws Exception {

if (hm == null || clazz == null) {

String msg = className

+ ".HashMapToSinglePo(final ArrayList,boolean encode> hmList,final Class clazz) Args is  null";

logger.error(msg);

}

T o = null;

ArrayList> hmList = new ArrayList>();

hmList.add(hm);

ArrayList lt = HashMapToPoList(hmList, clazz, encode, isEncode);

if (lt != null && lt.size() > 0) {

o = lt.get(0);

}

return o;

}

/*

* 对象比较 ,结果HashMap key 为checkColums 字段,value 相同 Same

* ,不同Different,不存在Object does not contain the

*/

public static HashMap Compare(Class clazz, T o1,

T o2, String[] checkColums) {

HashMap ht = new HashMap();

if (clazz == null || o1 == null || o2 == null) {

String msg = className

+ ".Compare(Class clazz,T o1, T o2,List checkColums) Args clazz,o1,o2 is null";

logger.error(msg);

}

final HashMap dic1 = GetObjectPropertyName(clazz);

final HashMap dic2 = GetObjectMethodName(clazz);

if (checkColums == null) {

int count = dic1.size();

checkColums = new String[count];

dic1.values().toArray(checkColums);

}

for (String str : checkColums) {

try {

String temp = str.toUpperCase();

if (dic1.containsKey(temp)) {

String str1 = dic1.get(temp);

String temp1 = str1.substring(0, 1);

str1 = str1.replaceFirst(temp1, temp1.toUpperCase());

str1 = "get" + str1;

Object result1 = dic2.get(str1).invoke(o1);

Object result2 = dic2.get(str1).invoke(o2);

if (result1 != null || result2 != null) {

if (result1 != null && result2 != null) {

if (!result1.equals(result2)) {

ht.put(str, "Different");

} else {

ht.put(str, "Same");

}

} else {

ht.put(str, "Different");

}

}

} else {

String msg = clazz.getName()

+ " Object does not contain the '" + str

+ "' field";

ht.put(str, msg);

logger.debug(msg);

}

} catch (Exception e) {

e.printStackTrace();

logger.equals(e);

}

}

return ht;

}

/*

* 对象克隆

*/

public static T Clone(Class clazz, T o) {

if (o == null || clazz == null) {

String msg = className

+ ".Clone(Class clazz,Object o) Args clazz or o is null";

logger.error(msg);

}

T cloneObject = null;

try {

cloneObject = clazz.newInstance();

} catch (final Exception e) {

e.printStackTrace();

}

final HashMap dic = GetObjectPropertyName(clazz);

final HashMap dic2 = GetObjectMethodName(clazz);

for (Map.Entry entry : dic.entrySet()) {

try {

String str = entry.getValue();

String temp = str.substring(0, 1);

str = str.replaceFirst(temp, temp.toUpperCase());

String str1 = "get" + str;

String str2 = "set" + str;

if (dic2.containsKey(str1) && dic2.containsKey(str2)) {

Object result = dic2.get(str1).invoke(o);

dic2.get(str2).invoke(cloneObject, result);

} else {

String msg = str1 + ";" + str2 + "Error";

System.out.println(msg);

logger.error(msg);

}

} catch (Exception e) {

e.printStackTrace();

logger.error(e);

}

}

return cloneObject;

}

/*

* 反射获取对象字段

*/

private static HashMap> propertys = new HashMap>();

private static HashMap> methods = new HashMap>();

private static HashMap GetObjectPropertyName(Class> clazz) {

HashMap method = GetObjectMethodName(clazz);

String className = clazz.getName();

HashMap ht = new HashMap();

if (propertys.containsKey(className)) {

ht = propertys.get(className);

} else {

ht = new HashMap();

getPropertys(clazz, ht, method);// 递归父类

propertys.put(className, ht);

}

return ht;

}

private static HashMap getPropertys(Class> clazz,

HashMap ht, HashMap method) {

Method[] md = clazz.getMethods();

for (int i = 0; i < md.length; i++) {

String str = md[i].getName().replace("get", "").replace("set", "");

if (method.containsKey("get" + str)

&& method.containsKey("set" + str)) {

if (!ht.containsKey(str)) {

ht.put(str.toUpperCase(), str);

}

}

}

Class> superClazz = clazz.getSuperclass();

if (superClazz != null && superClazz != Object.class) {

getPropertys(superClazz, ht, method);

}

return ht;

}

/*

* 反射获取类的方法名

*/

private static HashMap GetObjectMethodName(Class> clazz) {

HashMap ht = null;

String className = clazz.getName();

if (methods.containsKey(className)) {

ht = methods.get(className);

} else {

ht = new HashMap();

getMethods(clazz, ht);// 递归父类

methods.put(className, ht);

}

return ht;

}

private static HashMap getMethods(Class> clazz,

HashMap ht) {

Method[] md = clazz.getMethods();

for (int i = 0; i < md.length; i++) {

ht.put(md[i].getName(), md[i]);

}

Class> superClazz = clazz.getSuperclass();

if (superClazz != null && superClazz != Object.class) {

getMethods(superClazz, ht);

}

return ht;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了巩固知识,做的一个网站。基于struts2+spring+springjdbc开发的代码分享网,所有源码已开源。 网站功能介绍: 1、邮件注册(采用阿里云企业邮箱),为了让大家体验一下邮箱注册功能。我已经在分享的源码中,为大家配置好了测试账户,大家可以在自己电脑上进行测试。 2、md5加密,注册用户,所有密码会以密文保存在数据库,可以保证安全。 3、代码分享功能(核心功能),该功能的主要特色是集成了优秀的文本编辑器,支持插入代码、插入链接、插入表情、插入图片、支持在线预览。同时也实现了文件上传(基于struts2的文件上传功能)。 4、代码下载,下载功能会判断用户是否下载过该代码,若下载过则不扣积分。下载功能也是基于struts2的下载模块实现的。 5、代码评论,该功能是我仿照qq空间评论功能实现的,在本站中,我是以时间倒叙方式显示的(也支持正序)。 6、代码收藏,用户可以收藏代码。 7、消息中心,分为了0系统消息、1评论消息、2兑换消息、3上传图片消息、4上传文件消息、5下载消息(用户扣除积分)、6下载消息。 8、代码中心,分为了分享代码、下载代码、评论代码、收藏代码。 9、设置功能,支持修改昵称、城市、性别、座右铭、密码、头像。 10、赞助兑换功能,支持1个赞助兑换10个积分,也支持用赞助升级称号。 11、其他功能包括:图片压缩处理功能(即使是几M的图片,压缩后也只有几十kb)。通用json处理功能(向方法中传递任何参数,int、string等,都会返回json数据,而且速度很快)。分词功能(点击某一个分享,进入详情页的时候,会对该分享名称进行分词,并且加入到head中,利于网站seo)。 可能还有一些其他功能,通过查看源码可了解。 网站技术介绍: 1、采用语言,java 2、后台框架,struts2+spring+spring JDBC 3、前台技术,layui+jquery+ajax 网站设计思路: 前台渲染是采用的jsp技术,为了保证网站的速度,我使用了几种方法: 1、我将重复的代码保存成单独的jsp文件然后引入(这样的好处就是重复的jsp文件只会加载一次,然后浏览器缓存,下次加载速度会提升)。比如,我将link和header单独提取出来,然后在其他页面进行引入: 2、所有的业务功能,我都放在了html加载完成之后,利用jquery+ajax获取数据后再渲染界面(这样的好处就是给用户的感觉是网站速度很快。因为用户打开后,立马渲染html代码,此时网站结构已经出现,接着用jqury+ajx去后台获取数据。由于我的sql语句严格控制在ms级别,所以只需要几百ms,数据即可拿到,此时渲染在页面上给用户的感觉很快) 3、sql语句的控制,本站的所有sql语句,均控制在1s以下。这块我花了很长时间进行sql优化,我举个例子:为了减少数据库的访问次数,我会想方设法通过一条语句获取所有信息,并且严格控制它的执行速度,绝对不可以超过1s。首页的下载榜、评论榜、收藏榜,这三个功能的数据就是通过一条sql语句获取的: #优化联合查询用户评论、下载、收藏的资源列表 select a.sort,a.id,r.name,a.nowtime,r.isjing,r.isyuan, ifnull(c.res_comments_num,0) as res_comments_num, ifnull(d.res_download_num,0) as res_download_num, ifnull(kp.res_keep_num,0) as res_keep_num from #sort为1代表用户评论的代码列表 (select 1 as sort,c.resources_id as id,c.nowtime as nowtime from comments c #需要指定用户 where c.user1_id = 1 group by c.resources_id union all #sort为2代表用户下载的代码列表 select 2 as sort,d.resources_id as id,d.nowtime as nowtime from download d #需要指定用户 where d.user_id = 1 group by d.resources_id union all #sort为3代表用户收藏的代码列表 select 3 as sort,k.resources_id as id,k.nowtime as nowtime from keep

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值