[jboss-jira] [JBoss JIRA] (JASSIST-243) Create class by abstract generic template
Karol Kowalczyk (JIRA)
issues at jboss.org
Thu Feb 12 03:10:49 EST 2015
[ https://issues.jboss.org/browse/JASSIST-243?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13039700#comment-13039700 ]
Karol Kowalczyk edited comment on JASSIST-243 at 2/12/15 3:10 AM:
------------------------------------------------------------------
I'd like to generate new class from generic abstract template. A try to get this by javassist like this:
CtClass newClass = POOL.makeClass("test.templates.impl.GeneratedBigDecimalAction", genericAbstractTemplateClass);
String newGenericSignature="<Ljava/math/BigDecimal;Ljava/lang/String;Ltest/impl/NumberLogic;>Ltest/impl/ActionRoot<Ljava/math/BigDecimal;Ljava/lang/String;Ltest/impl/NumberLogic;>;Ltest/templates/api/IActionNumber2<Ljava/math/BigDecimal;Ljava/lang/String;>;"
newClass.setGenericSignature(genericSignature);
newClass.writeFile("target\test-classes");
newClass.POOL.get(newClass.getName());
But when I try to compile java sources with generated newClass test.templates.impl.GeneratedBigDecimalAction.class, a have error like this:
src\test\java\test\BigDecimalActionTest.java:[13,-1] cannot access test.templates.impl.GeneratedBigDecimalAction
bad class file: target\test-classes\test\templates\impl\GeneratedBigDecimalAction.class
bad signature: <null> bu
Why?
1. source of template (generic abstract template)
package test.templates.impl;
import test.impl.INumberLogic;
import test.impl.ActionRoot;
import java.util.Collection;
import test.templates.api.IActionNumber2;
public abstract class AActionNumber<
Dto extends Number,
Pk,
L extends INumberLogic<Dto, Pk>> extends ActionRoot<Dto, Pk, L>
implements IActionNumber2<Dto, Pk> {
final protected transient Logger logger = LoggerFactory
.getLogger(getClass());
public AActionNumber() throws Exception {
super();
}
public Class<Dto> getPersistentClass() {
Class<Dto> result = null;
if (logic != null) {
result = logic.getPersistentClass();
}
return result;
}
@Override
public String echo(String a) {
return a;
}
}
2. generic super class ActionRoot:
package test.impl;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.TypeVariable;
public class ActionRoot<
Dto extends Number,
Pk,
L extends INumberLogic<Dto, Pk>> {
protected L logic;
@SuppressWarnings({ "unchecked" })
public ActionRoot() throws Exception {
Class<?> tmpClass = this.getClass();
while (!(tmpClass.getGenericSuperclass() instanceof ParameterizedType)) {
tmpClass = tmpClass.getSuperclass();
}
Class<L> logicBeanClass = (Class<L>) ((ParameterizedType) tmpClass
.getGenericSuperclass()).getActualTypeArguments()[2];
try {
logic = (L) logicBeanClass.newInstance();
} catch (Exception ex) {
throw new Exception("Problem z utworzeniem obiektu beanu logic", ex);
}
}
}
3. Generic interfaces:
package test.impl;
public interface INumberLogic<Dto extends Number, Pk> {
Class<Dto> getPersistentClass();
}
package test.templates.api;
public interface IActionNumber2<Dto extends Number, Pk> {
String echo(String a);
}
was (Author: karol.kowalczyk):
I'd like to generate new class from generic abstract template. A try to get this by javassist like this:
CtClass newClass = POOL.makeClass("test.templates.impl.GeneratedBigDecimalAction", genericAbstractTemplateClass);
String newGenericSignature="<Ljava/math/BigDecimal;Ljava/lang/String;Ltest/impl/NumberLogic;>Ltest/impl/ActionRoot<Ljava/math/BigDecimal;Ljava/lang/String;Ltest/impl/NumberLogic;>;Ltest/templates/api/IActionNumber2<Ljava/math/BigDecimal;Ljava/lang/String;>;"
newClass.setGenericSignature(genericSignature);
newClass.writeFile("target/testClasses");
newClass.POOL.get(newClass.getName());
But when I try to compile java sources with generated newClass test.templates.impl.GeneratedBigDecimalAction.class, a have error like this:
src\test\java\test\BigDecimalActionTest.java:[13,-1] cannot access test.templates.impl.GeneratedBigDecimalAction
bad class file: target\test-classes\test\templates\impl\GeneratedBigDecimalAction.class
bad signature: <null> bu
Why?
1. source of template (generic abstract template)
package test.templates.impl;
import test.impl.INumberLogic;
import test.impl.ActionRoot;
import java.util.Collection;
import test.templates.api.IActionNumber2;
public abstract class AActionNumber<
Dto extends Number,
Pk,
L extends INumberLogic<Dto, Pk>> extends ActionRoot<Dto, Pk, L>
implements IActionNumber2<Dto, Pk> {
final protected transient Logger logger = LoggerFactory
.getLogger(getClass());
public AActionNumber() throws Exception {
super();
}
public Class<Dto> getPersistentClass() {
Class<Dto> result = null;
if (logic != null) {
result = logic.getPersistentClass();
}
return result;
}
@Override
public String echo(String a) {
return a;
}
}
2. generic super class ActionRoot:
package test.impl;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.TypeVariable;
public class ActionRoot<
Dto extends Number,
Pk,
L extends INumberLogic<Dto, Pk>> {
protected L logic;
@SuppressWarnings({ "unchecked" })
public ActionRoot() throws Exception {
Class<?> tmpClass = this.getClass();
while (!(tmpClass.getGenericSuperclass() instanceof ParameterizedType)) {
tmpClass = tmpClass.getSuperclass();
}
Class<L> logicBeanClass = (Class<L>) ((ParameterizedType) tmpClass
.getGenericSuperclass()).getActualTypeArguments()[2];
try {
logic = (L) logicBeanClass.newInstance();
} catch (Exception ex) {
throw new Exception("Problem z utworzeniem obiektu beanu logic", ex);
}
}
}
3. Generic interfaces:
package test.impl;
public interface INumberLogic<Dto extends Number, Pk> {
Class<Dto> getPersistentClass();
}
package test.templates.api;
public interface IActionNumber2<Dto extends Number, Pk> {
String echo(String a);
}
> Create class by abstract generic template
> -----------------------------------------
>
> Key: JASSIST-243
> URL: https://issues.jboss.org/browse/JASSIST-243
> Project: Javassist
> Issue Type: Bug
> Affects Versions: 3.19.0-GA
> Reporter: Karol Kowalczyk
> Assignee: Shigeru Chiba
> Priority: Critical
>
--
This message was sent by Atlassian JIRA
(v6.3.11#6341)
More information about the jboss-jira
mailing list