method-signature

How can I make a method take a null value?

痴心易碎 提交于 2019-12-12 06:04:33
问题 If you have two overloaded methods like so: public void methodName(File file){} public void methodName(String string){} If you try to call methodName with null you will get an error saying that it's ambiguous which is understandable because it doesn't know which method to do. I know that you can just cast the null: methodName((String) null) but how can I create a method specifically to handle the situations where you call methodName(null) ? Something like this: public void methodName(null

What is “override-equivalence” and how is it related to @Override?

一笑奈何 提交于 2019-12-10 00:52:21
问题 Reading the Javadoc for the @Override annotation, I came across the following rule: If a method is annotated with this annotation type compilers are required to generate an error message unless at least one of the following conditions hold: The method does override or implement a method declared in a supertype. The method has a signature that is override-equivalent to that of any public method declared in Object. I'm clear on the first point, but I'm unsure about the second one. What does it

How to get parameter names with Java reflection [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-12-07 08:45:59
问题 This question already has answers here : Getting the name of a method parameter (8 answers) Closed 3 years ago . How do I get method signatures with Java reflection? EDIT: I actually need the parameter NAMES not the types of a method. 回答1: To get the method i of a class C you call C.class.getMethods()[i].toString() . EDIT: Obtaining parameter names is not possible using the reflection API. But wen you compiled your class with debug information, it is possible to extract the information from

Why is varargs always the last parameter in a method signature?

和自甴很熟 提交于 2019-12-06 02:36:58
问题 Why does varargs have to be the last parameter in method signature? I want to know the reason. 回答1: Because it makes the compiler's life simpler. There's no real reason why it couldn't have more arguments after, but it would require a much more complex compiler and so the spec was written that way. 回答2: The main reason is because it would be potentially ambiguous otherwise.... For example, how could the compiler tell whether arguments are varargs or separate named arguments in a long list of

How to get parameter names with Java reflection [duplicate]

南楼画角 提交于 2019-12-05 18:15:20
This question already has answers here : Getting the name of a method parameter (8 answers) Closed 3 years ago . How do I get method signatures with Java reflection? EDIT: I actually need the parameter NAMES not the types of a method. mtsz To get the method i of a class C you call C.class.getMethods()[i].toString() . EDIT: Obtaining parameter names is not possible using the reflection API. But wen you compiled your class with debug information, it is possible to extract the information from bytecode. Spring does it using the ASM bytecode engineering library . See this answer for further

Determining if an Objective-C method is variadic during runtime

你离开我真会死。 提交于 2019-12-05 10:35:54
Is there a way to find out -- at runtime -- whether a given method is of variadic type? Something like method_getTypeEncoding() ; that won't tell me whether a method accepts variable number of arguments. Or is there maybe a trick to tell so? Robert's comment is correct. Consider: @interface Boogity @end @implementation Boogity - (void)methodWithOneIntArg:(int)a {;} - (void)variadicMethodWithIDSentinel:(id)a, ... {;} @end Running strings on the resulting binary produces (there was also the stock main() ): strings asdfasdfasdf Boogity methodWithOneIntArg: variadicMethodWithIDSentinel: v20@0:8i16

Why is varargs always the last parameter in a method signature?

守給你的承諾、 提交于 2019-12-04 08:47:12
Why does varargs have to be the last parameter in method signature? I want to know the reason. Because it makes the compiler's life simpler. There's no real reason why it couldn't have more arguments after, but it would require a much more complex compiler and so the spec was written that way. The main reason is because it would be potentially ambiguous otherwise.... For example, how could the compiler tell whether arguments are varargs or separate named arguments in a long list of arguments with multple varargs? Imagine a method signature like: printNames(String... girls, String... boys); If

What are the digits in an ObjC method type encoding string?

馋奶兔 提交于 2019-12-03 08:11:10
问题 I'm reading Apple's article about Objective-C runtime type encoding strings and some methods have numbers in their type strings. What do the numbers in v12@0:4@8 mean? 回答1: This looks like an encoding of a setter method like this: - (void) setSomething:(id) anObject To break it down: v means void return type 12 means the size of the argument frame (12 bytes) @0 means that there is an Objective-C object type at byte offset 0 of the argument frame (this is the implicit self object in each

Inheritance best practice : *args, **kwargs or explicitly specifying parameters

ぃ、小莉子 提交于 2019-12-03 01:37:40
问题 I often find myself overwriting methods of a parent class, and can never decide if I should explicitly list given parameters or just use a blanket *args, **kwargs construct. Is one version better than the other? Is there a best practice? What (dis-)advantages am I missing? class Parent(object): def save(self, commit=True): # ... class Explicit(Parent): def save(self, commit=True): super(Explicit, self).save(commit=commit) # more logic class Blanket(Parent): def save(self, *args, **kwargs):

Compute a Java function's signature

♀尐吖头ヾ 提交于 2019-12-02 23:36:50
Is there a way to compute a Java class's method's signature? A signature like ([Ljava/lang/String;)V represents a function that takes a String[] as argument and returns void . What's the rule to compute the signature? It's always a set of parentheses enclosing type signifiers for the arguments, one after the other with no commas or anything, followed by a type signifier for the return value after the closing paren. It's pretty straightforward. There's a table of type signatures on this page: Signature Java Type Z boolean B byte C char S short I int J long F float D double V void L fully