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

  • Home
  • SEARCH
  • 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 9171493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:10:13+00:00 2026-06-17T16:10:13+00:00

I have a legacy (not modifiable :-() code, that have a static initializer block.

  • 0

I have a legacy (not modifiable :-() code, that have a static initializer block.
I would like to create a subclass – but without running the static block.

Is there any way doing this? For example: class.ForName method has a second argument which is responsibe for initializing the class – but sadly, it doesn’t work for me (probably means something else ..): http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#forName(java.lang.String, boolean, java.lang.ClassLoader)

UPDATE: the above mentioned class.forName does not trigget initialization – but asking for a newInstance does 🙁

Thanks,
krisy

  • 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-17T16:10:14+00:00Added an answer on June 17, 2026 at 4:10 pm

    You can always patch the old class with ASM. Generating a new class from the old bytecode by ignoring the clinit block should be easy.

    import org.objectweb.asm.*;
    import org.objectweb.asm.commons.EmptyVisitor;
    import java.io.*;
    
    public class ClinitKiller {
        public static void main (String[] args) {
            final InputStream input = ClinitKiller.class.getResourceAsStream(Test.class.getName() + ".class");
            try {
                final byte[] bytes = instrument(input);
                FileOutputStream out = new FileOutputStream("/tmp/Test.class");
                out.write(bytes);
                out.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public static byte[] instrument(InputStream is) throws IOException {
            ClassReader reader = new ClassReader(is);
            ClassWriter writer = new ClassWriter(0);
            ClinitKillerClassAdapter classAdapter = new ClinitKillerClassAdapter(writer);
            reader.accept(classAdapter, 0);
            return writer.toByteArray();
        }
    }
    
    class ClinitKillerClassAdapter extends ClassAdapter {
        public ClinitKillerClassAdapter(final ClassVisitor cv) {
            super(cv);
        }
    
        public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature, final String[] exceptions) {
            if (name.equals("<clinit>")) {
                return new EmptyVisitor();
            }
            return cv.visitMethod(access, name, desc, signature, exceptions);
        }
    }
    

    Here is the before and after for the following class:

    public class Test {
        private static final String value;
        static {
            System.out.println("Test static");
            value = "test value";
        }
        public static void main(String[] args) {
            System.out.println(value);
        }
    }
    

    Before:

    javap -c Test
    Compiled from "Test.java"
    public class Test extends java.lang.Object{
        public Test();
        Code:
        0:  aload_0
        1:  invokespecial   #1; //Method java/lang/Object."<init>":()V
        4:  return
    
        public static void main(java.lang.String[]);
        Code:
        0:  getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;
        3:  getstatic   #3; //Field value:Ljava/lang/String;
        6:  invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
        9:  return
    
        static {};
        Code:
        0:  getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;
        3:  ldc #5; //String Test static
        5:  invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
        8:  ldc #6; //String test value
        10: putstatic   #3; //Field value:Ljava/lang/String;
        13: return
    
    }
    

    Output:
    Test static
    test value

    After:

    javap -c Test
    Compiled from "Test.java"
    public class Test extends java.lang.Object{
        public Test();
        Code:
        0:  aload_0
        1:  invokespecial   #11; //Method java/lang/Object."<init>":()V
        4:  return
    
        public static void main(java.lang.String[]);
        Code:
        0:  getstatic   #21; //Field java/lang/System.out:Ljava/io/PrintStream;
        3:  getstatic   #23; //Field value:Ljava/lang/String;
        6:  invokevirtual   #29; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
        9:  return
    
    }
    

    Output:
    null

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

Sidebar

Related Questions

I have a legacy code implemented in C (not C++). I would like to
We have a legacy system that requires a format which is not html but
I have some legacy classic ASP code (not ASP.Net, but ASP), and I want
I have a legacy application that I would like to move over to Symfony2.
I have legacy code that has cache implementation that looks like this: long lastUpadate;
I have legacy code that I am slowly moving to C# MVC. How would
I have a legacy class that the class itself is not a generic but
I have a legacy Java (not my native language) app that I'm trying to
As the post title implies, I have a legacy database (not sure if that
I have legacy code that is using an enum as a range and iterating

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.