use of org.apache.tapestry5.plastic.PlasticClass in project flowlogix by flowlogix.
the class AjaxAnnotationWorker method transform.
@Override
public void transform(PlasticClass plasticClass, TransformationSupport support, MutableComponentModel model) {
boolean hasTrackerMixin = model.getMixinClassNames().contains(SessionTracker.class.getName());
for (PlasticMethod method : plasticClass.getMethodsWithAnnotation(AJAX.class)) {
final AJAX annotation = method.getAnnotation(AJAX.class);
final boolean isVoid = method.isVoid();
if (annotation.requireSession() && (!hasTrackerMixin)) {
model.addMixinClassName(SessionTracker.class.getName());
hasTrackerMixin = true;
}
method.addAdvice(new MethodAdvice() {
@Override
@SneakyThrows(IOException.class)
public void advise(MethodInvocation invocation) {
if (!request.isXHR() || annotation.requireSession() == false) {
invocation.proceed();
} else {
// do not invoke on bad sessions
if (SessionTrackerUtil.isValidSession(rg.getActivePageName(), rg.getRequest().getSession(false))) {
invocation.proceed();
} else {
showSessionExpiredMessage = true;
SessionTrackerUtil.redirectToSelf(rg, linkSource);
if (!isVoid) {
invocation.setReturnValue(null);
}
return;
}
}
Object result = null;
if (!isVoid) {
result = invocation.getReturnValue();
}
if (!request.isXHR()) {
if (result != null) {
result = defaultForReturnType(result.getClass());
}
} else {
if (annotation.discardAfter()) {
cs.getActivePage().getComponentResources().discardPersistentFieldChanges();
}
}
if (!isVoid) {
invocation.setReturnValue(result);
}
}
});
}
}
use of org.apache.tapestry5.plastic.PlasticClass in project flowlogix by flowlogix.
the class EJBAnnotationWorker method transform.
@Override
@SneakyThrows({ NamingException.class })
public void transform(PlasticClass plasticClass, TransformationSupport support, MutableComponentModel model) {
for (PlasticField field : plasticClass.getFieldsWithAnnotation(EJB.class)) {
final EJB annotation = field.getAnnotation(EJB.class);
final Stateful stateful = field.getAnnotation(Stateful.class);
final String fieldType = field.getTypeName();
final String fieldName = field.getName();
final String mappedName = annotation.mappedName();
final JNDIObjectLocator locator = JNDIObjectLocator.builder().build();
final String lookupname = getLookupName(annotation, fieldType, locator);
Object injectionValue = lookupBean(field, fieldType, fieldName, lookupname, mappedName, stateful, locator);
if (injectionValue != null) {
field.claim(annotation);
}
}
}
use of org.apache.tapestry5.plastic.PlasticClass in project tapestry-5 by apache.
the class MethodAdviceManager method createNewMethod.
private void createNewMethod() {
String[] exceptions = advisedMethodNode.exceptions == null ? null : advisedMethodNode.exceptions.toArray(new String[0]);
// Remove the private flag, so that the MethodInvocation implementation (in the same package)
// can directly access the method without an additional access method.
MethodNode mn = new MethodNode(advisedMethodNode.access & ~Opcodes.ACC_PRIVATE, newMethodName, advisedMethodNode.desc, advisedMethodNode.signature, exceptions);
// Copy everything else about the advisedMethodNode over to the new node
advisedMethodNode.accept(mn);
// Add this new method, with the same implementation as the original method, to the
// PlasticClass
plasticClass.classNode.methods.add(mn);
}
use of org.apache.tapestry5.plastic.PlasticClass in project tapestry-5 by apache.
the class LogWorker method transform.
public void transform(PlasticClass plasticClass, TransformationSupport support, MutableComponentModel model) {
List<PlasticMethod> methods = plasticClass.getMethodsWithAnnotation(Log.class);
if (methods.isEmpty()) {
return;
}
final MethodAdvice loggingAdvice = new LoggingAdvice(model.getLogger(), exceptionTracker);
for (PlasticMethod method : methods) {
method.addAdvice(loggingAdvice);
}
}
use of org.apache.tapestry5.plastic.PlasticClass in project tapestry-5 by apache.
the class OnEventWorker method implementDispatchMethod.
private void implementDispatchMethod(final PlasticClass plasticClass, final boolean isRoot, final MutableComponentModel model, final Flow<EventHandlerMethod> eventHandlerMethods) {
plasticClass.introduceMethod(TransformConstants.DISPATCH_COMPONENT_EVENT_DESCRIPTION).changeImplementation(new InstructionBuilderCallback() {
public void doBuild(InstructionBuilder builder) {
builder.startVariable("boolean", new LocalVariableCallback() {
public void doBuild(LocalVariable resultVariable, InstructionBuilder builder) {
if (!isRoot) {
// As a subclass, there will be a base class implementation (possibly empty).
builder.loadThis().loadArguments().invokeSpecial(plasticClass.getSuperClassName(), TransformConstants.DISPATCH_COMPONENT_EVENT_DESCRIPTION);
// First store the result of the super() call into the variable.
builder.storeVariable(resultVariable);
builder.loadArgument(0).invoke(Event.class, boolean.class, "isAborted");
builder.when(Condition.NON_ZERO, RETURN_TRUE);
} else {
// No event handler method has yet been invoked.
builder.loadConstant(false).storeVariable(resultVariable);
}
boolean hasRestEndpointEventHandlerMethod = false;
JSONArray restEndpointEventHandlerMethods = null;
for (EventHandlerMethod method : eventHandlerMethods) {
method.buildMatchAndInvocation(builder, resultVariable);
model.addEventHandler(method.eventType);
if (method.handleActivationEventContext) {
model.doHandleActivationEventContext();
}
// We're collecting this info for all components, even considering REST
// events are only triggered in pages, because we can have REST event
// handler methods in base classes too, and we need this info
// for generating complete, correct OpenAPI documentation.
final OnEvent onEvent = method.method.getAnnotation(OnEvent.class);
final String methodName = method.method.getDescription().methodName;
if (isRestEndpointEventHandlerMethod(onEvent, methodName)) {
hasRestEndpointEventHandlerMethod = true;
if (restEndpointEventHandlerMethods == null) {
restEndpointEventHandlerMethods = new JSONArray();
}
JSONObject methodMeta = new JSONObject();
methodMeta.put("name", methodName);
JSONArray parameters = new JSONArray();
for (MethodParameter parameter : method.method.getParameters()) {
parameters.add(parameter.getType());
}
methodMeta.put("parameters", parameters);
restEndpointEventHandlerMethods.add(methodMeta);
}
}
// memory by not setting it to all component models.
if (model.isPage()) {
model.setMeta(InternalConstants.REST_ENDPOINT_EVENT_HANDLER_METHOD_PRESENT, hasRestEndpointEventHandlerMethod ? InternalConstants.TRUE : InternalConstants.FALSE);
}
// methods in components, something that would be ignored anyway.
if (restEndpointEventHandlerMethods != null) {
model.setMeta(InternalConstants.REST_ENDPOINT_EVENT_HANDLER_METHODS, restEndpointEventHandlerMethods.toCompactString());
}
builder.loadVariable(resultVariable).returnResult();
}
});
}
});
}
Aggregations