跳至主要内容

Java Synthetic Constructor

public class DefaultConstructor {

    private static class B {
    }

    public static void main(String[] args) throws IllegalAccessException, InstantiationException {
        B.class.newInstance();
    }
}

When I use reflection to create an object from B, it give me the following exception:

java.lang.IllegalAccessException: Class DefaultConstructor can not access a member of class DefaultConstructor$B with modifiers “private”

Let us see which member the DefaultConstructor is trying to access by viewing the source.

Source Code

The following is part of source code of Class#newInstance()

Constructor<T> tmpConstructor = cachedConstructor;
// Security check (same as in java.lang.reflect.Constructor)
int modifiers = tmpConstructor.getModifiers();
if (!Reflection.quickCheckMemberAccess(this, modifiers)) {
    Class<?> caller = Reflection.getCallerClass();
    if (newInstanceCallerCache != caller) {
        Reflection.ensureMemberAccess(caller, this, null, modifiers); // <--- here throws exception
        newInstanceCallerCache = caller;
    }
}

As it turns out, our code fails the constructor accessibility check, so it proves that the constructor the compiler provided is private!

Synthetic Default Constructor

We all know compiler will add a default constructor for us, but how this constructor is like?
Let’s look at the code from above example:

// original code:
private static clsss B {
}

//code de-compiled from byte code:
private static class B {
    private B() {
    }
}

The compiler is clever enough to detect that this is a private class, so it generate a private default constructor. In the most cases, we have a public class, so compiler will just generate a public constructor:

public class DefaultConstructor {
}

// de-compiled from byte code
public class DefaultConstructor {
    public DefaultConstructor() {
    }
}

What about protected and default visibility?

static class C {
}

protected static class D {
}

// de-compiled from byte code
protected static class D {
    protected D() {
    }
}

static class C {
    C() {
    }
}

So we can come to conclusion: compiler will generate constructor according to class’s visibility.

More Synthetic Constructor

Now, we move to a more complex example:

public class DefaultConstructor {
    private static class A {
    }

    public static void main(String[] args) {
        new A();
    }
}

Is this example will have different constructor? Let’s see the byte code:

$ javap -c -p DefaultConstructor\$A.class 

Compiled from "DefaultConstructor.java"
class reflect.DefaultConstructor$A {
  private reflect.DefaultConstructor$A();
    Code:
       0: aload_0
       1: invokespecial #2                  // Method java/lang/Object."<init>":()V
       4: return

  reflect.DefaultConstructor$A( 
      reflect.DefaultConstructor$1);
    Code:
       0: aload_0
       1: invokespecial #1                  // Method "<init>":()V
       4: return
}

We can clearly see two constructors! One is private, as we have seen before. Another is a default, with a parameter.
What this constructor for and what is DefaultConstructor$1? (Try to give your answer before moving on)


Nested Class Synthetic Constructor/Class

In order to answer this question, we should understand what’s the relationship between class A and DefaultConstructor: A is the nested class of DefaultConstructor, A has the privilege to access the private field/method of DefaultConstructor. So how is this implemented? The answer is adding a new constructor with default visibility for DefaultConstructor to use.

// this synthetic constructor is only for DefaultConstructor to use
reflect.DefaultConstructor$A( 
 reflect.DefaultConstructor$1);
  Code:
     0: aload_0
     1: invokespecial #1                  // Method "<init>":()V
     4: return

Because we already have a private constructor which has no parameter, we have to add a parameter to distinguish them (Otherwise, if we invoke new A() in class A, which constructor will be invoked?). So compiler synthesize an empty class called DefaultConstructor$1 to work as parameter.

$ javap -c -p DefaultConstructor\$1.class 

Compiled from "DefaultConstructor.java"
class reflect.DefaultConstructor$1 {
}

So the result code is like this:

public class DefaultConstructor {
    public DefaultConstructor() {
    }

    public static void main(String[] args) {
        new DefaultConstructor.A(null);
    }

    private static class A {
        private int a;

        private A() {
        }
        A(SomeClass o) {
            this();
        }
    }
}

Ref

Written with StackEdit.

评论

此博客中的热门博文

Spring Boot: Customize Environment

Spring Boot: Customize Environment Environment variable is a very commonly used feature in daily programming: used in init script used in startup configuration used by logging etc In Spring Boot, all environment variables are a part of properties in Spring context and managed by Environment abstraction. Because Spring Boot can handle the parse of configuration files, when we want to implement a project which uses yml file as a separate config file, we choose the Spring Boot. The following is the problems we met when we implementing the parse of yml file and it is recorded for future reader. Bind to Class Property values can be injected directly into your beans using the @Value annotation, accessed via Spring’s Environment abstraction or bound to structured objects via @ConfigurationProperties. As the document says, there exists three ways to access properties in *.properties or *.yml : @Value : access single value Environment : can access multi...

Elasticsearch: Join and SubQuery

Elasticsearch: Join and SubQuery Tony was bothered by the recent change of search engine requirement: they want the functionality of SQL-like join in Elasticsearch! “They are crazy! How can they think like that. Didn’t they understand that Elasticsearch is kind-of NoSQL 1 in which every index should be independent and self-contained? In this way, every index can work independently and scale as they like without considering other indexes, so the performance can boost. Following this design principle, Elasticsearch has little related supports.” Tony thought, after listening their requirements. Leader notice tony’s unwillingness and said, “Maybe it is hard to do, but the requirement is reasonable. We need to search person by his friends, didn’t we? What’s more, the harder to implement, the more you can learn from it, right?” Tony thought leader’s word does make sense so he set out to do the related implementations Application-Side Join “The first implementation ...

Learn Spring Expression Language

When reading the source code of some Spring based projects, we can see some code like following: @Value( "${env}" ) private int value ; and like following: @Autowired public void configure (MovieFinder movieFinder, @ Value ("#{ systemProperties[ 'user.region' ] } ") String defaultLocale) { this.movieFinder = movieFinder; this.defaultLocale = defaultLocale; } In this way, we can inject values from different sources very conveniently, and this is the features of Spring EL. What is Spring EL? How to use this handy feature to assist our developments? Today, we are going to learn some basics of Spring EL. Features The full name of Spring EL is Spring Expression Language, which exists in form of Java string and evaluated by Spring. It supports many syntax, from simple property access to complex safe navigation – method invocation when object is not null. And the following is the feature list from Spring EL document : ...