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 8828991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:43:57+00:00 2026-06-14T07:43:57+00:00

I have a java class (named A) that needs to be executed by groovy.

  • 0

I have a java class (named A) that needs to be executed by groovy. This class is standalone. I want to dynamically extend it with the class B.

The code for class A:

public class A {
    public void run() {
        System.out.println(context);
    }
}

The code for class B:

public class B {
    protected String context;

    public void setContext(Context c) {
        this.context = c;
    }
}

The code controlling groovy:

String code = FileUtils.readFileAsString(new File("A.java"));
GroovyObject obj = new GroovyClassLoader().parseClass(code).newInstance();
// here I want to make A extend B

I tried to use obj.setMetaClass but I don’t find any example in Java.

Can someone has already done this ?

  • 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-14T07:43:58+00:00Added an answer on June 14, 2026 at 7:43 am

    If you’re trying to execute in java, try something like this:

    Class a = new GroovyClassLoader().parseClass(aSourceString);//your A class; 
    Class b = new GroovyClassLoader().parseClass(bSourceString);//your B class
    
    ExpandoMetaClass emc = new ExpandoMetaClass(a, false, true);
    
    List<Class> classes = new ArrayList<Class>();
    classes.add(b);
    MixinInMetaClass.mixinClassesToMetaClass(emc, classes);
    
    MetaClassRegistry mcreg = MetaClassRegistryImpl.getInstance(MetaClassRegistryImpl.LOAD_DEFAULT);
    mcreg.setMetaClass(a, emc);
    emc.initialize();
    
    System.out.println(((GroovyObject)j.newInstance()).invokeMethod("setContext", new Context()));//or however you get a Context obj
    

    If in Groovy, much simpler:

    //given same class parsing as above
    a.mixin(b)
    
    def aObj = a.newInstance()
    aObj.context = new Context()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 tabs, named : Tab1, Tab2 class MainActivity extends TabActivity. MainActivity.java public
I have a service class implemented in Java 6 / Spring 3 that needs
In my project, I have a wrapper class named PlayerCluster.java , which loads the
For example I have a file name call A.java. Here its structure: public class
I have a Java class MyPojo that I am interested in deserializing from JSON.
I have a Java class, Node as follows : class Node { public ArrayList<Node>
hey there guys and girls i have this code that saves json as a
I have situation where my Java class needs to create a ton of certain
I have a template method that needs to instantiate class objects with given class
I have an Spring+Hibernate/Flex application that needs to switch dynamically between database schemas. To

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.