Reflective invoke of variable-argument method?

I'm having trouble reflectively invoking a variable-argument method. Can anyone provide a code snippet that does it? Here's my test, with the output below:
package comet.pod;
import java.lang.reflect.Method;
public class Test
   public void doSomething (Object... args)
      System.out.println ("hello world");
   public void test()
      Method method = null;
      try
         Class<?> c = Test.class;
         Class[] argTypes = new Class[] { Object[].class };
         method = c.getMethod ("doSomething", argTypes);
         System.out.println ("is VarArgs? " + method.isVarArgs());
      catch (Exception x) { System.err.println (x); }
      try
         method.invoke (this, (Object[]) null);
      catch (Exception x) { System.err.println (x); }
      try
         method.invoke (this, new Object());
      catch (Exception x) { System.err.println (x); }
      try
         method.invoke (this, new Object[] { "arg" });
      catch (Exception x) { System.err.println (x); }
   public static void main (final String[] args)
      new Test().test();
}is VarArgs? true
java.lang.IllegalArgumentException: wrong number of arguments
java.lang.IllegalArgumentException: argument type mismatch
java.lang.IllegalArgumentException: argument type mismatch

You need something like:
method.invoke(this, new Object[] { new Object[] { "arg1", "arg2" } });To see why, consider what you would do if your method was:
public void doSomething (Object a1, Object a2, Object... aVar)Alex

