use of org.glassfish.apf.AnnotationProcessorException in project Payara by payara.
the class InitHandler method processAnnotation.
protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo, EjbContext[] ejbContexts) throws AnnotationProcessorException {
Init init = (Init) ainfo.getAnnotation();
for (EjbContext next : ejbContexts) {
EjbSessionDescriptor sessionDescriptor = (EjbSessionDescriptor) next.getDescriptor();
Method m = (Method) ainfo.getAnnotatedElement();
// Check for matching method on home and/or local home interface.
int numMatches = 0;
String adaptedCreateMethodName = init.value();
try {
if (sessionDescriptor.isRemoteInterfacesSupported()) {
addInitMethod(sessionDescriptor, m, adaptedCreateMethodName, false);
numMatches++;
}
} catch (Exception e) {
}
try {
if (sessionDescriptor.isLocalInterfacesSupported()) {
addInitMethod(sessionDescriptor, m, adaptedCreateMethodName, true);
numMatches++;
}
} catch (Exception e) {
}
if (numMatches == 0) {
log(Level.SEVERE, ainfo, localStrings.getLocalString("enterprise.deployment.annotation.handlers.notmatchcreate", "Unable to find matching Home create method for Init method {0} on bean {1}.", new Object[] { m, sessionDescriptor.getName() }));
return getDefaultFailedResult();
}
}
return getDefaultProcessedResult();
}
use of org.glassfish.apf.AnnotationProcessorException in project Payara by payara.
the class AnnotationProcessorImpl method process.
private ProcessingResult process(ProcessingContext ctx, Class c) throws AnnotationProcessorException {
Scanner scanner = ctx.getProcessingInput();
ProcessingResultImpl result = new ProcessingResultImpl();
// let's see first if this package is new to us and annotated.
Package classPackage = c.getPackage();
if (classPackage != null && visitedPackages.add(classPackage)) {
// new package
result.add(classPackage, processAnnotations(ctx, ElementType.PACKAGE, classPackage));
}
ComponentInfo info = null;
try {
info = scanner.getComponentInfo(c);
} catch (NoClassDefFoundError err) {
// issue 456: allow verifier to report this issue
AnnotationProcessorException ape = new AnnotationProcessorException(AnnotationUtils.getLocalString("enterprise.deployment.annotation.classnotfounderror", "Class [ {0} ] not found. Error while loading [ {1} ]", new Object[] { err.getMessage(), c }));
ctx.getErrorHandler().error(ape);
// annotation processing
return result;
}
// process the class itself.
AnnotatedElementHandler handler = ctx.getHandler();
logStart(handler, ElementType.TYPE, c);
result.add(c, processAnnotations(ctx, c));
// now dive into the fields.
for (Field field : info.getFields()) {
result.add(field, processAnnotations(ctx, ElementType.FIELD, field));
}
// constructors...
for (Constructor constructor : info.getConstructors()) {
logStart(ctx.getHandler(), ElementType.CONSTRUCTOR, constructor);
result.add(constructor, processAnnotations(ctx, constructor));
// parameters
processParameters(ctx, constructor.getParameterAnnotations());
logEnd(ctx.getHandler(), ElementType.CONSTRUCTOR, constructor);
}
// methods...
for (Method method : info.getMethods()) {
logStart(ctx.getHandler(), ElementType.METHOD, method);
result.add(method, processAnnotations(ctx, method));
// parameters
processParameters(ctx, method.getParameterAnnotations());
logEnd(ctx.getHandler(), ElementType.METHOD, method);
}
// Because of annotation inheritance, we need to to travel to
// the superclasses to ensure that annotations defined at the
// TYPE level are processed at this component level.
// Note : so far, I am ignoring the implemented interfaces
Class currentClass = c.getSuperclass();
while (currentClass != null && !currentClass.equals(Object.class)) {
// the trick is to add the results for this class, not
// for the ones they are defined in...
result.add(c, processAnnotations(ctx, currentClass));
currentClass = currentClass.getSuperclass();
}
// end of class processing, we need to get the top handler
// since it may have changed during the annotation processing
logEnd(ctx.getHandler(), ElementType.TYPE, c);
return result;
}
use of org.glassfish.apf.AnnotationProcessorException in project Payara by payara.
the class ConnectorValidator method accept.
public void accept(ConnectorDescriptor descriptor) {
// make sure that the ActivationSpec class implement ActivationSpec interface.
validateActivationSpec(descriptor);
// validate & process annotations if a valid connector annotation is not already processed
if (!descriptor.getValidConnectorAnnotationProcessed()) {
Set<AnnotationInfo> annotations = descriptor.getConnectorAnnotations();
String raClass = descriptor.getResourceAdapterClass();
if (annotations.size() == 0) {
return;
}
// only one annotation is present
if (annotations.size() == 1) {
Iterator<AnnotationInfo> it = annotations.iterator();
AnnotationInfo annotationInfo = it.next();
Class claz = (Class) annotationInfo.getAnnotatedElement();
Connector connector = (Connector) annotationInfo.getAnnotation();
ConnectorAnnotationHandler.processDescriptor(claz, connector, descriptor);
Collection<AnnotationInfo> configProperties = descriptor.getConfigPropertyAnnotations(claz.getName());
if (configProperties != null) {
for (AnnotationInfo ai : configProperties) {
ConfigPropertyHandler handler = new ConfigPropertyHandler();
try {
handler.processAnnotation(ai);
} catch (AnnotationProcessorException e) {
RuntimeException re = new RuntimeException("Unable to process ConfigProperty " + "annotation in class [" + claz.getName() + "] : " + e.getMessage());
re.initCause(e);
throw re;
}
}
}
} else {
// are present, ignore them.
if (raClass == null || raClass.equals("")) {
// all the cases below are unacceptable, fail deployment
if (annotations.size() > 1) {
throw new RuntimeException("cannot determine appropriate @Connector annotation as multiple " + "annotations are present");
}
}
}
}
// check whether outbound is defined, if so, atleast one connection-definition must be present
if (descriptor.getOutBoundDefined()) {
Set connectionDefinitions = descriptor.getOutboundResourceAdapter().getConnectionDefs();
if (connectionDefinitions.size() == 0) {
throw new RuntimeException("Invalid connector descriptor for RAR [ " + descriptor.getName() + " ], when " + "outbound-resource-adapter is specified," + "atleast one connection-definition must be specified either via annotation or via descriptor");
}
}
if (_logger.isLoggable(Level.FINEST)) {
_logger.log(Level.FINEST, descriptor.toString());
}
processConfigProperties(descriptor);
// processed all annotations, clear from book-keeping
descriptor.getConnectorAnnotations().clear();
descriptor.getAllConfigPropertyAnnotations().clear();
descriptor.getConfigPropertyProcessedClasses().clear();
}
use of org.glassfish.apf.AnnotationProcessorException in project Payara by payara.
the class AnnotationProcessorImpl method process.
private void process(ProcessingContext ctx, AnnotationInfo element, HandlerProcessingResultImpl result) throws AnnotationProcessorException {
Annotation annotation = element.getAnnotation();
if (AnnotationUtils.shouldLog("annotation")) {
logger.finer("Annotation : " + annotation.annotationType().getName() + " delegate = " + delegate);
}
result.addResult(annotation.annotationType(), ResultType.UNPROCESSED);
// we ignore all java.* annotations
Package annPackage = annotation.annotationType().getPackage();
if (annPackage != null && annPackage.getName().startsWith("java.lang"))
return;
List<AnnotationHandler> annotationHandlers = handlers.get(annotation.annotationType().getName());
if (annotationHandlers != null) {
for (AnnotationHandler handler : annotationHandlers) {
// here we need to be careful, we are ready to invoke a handler
// to process a particular annotation type. However, this handler
// may have defined a list of annotations that should be processed
// (if present on the annotated element) before itself.
// do this check and process those annotations first.
Class<? extends Annotation>[] dependencies = handler.getTypeDependencies();
if (dependencies != null) {
AnnotatedElement ae = element.getAnnotatedElement();
for (Class<? extends Annotation> annotationType : dependencies) {
Annotation depAnnotation = ae.getAnnotation(annotationType);
if (depAnnotation != null) {
ResultType resultType = result.processedAnnotations().get(annotationType);
if (resultType == null || resultType == ResultType.UNPROCESSED) {
// annotation is present, process it.
AnnotationInfo info = new AnnotationInfo(ctx, ae, depAnnotation, getTopElementType());
process(ctx, info, result);
}
}
}
}
// at this point, all annotation that I declared depending on
// are processed
HandlerProcessingResult processingResult = null;
try {
processingResult = handler.processAnnotation(element);
} catch (AnnotationProcessorException ape) {
// I am logging this exception
log(Level.SEVERE, ape.getLocator(), ape.getMessage());
// errors.
if (ape.isFatal()) {
throw ape;
}
if (++errorCount > 100) {
throw new AnnotationProcessorException(AnnotationUtils.getLocalString("enterprise.deployment.annotation.toomanyerror", "Too many errors, annotation processing abandoned."));
}
processingResult = HandlerProcessingResultImpl.getDefaultResult(annotation.annotationType(), ResultType.FAILED);
} catch (Throwable e) {
AnnotationProcessorException ape = new AnnotationProcessorException(e.getMessage(), element);
ape.initCause(e);
throw ape;
}
result.addAll(processingResult);
}
} else {
if (delegate != null) {
delegate.process(ctx, element, result);
} else {
ctx.getErrorHandler().fine(new AnnotationProcessorException("No handler defined for " + annotation.annotationType()));
}
}
}
use of org.glassfish.apf.AnnotationProcessorException in project Payara by payara.
the class AbstractEjbHandler method setBusinessAndHomeInterfaces.
/**
* MessageDriven bean does not need to invoke this API.
* @param ejbDesc
* @param ainfo for error handling
* @return HandlerProcessingResult
*/
protected HandlerProcessingResult setBusinessAndHomeInterfaces(EjbDescriptor ejbDesc, AnnotationInfo ainfo) throws AnnotationProcessorException {
Set<Class> localBusIntfs = new HashSet<Class>();
Set<Class> remoteBusIntfs = new HashSet<Class>();
Set<Class> clientInterfaces = new HashSet<Class>();
Class ejbClass = (Class) ainfo.getAnnotatedElement();
// First check for annotations specifying remote/local business intfs.
// We analyze them here because they are needed during the
// implements clause processing for beans that specify
// @Stateless/@Stateful. In addition, they should *not* be processed
// if there is no @Stateful/@Stateless annotation.
Remote remoteBusAnn = (Remote) ejbClass.getAnnotation(Remote.class);
boolean emptyRemoteBusAnn = false;
if (remoteBusAnn != null) {
for (Class next : remoteBusAnn.value()) {
if (next.getAnnotation(Local.class) != null) {
AnnotationProcessorException fatalException = new AnnotationProcessorException(localStrings.getLocalString("enterprise.deployment.annotation.handlers.invalidbusinessinterface", "The interface {0} cannot be both a local and a remote business interface.", new Object[] { next.getName() }));
fatalException.setFatal(true);
throw fatalException;
}
clientInterfaces.add(next);
remoteBusIntfs.add(next);
}
emptyRemoteBusAnn = remoteBusIntfs.isEmpty();
}
Local localBusAnn = (Local) ejbClass.getAnnotation(Local.class);
if (localBusAnn != null) {
for (Class next : localBusAnn.value()) {
if (next.getAnnotation(Remote.class) != null) {
AnnotationProcessorException fatalException = new AnnotationProcessorException(localStrings.getLocalString("enterprise.deployment.annotation.handlers.invalidbusinessinterface", "The interface {0} cannot be both a local and a remote business interface.", new Object[] { next.getName() }));
fatalException.setFatal(true);
throw fatalException;
}
clientInterfaces.add(next);
localBusIntfs.add(next);
}
}
List<Class> imlementingInterfaces = new ArrayList<Class>();
List<Class> implementedDesignatedInterfaces = new ArrayList<Class>();
for (Class next : ejbClass.getInterfaces()) {
if (!excludedFromImplementsClause(next)) {
if (next.getAnnotation(Local.class) != null || next.getAnnotation(Remote.class) != null) {
implementedDesignatedInterfaces.add(next);
}
imlementingInterfaces.add(next);
}
}
LocalBean localBeanAnn = (LocalBean) ejbClass.getAnnotation(LocalBean.class);
if (localBeanAnn != null) {
ejbDesc.setLocalBean(true);
}
// total number of local/remote business interfaces declared
// outside of the implements clause plus implemented designated interfaces
int designatedInterfaceCount = remoteBusIntfs.size() + localBusIntfs.size() + ejbDesc.getRemoteBusinessClassNames().size() + ejbDesc.getLocalBusinessClassNames().size() + implementedDesignatedInterfaces.size();
for (Class next : imlementingInterfaces) {
String nextIntfName = next.getName();
if (remoteBusIntfs.contains(next) || localBusIntfs.contains(next) || ejbDesc.getRemoteBusinessClassNames().contains(nextIntfName) || ejbDesc.getLocalBusinessClassNames().contains(nextIntfName)) {
// Interface has already been identified as a Remote/Local
// business interface, so ignore.
} else if (next.getAnnotation(Local.class) != null) {
clientInterfaces.add(next);
localBusIntfs.add(next);
} else if (next.getAnnotation(Remote.class) != null) {
clientInterfaces.add(next);
remoteBusIntfs.add(next);
} else {
if ((designatedInterfaceCount == 0) && (!ejbDesc.isLocalBean())) {
// it's treated as a local business interface.
if (emptyRemoteBusAnn) {
remoteBusIntfs.add(next);
} else {
localBusIntfs.add(next);
}
clientInterfaces.add(next);
} else {
// Since the component has at least one other business
// interface, each implements clause interface that cannot
// be identified as business interface via the deployment
// descriptor or a @Remote/@Local annotation is ignored.
}
}
}
for (Class next : clientInterfaces) {
if (remoteBusIntfs.contains(next) && localBusIntfs.contains(next)) {
AnnotationProcessorException fatalException = new AnnotationProcessorException(localStrings.getLocalString("enterprise.deployment.annotation.handlers.invalidbusinessinterface", "The interface {0} cannot be both a local and a remote business interface.", new Object[] { next.getName() }));
fatalException.setFatal(true);
throw fatalException;
}
}
if (localBusIntfs.size() > 0) {
for (Class next : localBusIntfs) {
ejbDesc.addLocalBusinessClassName(next.getName());
}
}
if (remoteBusIntfs.size() > 0) {
for (Class next : remoteBusIntfs) {
ejbDesc.addRemoteBusinessClassName(next.getName());
}
}
// Do Adapted @Home / Adapted @LocalHome processing here too since
// they are logically part of the structural @Stateless/@Stateful info.
RemoteHome remoteHomeAnn = (RemoteHome) ejbClass.getAnnotation(RemoteHome.class);
if (remoteHomeAnn != null) {
Class remoteHome = remoteHomeAnn.value();
Class remoteIntf = getComponentIntfFromHome(remoteHome);
if (EJBHome.class.isAssignableFrom(remoteHome) && (remoteIntf != null)) {
clientInterfaces.add(remoteHome);
ejbDesc.setHomeClassName(remoteHome.getName());
ejbDesc.setRemoteClassName(remoteIntf.getName());
} else {
log(Level.SEVERE, ainfo, localStrings.getLocalString("enterprise.deployment.annotation.handlers.invalidremotehome", "Encountered invalid @RemoteHome interface {0}.", new Object[] { remoteHome }));
return getDefaultFailedResult();
}
}
LocalHome localHomeAnn = (LocalHome) ejbClass.getAnnotation(LocalHome.class);
if (localHomeAnn != null) {
Class localHome = localHomeAnn.value();
Class localIntf = getComponentIntfFromHome(localHome);
if (EJBLocalHome.class.isAssignableFrom(localHome) && (localIntf != null)) {
clientInterfaces.add(localHome);
ejbDesc.setLocalHomeClassName(localHome.getName());
ejbDesc.setLocalClassName(localIntf.getName());
} else {
log(Level.SEVERE, ainfo, localStrings.getLocalString("enterprise.deployment.annotation.handlers.invalidlocalhome", "Encountered invalid @LocalHome interface {0}.", new Object[] { localHome }));
return getDefaultFailedResult();
}
}
// Web Service API might not be available so do a check before looking
// for @WebService on bean class
boolean canDoWebServiceAnnCheck = false;
try {
canDoWebServiceAnnCheck = (provider.getType("javax.jws.WebService") != null);
} catch (Exception e) {
log(Level.FINE, ainfo, e.getMessage());
}
if ((!ejbDesc.isLocalBean()) && (clientInterfaces.size() == 0) && !ejbDesc.hasWebServiceEndpointInterface() && (!canDoWebServiceAnnCheck || (ejbClass.getAnnotation(javax.jws.WebService.class) == null))) {
ejbDesc.setLocalBean(true);
}
// If this is a no-Interface local EJB, set all classes for this bean
if (ejbDesc.isLocalBean()) {
addNoInterfaceLocalBeanClasses(ejbDesc, ejbClass);
}
return getDefaultProcessedResult();
}
Aggregations