Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8449543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:44:15+00:00 2026-06-10T10:44:15+00:00

I came up with a solution today involving creating classes at runtime, after parsing

  • 0

I came up with a solution today involving creating classes at runtime, after parsing a file, using the Reflection API in Java.

    while ((line = textReader.readLine()) != null)
    {
        Pattern p = Pattern
            .compile("([^:]+):([^:]+)::([\\d]+)::([^:]+)::(.+)");
        Matcher m = p.matcher(line);
        if (m.find())
        {
            String id = m.group(1);
            String className = m.group(2);
            int orderOfExecution = Integer.valueOf(m.group(3));
            String methodNameOrNew = m.group(4);
            Object[] arguments = m.group(5).split("::");
            if (methodNameOrNew.compareTo("new") == 0)
            {
                System.out.println("Loading class: " + className);
                if (className.contains("Competition"))
                {
                    continue;
                }
                else if (className.contains("$"))
                {
                    continue;
                }
                else
                {
                    Class<?> cl = Class.forName(className);
                    printMembers(cl.getConstructors(), "Constructor");
                    Constructor<?>[] cons = cl.getConstructors();
                    Object obj = cons[0].newInstance(arguments);
                    this.map.put(id, obj);
                }
            }
        }
    }

and printMembers():

private static void printMembers(Member[] mbrs, String s)
{
    out.format("%s:%n", s);
    for (Member mbr : mbrs)
    {
        if (mbr instanceof Field)
            out.format("  %s%n", ((Field) mbr).toGenericString());
        else if (mbr instanceof Constructor)
            out.format("  %s%n", ((Constructor) mbr).toGenericString());
        else if (mbr instanceof Method)
            out.format("  %s%n", ((Method) mbr).toGenericString());
    }
    if (mbrs.length == 0)
        out.format("  -- No %s --%n", s);
    out.format("%n");
}

However, I get the following error:

Loading class: org.powertac.common.TariffSpecification
Constructor:
  public org.powertac.common.TariffSpecification(org.powertac.common.Broker,org.powertac.common.enumerations.PowerType)

java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at Parser.parse(Parser.java:64)
    at Parser.main(Parser.java:137)

arguments[] is : 1 : CONSUMPTION. How could I create the right constructor, and give it the right arguments (types) ?
For example, in the sample parser I’m using I have:

2233:org.powertac.common.Tariff::6::new::6

then I have to create a class of the type org.powertac.common.Tariff (new tells me a new object needs to be created, and it takes a double rate as argument, in this case 6. However, I don’t know it takes a double, only the argument is String (6). How could I create / convert / cast to the correct type and then assign it to the constructor? My first thought was to create a symbol table, but I’m wondering about an easier solution…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T10:44:17+00:00Added an answer on June 10, 2026 at 10:44 am

    You need to use Class.getConstructor(Class...) to choose the constructor that is appropriate for arguments you wish to pass in to Constructor.newInstance(Object...)

    In your example I’m going to assume an array of 1 : CONSUMPTION means you have an array equivalent to

    Object[] arguments = new Object[]{Integer.valueOf(1), "CONSUMPTION"};
    

    So you call the following

    Class clazz = ... //Whatever class reference you have
    Constructor c = clazz.getConstructor(Integer.class, String.class);
    Object obj = c.newInstance(arguments);
    

    If you don’t know the types of your arguments you will have to test the argument set against the Class array returned by Constructor.getParameterTypes() for each constructor returned by Class.getConstructors() until you find a constructor that matches your argument array. More specifically, the array of arguments and array of classes are the same length and each class in the class array passes Class.isAssignableFrom(Class) for the class of the value in the same position in the argument array.

    Implementation of above in code

        public boolean canConstruct(Object[] args, Constructor<?> c){
            Class<?>[] paramTypes = c.getParameterTypes(); 
            if(args.length != paramTypes.length){
                return false;
            }
    
            int i = 0;
            for(Object arg: args){
                if(!paramTypes[i].isAssignableFrom(arg.getClass())){
                    return false;
                }
                        i++;
            }
    
            return true;
        }
    

    In order to use this you will have to have your argument array as you want to pass it to the constructor. You could try to edit your input so that it includes type information (this is similar to how java serialization works) so that you can construct the arguments for the constructor argument array via reflection with their own type constructors

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into an interesting SQL problem today and while I came up with
I came across an issue with one of our utility classes today. It is
I started reading Programming Pearls today and while doing it's exercise I came across
Today while struggling to make my C++ code (using Ooura FFT library) to give
I came across solution given at http://discuss.joelonsoftware.com/default.asp?interview.11.780597.8 using Morris InOrder traversal using which we
I have came with solution to remove duplicates from generic list<T> in .NET 2.0
I came with below solution but I believe that must be nicer one out
I came across this thread when I was looking for a solution, but it
So basically I came up with a semi home grown solution to lazy loading
Came across something like this today, and was wondering if there was an equivalent

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.