Skip to content

Commit

Permalink
Avoiding synthetic constructors.
Browse files Browse the repository at this point in the history
Fixes #5 .
  • Loading branch information
rafaeldff committed Oct 10, 2012
1 parent a5feeee commit d81b0c9
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/br/com/caelum/iogi/reflection/ClassConstructor.java
Expand Up @@ -17,7 +17,7 @@
import static br.com.caelum.iogi.util.IogiCollections.zip;

public class ClassConstructor {
private final Set<String> names;
private final Set<String> names;
private final Constructor<?> constructor;

public ClassConstructor(final Constructor<?> constructor, final ParameterNamesProvider parameterNamesProvider) {
Expand Down
3 changes: 2 additions & 1 deletion src/br/com/caelum/iogi/reflection/Target.java
Expand Up @@ -111,7 +111,8 @@ public String toString() {
public Constructors constructors(ParameterNamesProvider parameterNamesProvider, DependenciesInjector dependenciesInjector) {
final HashSet<ClassConstructor> classConstructors = new HashSet<ClassConstructor>();
for (final Constructor<?> constructor : getClassType().getDeclaredConstructors()) {
classConstructors.add(new ClassConstructor(constructor, parameterNamesProvider));
if (!constructor.isSynthetic())
classConstructors.add(new ClassConstructor(constructor, parameterNamesProvider));
}

return new Constructors(classConstructors, dependenciesInjector);
Expand Down
@@ -0,0 +1,23 @@
package br.com.caelum.iogi;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.bytecode.ConstPool;
import javassist.bytecode.SyntheticAttribute;

public class GenerateClassfileWithSyntheticConstructor {
public static void main(String[] args) throws Exception {
ClassPool cp = ClassPool.getDefault();
CtClass ctClass = cp.get("WithSyntheticConstructor");
CtConstructor constructor = ctClass.getConstructors()[0];
ConstPool constPool = new ConstPool("WithSyntheticConstructor");

SyntheticAttribute isSynthetic = new SyntheticAttribute(constPool);
constructor.setAttribute(isSynthetic.getName(), isSynthetic.get());

System.out.println("Saving");
ctClass.writeFile("testBytecode");
System.out.println("Saved");
}
}
31 changes: 20 additions & 11 deletions test/br/com/caelum/iogi/reflection/TargetTests.java
Expand Up @@ -9,19 +9,28 @@
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;

public class TargetTests {
private ParameterNamesProvider nullNamesProvider = new ParameterNamesProvider() {
public List<String> lookupParameterNames(AccessibleObject methodOrConstructor) {
return Collections.emptyList();
}
};
private ParameterNamesProvider nullNamesProvider = new ParameterNamesProvider() {
public List<String> lookupParameterNames(AccessibleObject methodOrConstructor) {
return Collections.emptyList();
}
};

@Test
public void willListProtectedConstructors() {
final Target<OnlyOneProtectedConstructor> target = Target.create(OnlyOneProtectedConstructor.class, "foo");
final Constructors classConstructors = target.constructors(nullNamesProvider, DependenciesInjector.nullDependenciesInjector());
assertEquals(1, classConstructors.size());
}
@Test
public void willListProtectedConstructors() {
final Target<OnlyOneProtectedConstructor> target = Target.create(OnlyOneProtectedConstructor.class, "foo");
final Constructors classConstructors = target.constructors(nullNamesProvider,
DependenciesInjector.nullDependenciesInjector());
assertEquals(1, classConstructors.size());
}

@Test
public void willNotListSyntheticConstructors() throws Exception {
Target<?> target = Target.create(Class.forName("WithSyntheticConstructor"), "");
Constructors constructors = target.constructors(nullNamesProvider, DependenciesInjector.nullDependenciesInjector());
assertEquals(0, constructors.size());
}
}
Binary file added testBytecode/WithSyntheticConstructor.class
Binary file not shown.
Binary file added testLib/javassist.jar
Binary file not shown.

0 comments on commit d81b0c9

Please sign in to comment.