Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for multi-argument constructors #78

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<!-- Disable resources (project has none) -->
Expand Down
89 changes: 88 additions & 1 deletion src/com/esotericsoftware/reflectasm/ConstructorAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Type;

abstract public class ConstructorAccess<T> {
boolean isNonStaticMemberClass;
Expand All @@ -36,12 +37,30 @@ public boolean isNonStaticMemberClass () {
* fron the enclosing instance. */
abstract public T newInstance ();

abstract public T newInstance0 (Object... args);

public T newInstance0 () {
return newInstance();
}

/** Constructor for inner classes (non-static nested classes).
* @param enclosingInstance The instance of the enclosing type to which this inner instance is related to (assigned to its
* synthetic this$0 field). */
abstract public T newInstance (Object enclosingInstance);

static public <T> ConstructorAccess<T> get (Class<T> type) {
return get(type, (Class<?>[])null);
}

static public <T> ConstructorAccess<T> get (Class<T> type, Constructor<?> constructor) {
Class<?>[] parameterTypes = null;
if (constructor != null) {
parameterTypes = constructor.getParameterTypes();
}
return get(type, parameterTypes);
}

static public <T> ConstructorAccess<T> get (Class<T> type, Class<?>... args) {
Class enclosingType = type.getEnclosingClass();
boolean isNonStaticMemberClass = enclosingType != null && type.isMemberClass() && !Modifier.isStatic(type.getModifiers());

Expand Down Expand Up @@ -88,11 +107,14 @@ static public <T> ConstructorAccess<T> get (Class<T> type) {
? "com/esotericsoftware/reflectasm/PublicConstructorAccess"
: "com/esotericsoftware/reflectasm/ConstructorAccess";

ClassWriter cw = new ClassWriter(0);
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, accessClassNameInternal, null, superclassNameInternal, null);

insertConstructor(cw, superclassNameInternal);
insertNewInstance(cw, classNameInternal);
if (args != null) {
insertNewInstance0(cw, classNameInternal, args);
}
insertNewInstanceInner(cw, classNameInternal, enclosingClassNameInternal);

cw.visitEnd();
Expand Down Expand Up @@ -138,6 +160,71 @@ static void insertNewInstance (ClassWriter cw, String classNameInternal) {
mv.visitEnd();
}

static void insertNewInstance0 (ClassWriter cw, String classNameInternal, Class<?>[] args) {
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "newInstance0", "([Ljava/lang/Object;)Ljava/lang/Object;", null, null);
mv.visitCode();
mv.visitTypeInsn(NEW, classNameInternal);
mv.visitInsn(DUP);

StringBuilder buffer = new StringBuilder(128);
buffer.setLength(0);
buffer.append('(');
if (args == null)
args = new Class[0];
for (int i = 0; i < args.length; i++) {
mv.visitVarInsn(ALOAD, 1);
mv.visitIntInsn(BIPUSH, i);
mv.visitInsn(AALOAD);
Type paramType = Type.getType(args[i]);
switch (paramType.getSort()) {
case org.objectweb.asm.Type.BOOLEAN:
mv.visitTypeInsn(CHECKCAST, "java/lang/Boolean");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z");
break;
case org.objectweb.asm.Type.BYTE:
mv.visitTypeInsn(CHECKCAST, "java/lang/Byte");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Byte", "byteValue", "()B");
break;
case org.objectweb.asm.Type.CHAR:
mv.visitTypeInsn(CHECKCAST, "java/lang/Character");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Character", "charValue", "()C");
break;
case org.objectweb.asm.Type.SHORT:
mv.visitTypeInsn(CHECKCAST, "java/lang/Short");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Short", "shortValue", "()S");
break;
case org.objectweb.asm.Type.INT:
mv.visitTypeInsn(CHECKCAST, "java/lang/Integer");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I");
break;
case org.objectweb.asm.Type.FLOAT:
mv.visitTypeInsn(CHECKCAST, "java/lang/Float");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Float", "floatValue", "()F");
break;
case org.objectweb.asm.Type.LONG:
mv.visitTypeInsn(CHECKCAST, "java/lang/Long");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J");
break;
case org.objectweb.asm.Type.DOUBLE:
mv.visitTypeInsn(CHECKCAST, "java/lang/Double");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Double", "doubleValue", "()D");
break;
case org.objectweb.asm.Type.ARRAY:
mv.visitTypeInsn(CHECKCAST, paramType.getDescriptor());
break;
case org.objectweb.asm.Type.OBJECT:
mv.visitTypeInsn(CHECKCAST, paramType.getInternalName());
break;
}
buffer.append(paramType.getDescriptor());
}
buffer.append(")V");
mv.visitMethodInsn(INVOKESPECIAL, classNameInternal, "<init>", buffer.toString());
mv.visitInsn(ARETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}

static void insertNewInstanceInner (ClassWriter cw, String classNameInternal, String enclosingClassNameInternal) {
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "newInstance", "(Ljava/lang/Object;)Ljava/lang/Object;", null, null);
mv.visitCode();
Expand Down
17 changes: 16 additions & 1 deletion test/com/esotericsoftware/reflectasm/ConstructorAccessTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@

public class ConstructorAccessTest extends TestCase {
public void testNewInstance () {
ConstructorAccess<SomeClass> access = ConstructorAccess.get(SomeClass.class);
ConstructorAccess<SomeClass> access = ConstructorAccess.get(SomeClass.class, String.class, int.class, float.class, Float.class, String.class);
SomeClass someObject = new SomeClass();
SomeClass someObject0 = new SomeClass("11", 12, 13f, 14f, "15");
assertEquals(someObject, access.newInstance());
assertEquals(someObject, access.newInstance());
assertEquals(someObject, access.newInstance());
assertEquals(someObject0, access.newInstance0("11", 12, 13f, 14f, "15"));
assertEquals(someObject0, access.newInstance0("11", 12, 13f, 14f, "15"));
assertEquals(someObject0, access.newInstance0("11", 12, 13f, 14f, "15"));
}

public void testPackagePrivateNewInstance () {
Expand Down Expand Up @@ -133,6 +137,17 @@ static public class SomeClass {
Float test2;
private String test3;

public SomeClass() {
}

public SomeClass(String name, int intValue, float test1, Float test2, String test3) {
this.name = name;
this.intValue = intValue;
this.test1 = test1;
this.test2 = test2;
this.test3 = test3;
}

public boolean equals (Object obj) {
if (this == obj) return true;
if (obj == null) return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright (c) 2008, Nathan Sweet
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* 3. Neither the name of Esoteric Software nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

package com.esotericsoftware.reflectasm.benchmark;

import com.esotericsoftware.reflectasm.ConstructorAccess;

import java.lang.reflect.Constructor;

public class ConstructorAccessBenchmark2 extends Benchmark {
public ConstructorAccessBenchmark2() throws Exception {
int count = 1000000;
Object[] dontCompileMeAway = new Object[count];

Class type = SomeClass.class;
Constructor constructor = type.getDeclaredConstructor(String.class);
Constructor constructor0 = type.getDeclaredConstructor();
ConstructorAccess<SomeClass> access = ConstructorAccess.get(type, String.class);

for (int i = 0; i < 100; i++)
for (int ii = 0; ii < count; ii++)
dontCompileMeAway[ii] = access.newInstance0("11");
for (int i = 0; i < 100; i++)
for (int ii = 0; ii < count; ii++)
dontCompileMeAway[ii] = constructor.newInstance("11");
warmup = false;

for (int i = 0; i < 100; i++) {
start();
for (int ii = 0; ii < count; ii++)
dontCompileMeAway[ii] = access.newInstance0("11");
end("ConstructorAccess");
}
for (int i = 0; i < 100; i++) {
start();
for (int ii = 0; ii < count; ii++)
dontCompileMeAway[ii] = constructor.newInstance("11");
end("Constructor");
}

chart("Constructor");
}

static public class SomeClass {
public String name;

public SomeClass() {
}

public SomeClass(String name) {
this.name = name;
}
}

public static void main (String[] args) throws Exception {
new ConstructorAccessBenchmark2();
}
}