use of com.helger.jcodemodel.JDefinedClass in project RoboBinding by RoboBinding.
the class AbstractPresentationModelObjectClassGen method defineTryToCreateFunction.
/*
@Override
public Function tryToCreateFunction(MethodDescriptor methodDescriptor) {
if(methodDescriptor.equals(createMethodDescriptor(ON_CLICK))) {
return new Function() {
@Override
public Object call(Object... args) {
presentationModel.onClick();
return null;
}
};
}
if(methodDescriptor.equals(createMethodDescriptor(ON_CLICK_WITH_EVENT, AbstractViewEvent.class))){
return new Function() {
@Override
public Object call(Object... args) {
boolean result = presentationModel.onLongClickWithEvent((AbstractViewEvent)args[0]);
return (Boolean)result;
}
};
}
return null;
}
*/
public void defineTryToCreateFunction() {
JMethod method = declarePublicMethodOverride("tryToCreateFunction", Function.class);
JVar methodDescriptorParam = method.param(MethodDescriptor.class, "methodDescriptor");
JBlock body = method.body();
for (EventMethodInfo eventMethodInfo : presentationModelInfo.eventMethods()) {
JInvocation getMethod = JExpr.invoke("createMethodDescriptor").arg(eventMethodInfo.name());
if (eventMethodInfo.hasEventArg()) {
getMethod.arg(codeModel.ref(eventMethodInfo.eventArgType()).dotclass());
}
JConditional conditional = body._if(methodDescriptorParam.invoke("equals").arg(getMethod));
JBlock conditionalBody = conditional._then();
// create Function.
JDefinedClass anonymousFunction = codeModel.anonymousClass(Function.class);
JMethod call = declarePublicMethodOverride(anonymousFunction, "call", Object.class);
JVar argsVar = call.varParam(Object.class, "args");
JBlock callBody = call.body();
// call event method.
JInvocation onEvent = presentationModelFieldWithoutThis.invoke(eventMethodInfo.name());
if (eventMethodInfo.hasEventArg()) {
AbstractJClass eventArgClass = codeModel.ref(eventMethodInfo.eventArgType());
onEvent.arg(JExpr.cast(eventArgClass, argsVar.component(JExpr.lit(0))));
}
// call return.
if (eventMethodInfo.hasReturn()) {
JVar returnVar = callBody.decl(codeModel.ref(eventMethodInfo.nonPrimitiveReturnType()), "result", onEvent);
callBody._return(returnVar);
} else {
callBody.add(onEvent);
callBody._return(JExpr._null());
}
// return Function.
conditionalBody._return(JExpr._new(anonymousFunction));
}
body._return(JExpr._null());
}
use of com.helger.jcodemodel.JDefinedClass in project RoboBinding by RoboBinding.
the class ViewBindingObjectClassGen method defineSimpleOneWayPropertyClasses.
/**
* private static class ImageAlphaAttribute implements OneWayPropertyViewAttribute<ImageView, Integer> {
* @Override
* public void updateView(ImageView view, Integer newValue) {
* view.setImageAlpha(newValue);
* }
* }
*/
public void defineSimpleOneWayPropertyClasses() {
for (SimpleOneWayPropertyInfo propInfo : simpleOneWayPropertyInfoList) {
JDefinedClass definedBindingAttributeClass = propInfo.defineBindingClass(new ClassDefinitionCallback() {
@Override
public JDefinedClass define(String typeName) {
try {
return definedClass._class(JMod.PUBLIC | JMod.STATIC, typeName);
} catch (JClassAlreadyExistsException e) {
throw new RuntimeException("Class '" + typeName + "' already exists", e);
}
}
});
AbstractJClass propertyClass = codeModel.ref(propInfo.propertyType());
AbstractJClass oneWayPropertyInterface = codeModel.ref(OneWayPropertyViewAttribute.class).narrow(viewClass, propertyClass);
definedBindingAttributeClass._implements(oneWayPropertyInterface);
JMethod method = definedBindingAttributeClass.method(JMod.PUBLIC, codeModel.VOID, "updateView");
method.annotate(Override.class);
JVar viewParam = method.param(viewClass, "view");
JVar newValueParam = method.param(propertyClass, "newValue");
JBlock body = method.body();
body.invoke(viewParam, propInfo.propertySetter()).arg(newValueParam);
}
}
use of com.helger.jcodemodel.JDefinedClass in project adt4j by sviperll.
the class ValueClassConfiguration method classCustomization.
private static GenerationResult<ClassCustomization> classCustomization(JAnnotationUse annotation, VisitorDefinition visitorDefinition, JDefinedClass valueClass) throws ClassCastException, NullPointerException {
GenerationProcess generation = new GenerationProcess();
AbstractJClass extendsClass = annotation.getParam("extendsClass", AbstractJClass.class);
AbstractJClass wrapperClass = annotation.getParam("wrapperClass", AbstractJClass.class);
if (wrapperClass == null)
throw new NullPointerException("wrapperClass annotation argument should never be null");
String wrapperClassFullName = wrapperClass.fullName();
if (wrapperClassFullName == null)
throw new NullPointerException("wrapperClass.fullName() is null");
if (wrapperClassFullName.equals("java.lang.Object"))
wrapperClass = null;
String className = annotation.getParam("className", String.class);
if (className == null)
throw new NullPointerException("className annotation argument should never be null");
if (wrapperClass == null) {
if (className.equals(":auto")) {
className = autoClassName(visitorDefinition.visitorName());
}
} else {
AbstractJClass wrapperClassErasure = wrapperClass.erasure();
if (wrapperClassErasure instanceof JDefinedClass) {
JDefinedClass definition = (JDefinedClass) wrapperClassErasure;
JAnnotationUse wrapsGeneratedAnnotation = null;
for (JAnnotationUse wrapperAnnotaion : definition.annotations()) {
String annotationClassFullName = wrapperAnnotaion.getAnnotationClass().erasure().fullName();
if (annotationClassFullName != null && annotationClassFullName.equals(WrapsGeneratedValueClass.class.getName())) {
wrapsGeneratedAnnotation = wrapperAnnotaion;
}
}
if (wrapsGeneratedAnnotation == null)
generation.reportError(MessageFormat.format("Wrapper class should be annotated with @{0} annotation.", com.github.sviperll.adt4j.WrapsGeneratedValueClass.class.getName()));
else {
AbstractJClass visitor = wrapsGeneratedAnnotation.getParam("visitor", AbstractJClass.class);
if (visitor == null || visitor.fullName() == null || !visitor.fullName().equals(visitorDefinition.qualifiedName()))
generation.reportError("@" + WrapsGeneratedValueClass.class.getName() + " annotation should have " + visitorDefinition.qualifiedName() + " as visitor argument");
}
}
if (!className.equals(":auto")) {
generation.reportError("You shouldn't define className when wrapperClass is used. Generated class name is derived from wrapper class' extends clause.");
} else {
AbstractJClass extendedClass = wrapperClass._extends();
boolean extendedClassError = false;
if (extendedClass != null) {
if (extendedClass.isError()) {
className = extendedClass.name();
} else {
if (valueClass == null) {
extendedClassError = true;
} else {
String valueClassFullName = valueClass.fullName();
if (valueClassFullName == null || !valueClassFullName.equals(extendedClass.erasure().fullName()))
extendedClassError = true;
else
className = valueClass.name();
}
}
}
if (extendedClass == null || extendedClassError) {
generation.reportError("Wrapper class should explicitly extend non-existing class, that class is to be generated");
className = autoClassName(visitorDefinition.visitorName());
} else {
boolean typeParamsError = false;
List<? extends AbstractJClass> typeArguments = extendedClass.getTypeParameters();
List<JTypeVar> generatedTypeParameters = visitorDefinition.nonspecialTypeParameters();
JTypeVar[] wrapperTypeParameters = wrapperClass.typeParams();
if (wrapperTypeParameters.length != typeArguments.size() || wrapperTypeParameters.length != generatedTypeParameters.size())
typeParamsError = true;
else {
for (int i = 0; i < wrapperTypeParameters.length; i++) {
JTypeVar wrapperTypeParameter = wrapperTypeParameters[i];
if (typeArguments.get(i) != wrapperTypeParameter) {
typeParamsError = true;
break;
}
}
}
if (typeParamsError) {
generation.reportError("Wrapper class should declare same type-parameters as generated class and should extend generated class with all type-arguments applied");
}
}
}
}
ClassCustomization classCustomization = new ClassCustomization(className, wrapperClass, extendsClass);
return generation.createGenerationResult(classCustomization);
}
use of com.helger.jcodemodel.JDefinedClass in project adt4j by sviperll.
the class FinalValueClassModel method buildCaseClasses.
private Map<String, JDefinedClass> buildCaseClasses(Serialization serialization) throws JClassAlreadyExistsException {
Map<String, JDefinedClass> caseClasses = new TreeMap<>();
for (JMethod interfaceMethod : environment.visitorDefinition().methodDefinitions()) {
JDefinedClass caseClass = buildCaseClass(interfaceMethod.name(), serialization);
caseClasses.put(interfaceMethod.name(), caseClass);
}
return caseClasses;
}
use of com.helger.jcodemodel.JDefinedClass in project adt4j by sviperll.
the class FinalValueClassModel method buildFactory.
JMethod buildFactory(Map<String, JMethod> constructorMethods) throws JClassAlreadyExistsException {
JDefinedClass factory = buildFactoryClass(constructorMethods);
JFieldVar factoryField = environment.buildValueClassField(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, factory, "FACTORY");
JAnnotationUse fieldAnnotationUse = factoryField.annotate(SuppressWarnings.class);
JAnnotationArrayMember paramArray = fieldAnnotationUse.paramArray("value");
paramArray.param("unchecked");
paramArray.param("rawtypes");
factoryField.init(JExpr._new(factory));
JMethod factoryMethod = environment.buildValueClassMethod(Source.toJMod(environment.factoryMethodAccessLevel()) | JMod.STATIC, "factory");
Source.annotateNonnull(factoryMethod);
JAnnotationUse methodAnnotationUse = factoryMethod.annotate(SuppressWarnings.class);
methodAnnotationUse.param("value", "unchecked");
for (JTypeVar visitorTypeParameter : environment.getValueTypeParameters()) {
JTypeVar typeParameter = factoryMethod.generify(visitorTypeParameter.name());
typeParameter.boundLike(visitorTypeParameter);
}
AbstractJClass usedValueClassType = environment.wrappedValueClassType(factoryMethod.typeParams());
factoryMethod.type(environment.visitor(usedValueClassType, usedValueClassType, types._RuntimeException).getVisitorType());
factoryMethod.body()._return(factoryField);
return factoryMethod;
}
Aggregations