Usage example:
public void testMethod(String para1, String para2, String para3){ if(para1==null){ System.out.println("para1 cannot be null"); } if(para2)... }
As the null verification code above, we will repeat our request to write the same code to verify each parameter. But we cannot really expose a general method, say, checknull (String para), because we need to print the parameter name so that users know which one is wrong.
Perhaps in java there is no way to do this in java. Method parameter names should disappear after compilation, if I understand them correctly.
So how do you guys usually solve this problem?
Paste the message. There is no other way to do this. And no, you cannot get the variable name.
Java assert, . :
assert
public void testMethod(String para1, String para2, String para3) { assert para1 != null : "para1 is null"; assert para2 != null : "para2 is null"; assert para3 != null : "para3 is null"; }
-ea. : .
-ea
, <23 > , Error ( , Java), catch (Exception e) . , , , .
Error
catch (Exception e)
, ( null, , ), RuntimeException .
null
RuntimeException
, , :
package sandbox; import java.lang.reflect.Field; import java.lang.reflect.Modifier; public class ReflectionClassChecker { public static boolean checkAllPublic(Object someObject){ System.out.println("Checking someObject " + someObject.toString()); boolean hasNulls = false; Class<?> c = someObject.getClass(); Field[] fields = c.getFields(); for(Field field: fields){ System.out.println("Checking field " + field.getName() + "."); if(isFieldPublic(field)){ System.out.println("Field " + field.getName() + " is public, checking it for null."); Object value = getField(field, someObject) ; if(value == null){ System.out.println("Field " + field.getName() + " is null."); hasNulls = true; } else { System.out.println("Field " + field + " has value " + value ); } } } return hasNulls; } private static boolean isFieldPublic(Field field){ int modifiers = field.getModifiers(); boolean isPublic = Modifier.isPublic(modifiers); return isPublic; } private static Object getField(Field field, Object someObject){ Object value = null; try{ value = field.get(someObject); } catch (IllegalAccessException ignore){ System.out.println(ignore); } return value; } }
, getter/setter, . , -, , c.getField(String), , field.get(object) .
, Java 7 , , . , Java 6 : (
varargs Java 1.5. "Object... objects" .
public static void printSpaced (Object... objects) { for (Object o: objects) { System.out.print(o + ":" ); if (o == "x" ) { System.out.print( "x found" ); } } }
, , , , , NullPointerException ( Java, IllegalArgumentException).
... if (para1 == null) { throw new NullPointerException("para1 cannot be null"); } ...
, - :
assertNotNull(Object param, String paramName) {...}
, , , , , Java, , . , - .
, , , . :
checkNull("parm1", parm1); checkNull("parm2", parm2); checkNull("parm3", parm3);
, , , parm1, parm2 parm3, - , customerName, balanceDue accountType.
, , , , . _ , , , . balanceDue , , . .. , , , .
, , . , , , .
, Paranamer ( ). .
, FindBugs JSR305 ( Java7, FindBugs FindBugs Maven) Java5 , . , ( ), ( , ), (, , AspectJ, ).
Use the parameter annotation and add a dynamic proxy (or Java 4 CGLIB style) to perform validation. See link text for how to use CGLIB
Why don't you transfer them as a card? So you can have a key representing the variable name and a value representing the actual String variable.
Source: https://habr.com/ru/post/1711581/More articles:How to transfer an arbitrary object on the Fitnesse page? - javaScala / Raise a question rss feed fetch - javaDo you use strict conditions in your projects? - dynamiccollecting mysql statistics - sqlWhat classes / interfaces should each .NET developer know? - .netGDI + Exception when using DrawString () - c #my vs2008 addin for text formatting is awfully slow - code-formattingCan I use a column number instead of a column name in which the query selection condition in SQL Server 2005? - sqlhow to restart one instance winforms application - c #Visual Studio testing tools compared to third-party tools - unit-testingAll Articles