我想只获得一个POJO的字段名称,我的java POJO看起来像:
public class A1 {
private String field2;
private String field1;
public String getField2() {
return field2;
}
public void setField2(String field2) {
this.field2 = field2;
}
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
@Override
public String toString() {
return "A1 [field2=" + field2 + ", field1=" + field1 + "]";
}
public String getFiled1Name() throws NoSuchFieldException, SecurityException{
return this.getClass().getDeclaredField(field1).getName();
}
}
在客户端代码中:
System.out.println(new A1().getFiled1Name());
我正在NullPointerException
我尝试了方法this.getClass().getDeclaredFields()
,这可以按预期工作。但问题是:它返回所有字段(java.lang.reflect.Field
),我需要迭代,比较然后返回正确的字段名称。
相反,我想只获得一个字段名称,并且方法中没有硬编码字段名称。我怎么能得到这个?
答案 0 :(得分:4)
您应该提供字段的名称而不是字段本身,因为field1
是字符串,null
并且您将null
传递给getDeclaredField
,
public String getFiled1Name() throws NoSuchFieldException, SecurityException {
return getClass().getDeclaredField("field1").getName();
}
Class#getDeclareField
抛出NullPointerException
- 如果name为null
我不想硬编码“field1”
您可以为字段名称创建常量而不是使用反射,但是对于名称中的每个更改,您还需要更新常量。
在其他方面,您可以获取所有声明的字段并从数组中访问字段的名称。但是在使用它时要小心,因为声明中的更改可能会改变方法的行为。
public String getFiled1Name() throws NoSuchFieldException, SecurityException {
return getClass().getDeclaredFields()[0].getName();
}
答案 1 :(得分:0)
您需要将字段名称作为字符串传递。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<link rel="stylesheet" href="/css/theme.css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<meta name="CocoaVersion" content="1404.46">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 14.0px; font: 12.0px Times; color: #000000; -webkit-text-stroke: #000000}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 14.0px; font: 12.0px Times; color: #000000; -webkit-text-stroke: #000000; min-height: 14.0px}
p.p5 {margin: 0.0px 0.0px 12.0px 0.0px; line-height: 14.0px; font: 12.0px Times; color: #000000; -webkit-text-stroke: #000000}
p.p7 {margin: 0.0px 0.0px 12.0px 0.0px; line-height: 14.0px; font: 12.0px Times; color: #000000; -webkit-text-stroke: #000000; min-height: 14.0px}
p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 13.0px; font: 11.0px '.AppleSystemUIFont'; color: #000000; -webkit-text-stroke: #000000}
li.li2 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 14.0px; font: 12.0px Times; color: #0000ee; -webkit-text-stroke: #0000ee}
span.s1 {font-kerning: none}
span.s2 {-webkit-text-stroke: 0px #000000}
span.s3 {text-decoration: underline ; font-kerning: none}
span.s4 {font-kerning: none; color: #0000ee; -webkit-text-stroke: 0px #0000ee}
span.s5 {font-kerning: none; background-color: #c0c0c0}
span.s6 {font: 12.0px Times; font-kerning: none}
ul.ul1 {list-style-type: disc}
</style>
</head>
<body>
<p class="p1"><span class="s1"></span></p>
<ul class="ul1">
<li class="li2"><span class="s2"></span><span class="s3">What we do</span></li>
<li class="li2"><span class="s2"></span><span class="s3">Our process<span class="Apple-converted-space"> </span></span></li>
<li class="li2"><span class="s2"></span><span class="s3">Our team<span class="Apple-converted-space"> </span></span></li>
<li class="li2"><span class="s2"></span><span class="s3">Contact us</span></li>
</ul>
<p class="p3"><span class="s1"></span><br></p>
<h1 style="margin: 0.0px 0.0px 16.1px 0.0px; line-height: 28.0px; font: 24.0px Times; color: #000000; -webkit-text-stroke: #000000"><span class="s1"><b>Change The Way You Pay<span class="Apple-converted-space"> </span></b></span></h1>
<p class="p5"><span class="s1">With Tip Me, you will be able to make transactions like never before</span></p>
<p class="p1"><span class="s1"></span></p>
<h2 style="margin: 0.0px 0.0px 14.9px 0.0px; line-height: 22.0px; font: 18.0px Times; color: #000000; -webkit-text-stroke: #000000"><span class="s1"><b>Tip Anyone. Anytime. Anywhere.<span class="Apple-converted-space"> </span></b></span></h2>
<p class="p5"><span class="s1">No need to pull out your wallet anymore. With Tip Me, you will be able to make simple, efficient transactions with the click of a button.<span class="Apple-converted-space"> </span></span></p>
<p class="p1"><span class="s1"></span></p>
<h2 style="margin: 0.0px 0.0px 14.9px 0.0px; line-height: 22.0px; font: 18.0px Times; color: #000000; -webkit-text-stroke: #000000"><span class="s1"><b>Efficiently Manage Your Money<span class="Apple-converted-space"> </span></b></span></h2>
<p class="p5"><span class="s1">Never lose track of your transactions. You will be able to view who has tipped you, whom you have tipped, and how much was exchanged.</span></p>
<p class="p1"><span class="s1"></span></p>
<h2 style="margin: 0.0px 0.0px 14.9px 0.0px; line-height: 22.0px; font: 18.0px Times; color: #000000; -webkit-text-stroke: #000000"><span class="s1"><b>Make Secure Transactions<span class="Apple-converted-space"> </span></b></span></h2>
<p class="p5"><span class="s1">Never worry about securing confidential information again. Through Tip Me, all user payments are Level 1 PCI compliance encrypted.</span></p>
<p class="p7"><span class="s1"></span><br></p>
<h1 style="margin: 0.0px 0.0px 16.1px 0.0px; line-height: 28.0px; font: 24.0px Times; color: #000000; -webkit-text-stroke: #000000"><span class="s1"><b>Our Process<span class="Apple-converted-space"> </span></b></span></h1>
<p class="p5"><span class="s1">Select a friend, enter an amount, and tip. Yes, it's that simple.<span class="Apple-converted-space"> </span></span></p>
<p class="p1"><span class="s1"><span class="Apple-converted-space"> </span></span></p>
<p class="p1"><span class="s1"></span></p>
<p class="p1"><span class="s1"><span class="Apple-converted-space"> </span></span></p>
<p class="p1"><span class="s1"><span class="Apple-converted-space"> </span></span></p>
<p class="p1"><span class="s1"><span class="Apple-converted-space"> </span></span></p>
<h1 style="margin: 0.0px 0.0px 16.1px 0.0px; line-height: 28.0px; font: 24.0px Times; color: #000000; -webkit-text-stroke: #000000"><span class="s1"><b>Our Team<span class="Apple-converted-space"> </span></b></span></h1>
<p class="p1"><span class="s1"></span></p>
<h2 style="margin: 0.0px 0.0px 14.9px 0.0px; line-height: 22.0px; font: 18.0px Times; color: #000000; -webkit-text-stroke: #000000"><span class="s1"><b>Nilay Neeranjun<span class="Apple-converted-space"> </span></b></span></h2>
<p class="p5"><span class="s1">Co-Founder, Software Developer</span></p>
<p class="p1"><span class="s4"><a href="file:///Users/nneeranjun/Desktop/Tip%20Me%20Website/www.linkedin.com/in/nilay-neeranjun"></a></span><span class="s1"><span class="Apple-converted-space"> </span></span></p>
<p class="p1"><span class="s1"></span></p>
<h2 style="margin: 0.0px 0.0px 14.9px 0.0px; line-height: 22.0px; font: 18.0px Times; color: #000000; -webkit-text-stroke: #000000"><span class="s1"><b>Badri Narayan</b></span></h2>
<p class="p5"><span class="s1">Co-Founder, Software Developer</span></p>
<p class="p1"><span class="s4"><a href="https://www.linkedin.com/in/badrinnarayan"></a></span><span class="s1"><span class="Apple-converted-space"> </span></span></p>
<h1 style="margin: 0.0px 0.0px 14.9px 0.0px; line-height: 22.0px; font: 18.0px Times; color: #000000; -webkit-text-stroke: #000000"><span class="s1"><b>Contact Us</b></span></h1>
<h4 style="margin: 0.0px 0.0px 16.0px 0.0px; line-height: 14.0px; font: 12.0px Times; color: #000000; -webkit-text-stroke: #000000"><span class="s1"><b>Send us a message and we will get back to you as soon as possible</b></span></h4>
<p class="p7"><span class="s1"></span><br></p>
<p class="p7"><span class="s1"></span><br></p>
<p class="p7"><span class="s1"></span><br></p>
<p class="p7"><span class="s1"></span><br></p>
<p class="p3"><span class="s1"></span><br></p>
<p class="p3"><span class="s1"></span><br></p>
<p class="p9"><span class="s5">Submit</span><span class="s6"><span class="Apple-converted-space"> </span></span></p>
<p class="p1"><span class="s1">Copyright © TipMe 2015<span class="Apple-converted-space"> </span></span></p>
</body>
</html>
答案 2 :(得分:0)
您的字段 field1 为空。您尚未初始化String。这会导致NullPointerException。您目前只是将 null 传递给 getDeclaredField()方法。
答案 3 :(得分:0)
我想到了这一点,不确定Thread.currentThread().getStackTrace()
是否可以接受,但是你想要一些完全动态的东西,没有硬编码......所以这是我的拍摄:
public class Reflections {
private String field2;
private String field1;
public static void main(String[] args) {
Reflections r = new Reflections();
System.out.println(r.getField1Name());
}
private Field fields[] = null;
public Reflections() {
fields = getClass().getDeclaredFields();
}
public String getField1Name() {
if(fields == null || fields.length==0){
return null;
}
final StackTraceElement[] stes = Thread.currentThread().getStackTrace();
if(stes == null || stes.length<2){
return null;
}
final String thisMethodName = stes[1].getMethodName(); // [1] this is supposed to have the name of current method
for (Field f : fields) {
if (thisMethodName.toLowerCase().contains(f.getName().toLowerCase())) {
//System.out.println(f.getName());
return f.getName();
}// match
}// for fields
return null;
}// getFiled1Name
}
简而言之,getField1Name()
试图找到它自己的名字,
然后遍历当前类的字段列表,并尝试查找其名称是方法名称的一部分的字段。
这有时可能会返回无效结果,因为某些字段名称可能是较长名称的子部分,以及其他一些结果不准确的情况
但是,在这里它可能对你或其他任何人都有用。
你可以重构代码,创建新方法getFieldNameUsingMethodName()
??也许 ?!?!这将做循环和匹配的事情,如果你有10个或更多getFieldXname()
所以你可以这样做,如果你认为这个溶胶值得。