Similar Messages

  • To get the reflection of a variable arity method.

    Hi,
    I am having
    public static Object myMethod(Object... args)I need to return the rfletion of this method. I have tried out the following, but it throws an error of mismatch in the number of arguments when I pass more than one argument.
    return cls.getMethod("myMethod", Object[ ].class);Please help in this regard.
    Thanks in advance.

    public class Test  {
        public static void main( String argv[] ) throws Throwable {
         Class cls = Test.class;
         System.out.println( Object[ ].class );
         java.lang.reflect.Method m = cls.getMethod("myMethod", Object[ ].class);
         Test t = new Test();
         Object[] paramsA = new Object[0];
         System.out.println( m.invoke( t, new Object[] { paramsA } )  );
        public  Object myMethod( Object... a ) {
         return "Test";
    }seams to be working for me. Could your post your error message? Thanks

  • Variable arguments and overloading methods

    If I have overloaded methods, say:
    double foo(int... args) and double foo(double... args)
    I can do:
    int[] val = {1,2,3,4,5};
    double dd = foo(val);
    I don't get a compiler error, but if I do:
    int a=1, b=2, c=3, d=4, e=5;
    double dd = foo(a,b,c,d,e);
    java complains that foo(int..) and foo(double...) are ambiguous.
    Why?

    jverd wrote:
    jverd wrote:
    >
    [JLS 15.12.2.4 Phase 3: Identify Applicable Variable Arity Methods|http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12.2.4]
    and
    [JLS 15.12.2.5 Choosing the Most Specific Method|http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12.2.5]
    I don't feel like unravelling those in detail right now, but I think it's that an int[] cannot be promoted to a double[], so there's no ambiguity there, but an int can be promoted to a double, so that's ambiguous.Just because two versions apply doesn't make it ambiguous. It's ambiguous when there isn't a "most specific method" demo:
    public class Example {
        static void f(String x, Object y) {} //version 1
        static void f(Object x, String y) {} //version 2
        static void f(Object x, Object y) {} //version 3
        public static void main(String[] args) {
            String s = "string";
            Object o = "object";
            f(s, o);
            f(0, s);
            f(s, s); //ambiguous!
    }I define three versions of f. In the first invocation, versions 1 and 3 apply but 1 is the most specific.
    In the second invocation versions 2 and 3 apply but 2 is the most specific.
    In the third invocation versions 1 and 2 apply but neither is the more specific than the other, so the call is ambiguous.

  • Transportaion probled for Variable Substitutiom Method

    hi all,.
    we have done variable substitution method in DEV, but it not reflecting in QA.
    also tell me wht r the things we need to change manually after importing the object into INT and QA?
    regards
    sreeni

    Hi Chirag,
    Tank you very much.
    we are using CMS for this transportations in XI.
    I have created one CC as RCVR and we have used Message all as FCC.
    Now exported in DEV then it ready to import in CMS. If we import this in CONSODITION TAB of CMS, then it is imported into my QA.
    Now, I open the that Scenario in ID of QA.
    Here My doubt is:
    In CC channel we have give some values manually for  EXAMPLE, Target DIR.
    What abt file name scheme?
    If we use FCC and to get File Name as dynamic form PayLoad Values.
    For that we can use Variable Substitution Method using percenatge simbles.
    And we have to check the check box for Variable Substitution Method Enable.
    After checking the check box, we have to give variable names and values from PayLoad data.
    Ok,
    now my doubt is,
    Whethere we have to change the following manually or not.
    1) Taget Directory
    2) File name scheme
    3) FTP Server Name
    4) User ID (FTP)
    5) Pasword
    6)Check Box (for Variable Substitution Method Enable)
    7)For this Variable Substitution Method, variable names which we used for  FIle Name Scheme and its values   right.
    plz
    revert
    regards
    sreeni

  • IBM JDK 6 32bit & 64bit Reflection Invoke issue on Weblogic 10.3

    We just found a issue on Weblogic 10.3 with IBM JDK 6 32bit, which calls different method during reflection invoke with that been processed under 64bit JDK, here is my source code:
    public class ButtonTag extends InputTag {
    private boolean checkNotNull = false;
    public void setCheckNotNull(Object checkNotNull) {
    public void setCheckNotNull(boolean checkNotNull) {
    this.checkNotNull = checkNotNull;
    this.setUserSetCheckNotNull(true);
    Under 64bit, reflection invoke will call setCheckNotNull(boolean checkNotNull), however under 32bit, it will call setCheckNotNull(Object checkNotNull), actually 32bit version is doing something wrong, we are not sure it is a issue of weblogic or IBM JDK, pls refer the information below for details, Thanks
    OS: AIX 5.3
    AIX@ /usr/java6/bin./java -version
    java version "1.6.0"
    Java(TM) SE Runtime Environment (build pap3260sr2-20080818_01(SR2))
    IBM J9 VM (build 2.4, J2RE 1.6.0 IBM J9 2.4 AIX ppc-32 jvmap3260-20080816_22093 (JIT enabled, AOT enabled)
    J9VM - 20080816_022093_bHdSMr
    JIT - r9_20080721_1330ifx2
    GC - 20080724_AA)
    JCL - 20080808_02
    AIX@ /usr/java6_64/bin./java -version
    java version "1.6.0"
    Java(TM) SE Runtime Environment (build pap6460sr6-20090925_01(SR6))
    IBM J9 VM (build 2.4, JRE 1.6.0 IBM J9 2.4 AIX ppc64-64 jvmap6460sr6-20090923_42924 (JIT enabled, AOT enabled)
    J9VM - 20090923_042924
    JIT - r9_20090902_1330ifx1
    GC - 20090817_AA)
    JCL - 20090924_01
    AIX server107 3 5 00CE9E7B4C00

    The problem was the name of the Seam EJB module. Changing the jar to jboss-seam.jar, everything worked. This issue does not appear on JBoss 4.2.3.GA, where I also tested.
    Edited by: deadlock_gr on Jun 10, 2010 9:55 AM
    Edited by: deadlock_gr on Jun 10, 2010 9:56 AM

  • FileName using Variable Substitution Method

    Hi SAP experts,
    I need some advise regarding this.
    I have here sample structure of message,
    DT_Root  --> root node
      Tree  --> occurence is 1 to unbounded
        SubTree1
        SubTree2
        SubTree3
    What will be my File Content Conversion parameters to be set in Variable Substitution Method in order for me to get the value of SubTree3 and make it as my filename?
    Also, the value of SubTree3 must not be present on my actual payload..
    var1:payload,1,SLI,1   ---?? what if it is unbounded?
    Kindly advise.
    Thank you very much.

    Hi Raj,
    I cannot open the link.
    Anyway, is it ok if you will advise me regarding my question a while ago?
    I have here sample message structure,
    CSV --> Message Type
    SLI  --> 1 .. unbounded (occurence)
      subSLI1
      subSLI2
      subSLI3
    FileNode --> 1..1 (occurence)
      FileName --> 1..1
    Here's my parameters in Variable Substituion method,
    FileName Scheme: %var1%.csv
    var1   --  payload:CSV,1,FileNode,1,FileName,1
    File Content Conversion Parameters:
    Recordset Structure: SLI,FileNode
    SLI.addHeaderLine: 3
    SLI.headerLine: PERIO;EAN;ILN,....
    SLI.fieldFixedLengths: 8;10;13;...
    SLI.fixedLengthTooShortHandling: Cut
    SLI.fieldSeparator: ;
    SLI.endSeparator: 'nl'
    FileNode.fieldFixedLengths: 0
    FileNode.fixedLengthTooShortHandling: Cut
    I must the output flatfile with a value of the filename in the FileName field.
    And the content of the flatfile must look like this,'
    PERIO;EAN;ILN;SALES;TURNO;PERFL;SUBSFL;SALUN;%CURRENCY%;STOCK;STOUN;ARTNR;ARKTX
    20071020;737052020198 ;4333342000008;         1;        9.95;W;X;ST ;EUR;0000000000;ST ;                                  
    20071020;737052056852 ;4333342000008;         1;       14.50;W;X;ST ;EUR;0000000000;ST ;                                  
    20071020;737052073552 ;4333342000008;         1;       12.95;W;X;ST ;EUR;0000000000;ST ;                                  
    *each field has a fixed field length.
    Kindly advise if my set parameters are correct.
    Thank you very much!

  • Variable Substitution Method

    Hi,
    When Im going through the variable substitution method with respect to the target directory:
    with the help of the blog
    An interesting usage of Variable Substitution in XI
    I dint understand what is the SID here. SID of different systems like DEV system, PRD system and etc. What exactly is an SID?
    If we take SID as a variable in the variable substitution method, then the value to VAR will be getting from the header i.e, Receiver_Service.
    From where does the Receiver_Service gets the SID? whenever the system is changed from DEV to PRD or to someother.
    Kindly let me know if i can get any such other blogs on this scenario.
    Thanks in Advance,
    Divya

    Hi ,
    As said above Message header contains the following informations
    sender_party
    sender_service
    receiver_party
    receiver_service
    interface_name
    interface_namespace
    message_id
    message_id_hex
    SID stands for System ID and is a three character unique name for a SAP system. It will be different for different systems like for Dev system it may be XXX and for quality system say YYY and similarly for production system say ZZZ. Message header values are accessed dynamically.
    Thanks!

  • Absorption&Variable(Direct)method Costing, are possible both method on SAP?

    Hello my dear Gurus:
    In our actual project; we are trying to include on SAP the Absorption and Variable (Direct) costing methods; this of course within the Controlling modules (Product costing (actual costing), CCA, IO's, etc.) In order to comply with the external (Absorption) and internal (Variable/Direct) costing requirements. So the Big question we already know that the absorption method is plain, BUT how can we include the variable (Direct) method costing with any development (i.e: Parallel Accounting costing (Alternative Valuation Run)). or configuring the Cost Components attributes that the (Fixed costs) are not included in the inventory valuation (TCODE OKTZ).
    Our first impression is that SAP it is only capable to execute the analysis under the Absorption method without the Variable(direct) costing.
    Please if you have both methods implemented, please let me know how you did it?
    Kinds regards!

    Hi,
    Yes. This Variable/direct/Marginal Costing is possible in SAP. All you have to do is to create a Profitability Report. First you need to deduct all variable costs from the Revenue to arrive at  the contribution/Gross Margin. And then, you need to deduct Fixed costs from the Contribution to get the Profit. You can also find breakeven point and breakeven volume for the business. The only challenge is to segregate the costs into variable and fixed as accurately as possible.
    Trust this helps to a large extent,
    Cheers,

  • Variable arguments in macros

    Don't know if this is the right place to post this question but none of the other categories seemed right.
    I am trying to use a macro with variable arguments in a pro-c module. The short version of my macro is:
    #define debug(...) printf(stderr, __VA_ARGS__)
    I consistently get the following error when I build:
    PCC-F-02209, Macro invocation has incorrect number of arguments
    Does anyone know of a way to get around this problem?
    Thanks

    Don't know if this is the right place to post this questionI'm not sure neither. What's your Oracle products you use ? Maybe we can help you to find a proper place to post your question.
    Nicolas.

  • A basic question about static variables and methods

    What would be the effects if I declare lots of static variables and static methods in my class?
    Would my program run faster?
    Would the class take more memory spaces? (If so, how do I estimate an appropriate number of variabls and methods to be declared static?)
    Thank you @_@

    when you declare a static var the var isn't created for every instance of the class, it just "live" in the class
    when you declare a static method is the same, so if you have:
    class MyClass
    static int myMethod()
    //Method work
    you dont need to have a instance of the class to call myMethod, you can do such things like this.
    int value = Myclass.myMethod();
    if myMethod isn't static you can't do this.. so, if
    class MyClass
    int myMethod()
    //Method work
    you CAN'T call
    int value = MyClass.myMethod();
    instead, you have to write
    MyClass m;
    m = new MyClass();
    value = m.myMethod();

  • I have deploy an EJB in weblogic 6.1,but how to use jsp to invoke the EJB's method?

    i have deploy an EJB in weblogic 6.1,but how to use jsp to invoke the EJB's method?
    thanks!

    You'd do something like:
    <%
    //vvv this part can potentially be done in initialization
    Context ctx = getInitialContext();
    BeanHome home =
    (BeanHome)PortableRemoteObject.narrow(ctx.lookup("the.jndi.name"),
    BeanHome.class);
    Bean b = home.create();
    //^^^
    Result r = b.invokeMethod();
    %>
    "toxin" <[email protected]> wrote in message
    news:3d2e95e5$[email protected]..
    >
    i have deploy an EJB in weblogic 6.1,but how to use jsp to invoke theEJB's method?
    thanks!

  • Sending variables to methods

    i want to send some variables a method in another file, but my coding isn't working
    TextToFile.main(variablea,variableb,variablec)any suggestions?

    sorry, should have defined that, will next time
    what i meant was that the code (posted above) is supposed to be sending the variables to the file named TextToFile, however my error reads
    unreported exception java.lang.Exception;must be caught or declared to be thrown
    is there any other way to send multiple string variables to a file?

  • Invoking servlet's doGet() method on startup

    Hi Friends,
    Is there any way of invoking the servlets doGet method on server start -up.
    I am using Tomcat 4.1.18.
    I know that we can Invoke the servlet at stat-up i.e. it will invoke init() method osf servlet.But what if I want to invoke the doGet method on start - up?
    Regards
    Abhay

    The doGet will require an Request and Response object. It would be easier to remove what ever logic in the doGet you need to execute to seperate methods that can be called from both the init and doGet methods.

  • How to get the number of invokes pendents on sincronized method

    hello,
    I have a sincronized method that depends on a slow database function. Well I would show the position of the line that the invoke of this method is.
    Is it possible to get status, or numer of invokes pendents on sincronized method?

    hello,
    I have a sincronized method that depends on a slow
    database function. Well I would show the position of
    the line that the invoke of this method is.
    Is it possible to get status, or numer of invokes
    pendents on sincronized method?No

  • Using java.lang.reflect.Method.invoke on a static method?

    This is probably a FAQ, but I am finding it impossible to construct a search which answers this question for me.
    How do I call java.lang.reflect.Method.invoke on a static (e.g. class) method? Is it even possible?
    Thanks,
    dwh

    Is this of any help?
    http://www.esus.com/javaindex/j2se/jdk1.2/javalang/reflection/reflection.html
    Cheers,
    Joris

Maybe you are looking for

  • Problem with infosource for 0customer

    Hello Experts I am workign in 3.5 Bw system. We have a 0customer master data object and values are being populated from ECC system. I am not able to look at infosource and whenever i try to expand 0customer in RSA 1 it gives me ABAP dump which i dont

  • Problem using Win 8.1 Mail app with corporate email account

    I have a client where most of the PCs are running Windows 8.1 with Office 2013 installed and an on-premise Exchange 2013 server. Even though they've been using Office 2013, now that they have Win 8.1 the staff now like to use the included Mail app fo

  • Set Combobox value based on Datagrid selectedItem

    I have been searching the forums and google for some time now and I'm not sure how do this. I populate a datagrid from a httpservice call to a php script. The datagrid is displaying perfectly. I also populate two comboboxes from the same httpservice

  • Role no longer valid

    I just tried to send a connection key to a user who received this message: Your role in this website is no longer valid. Please obtain a new connection key file from this websites administrator. I've not sent her one before, so am not sure why this h

  • Which authorization object we have to use for direct cube request ?

    Hello, In Analyser,  when we would like to request directly to Cubes, whe have 'No authorisation' message. Do you know wich authorisation objet we have to use for that ? Thanks in advance for you help. Best regards Nicolas Trinquand