Skip to content

Commit

Permalink
web.beans: Simplify/consolidate annotation analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasblaesing committed Dec 21, 2024
1 parent 1e3bbde commit d31d18a
Show file tree
Hide file tree
Showing 26 changed files with 451 additions and 411 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,18 @@
import org.netbeans.api.java.source.CompilationInfo;
import org.netbeans.modules.web.beans.analysis.CdiAnalysisResult;

import static org.netbeans.modules.web.beans.analysis.analyzer.AnnotationUtil.SPECIALIZES;
import static org.netbeans.modules.web.beans.analysis.analyzer.AnnotationUtil.SPECIALIZES_JAKARTA;
import static org.netbeans.modules.web.beans.analysis.analyzer.AnnotationUtil.TYPED;
import static org.netbeans.modules.web.beans.analysis.analyzer.AnnotationUtil.TYPED_JAKARTA;

/**
* @author ads
*
*/
public abstract class AbstractTypedAnalyzer {
public void analyze( Element element, TypeMirror elementType,

public void analyze( Element element, TypeMirror elementType,
AtomicBoolean cancel , CdiAnalysisResult result )
{
CompilationInfo compInfo = result.getInfo();
Expand All @@ -67,27 +71,26 @@ public void analyze( Element element, TypeMirror elementType,
if ( cancel.get()){
return;
}
if (AnnotationUtil.hasAnnotation(element, AnnotationUtil.SPECIALIZES, compInfo)
|| AnnotationUtil.hasAnnotation(element, AnnotationUtil.SPECIALIZES_JAKARTA, compInfo))
if (AnnotationUtil.hasAnnotation(element, compInfo, SPECIALIZES_JAKARTA, SPECIALIZES))
{
result.requireCdiEnabled(element);
checkSpecializes( element , elementType , list, cancel , result );
}
}

protected abstract void checkSpecializes( Element element, TypeMirror elementType,
List<TypeMirror> restrictedTypes, AtomicBoolean cancel , CdiAnalysisResult result );

protected boolean hasBeanType( Element subject, TypeMirror elementType,
protected boolean hasBeanType( Element subject, TypeMirror elementType,
TypeMirror requiredBeanType,CompilationInfo compInfo )
{
return compInfo.getTypes().isSubtype(elementType, requiredBeanType);
}
protected abstract void addError ( Element element ,

protected abstract void addError ( Element element ,
CdiAnalysisResult result );

protected void collectAncestors(TypeElement type , Set<TypeElement> ancestors,
protected void collectAncestors(TypeElement type , Set<TypeElement> ancestors,
CompilationInfo compInfo )
{
TypeMirror superclass = type.getSuperclass();
Expand All @@ -97,7 +100,7 @@ protected void collectAncestors(TypeElement type , Set<TypeElement> ancestors,
addAncestor(interfaze, ancestors, compInfo);
}
}

private void addAncestor( TypeMirror parent , Set<TypeElement> ancestors,
CompilationInfo compInfo)
{
Expand All @@ -114,26 +117,24 @@ private void addAncestor( TypeMirror parent , Set<TypeElement> ancestors,
collectAncestors((TypeElement)parentElement, ancestors, compInfo);
}
}
protected List<TypeMirror> getRestrictedTypes(Element element,

protected List<TypeMirror> getRestrictedTypes(Element element,
CompilationInfo compInfo , AtomicBoolean cancel)
{
AnnotationMirror typedMirror = AnnotationUtil.getAnnotationMirror(element, AnnotationUtil.TYPED_JAKARTA, compInfo);
if (typedMirror == null) {
typedMirror = AnnotationUtil.getAnnotationMirror(element, AnnotationUtil.TYPED, compInfo);
}
AnnotationMirror typedMirror = AnnotationUtil.getAnnotationMirror(
element, compInfo, TYPED_JAKARTA, TYPED);
if ( typedMirror == null ){
return null;
}
Map<? extends ExecutableElement, ? extends AnnotationValue> values =
Map<? extends ExecutableElement, ? extends AnnotationValue> values =
typedMirror.getElementValues();
AnnotationValue restrictedTypes = null;
for (Entry<? extends ExecutableElement, ? extends AnnotationValue> entry :
values.entrySet() )
for (Entry<? extends ExecutableElement, ? extends AnnotationValue> entry :
values.entrySet() )
{
ExecutableElement key = entry.getKey();
AnnotationValue value = entry.getValue();
if ( AnnotationUtil.VALUE.contentEquals(key.getSimpleName())){
if ( AnnotationUtil.VALUE.contentEquals(key.getSimpleName())){
restrictedTypes = value;
break;
}
Expand All @@ -146,7 +147,7 @@ protected List<TypeMirror> getRestrictedTypes(Element element,
}
Object value = restrictedTypes.getValue();
if ( value instanceof List<?> ){
List<TypeMirror> result = new ArrayList<TypeMirror>(((List<?>)value).size());
List<TypeMirror> result = new ArrayList<>(((List<?>)value).size());
for( Object type : (List<?>)value){
AnnotationValue annotationValue = (AnnotationValue)type;
type = annotationValue.getValue();
Expand All @@ -158,20 +159,20 @@ protected List<TypeMirror> getRestrictedTypes(Element element,
}
return Collections.emptyList();
}

protected Set<TypeElement> getUnrestrictedBeanTypes( TypeElement element,
CompilationInfo compInfo)
{
Set<TypeElement> set = new HashSet<TypeElement>();
Set<TypeElement> set = new HashSet<>();
set.add( element );
collectAncestors(element, set, compInfo);
return set;
}

protected Set<TypeElement> getElements( Collection<TypeMirror> types ,
CompilationInfo info )
{
Set<TypeElement> result = new HashSet<TypeElement>();
Set<TypeElement> result = new HashSet<>();
for (TypeMirror typeMirror : types) {
Element element = info.getTypes().asElement(typeMirror);
if ( element instanceof TypeElement ){
Expand All @@ -180,5 +181,5 @@ protected Set<TypeElement> getElements( Collection<TypeMirror> types ,
}
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -173,29 +173,42 @@ public final class AnnotationUtil {
private AnnotationUtil(){
}

public static boolean hasAnnotation(Element element, String annotation,
CompilationInfo info )
/**
* @param element
* @param info
* @param annotationFqns
* @return true if at least one annotation from {@code annotationFqns} is
* present
*/
public static boolean hasAnnotation(Element element,
CompilationInfo info, String... annotationFqns )
{
return getAnnotationMirror(element, annotation, info)!=null;
return getAnnotationMirror(element, info, annotationFqns) != null;
}

public static AnnotationMirror getAnnotationMirror(Element element,
String annotation,CompilationInfo info )
/**
* @param element
* @param model
* @param annotationFqns
* @return true if at least one annotation from {@code annotationFqns} is
* present
*/
public static boolean hasAnnotation(Element element,
WebBeansModel model, String... annotationFqns)
{
return getAnnotationMirror(element, info, annotation);
return hasAnnotation(element, model.getCompilationController(), annotationFqns);
}

/**
* return AnnotationMirror for first found annotation from annotationFqns
* @param element
* @param info
* @param annotationFqns
* @return
* @return AnnotationMirror for first found annotation from annotationFqns
*/
public static AnnotationMirror getAnnotationMirror(Element element,
CompilationInfo info , String... annotationFqns)
{
Set<TypeElement> set = new HashSet<TypeElement>();
Set<TypeElement> set = new HashSet<>();
Elements els = info.getElements();
for( String annotation : annotationFqns){
TypeElement annotationElement = els.getTypeElement(
Expand All @@ -220,26 +233,25 @@ public static AnnotationMirror getAnnotationMirror(Element element,
public static boolean isSessionBean(Element element ,
CompilationInfo compInfo )
{
return getAnnotationMirror(element, compInfo, STATEFUL, STATELESS, SINGLETON, STATEFUL_JAKARTA, STATELESS_JAKARTA, SINGLETON_JAKARTA) != null;
return hasAnnotation(element, compInfo, STATEFUL, STATELESS, SINGLETON, STATEFUL_JAKARTA, STATELESS_JAKARTA, SINGLETON_JAKARTA);
}

public static boolean isDelegate(Element element, TypeElement parent,
WebBeansModel model )
{
return (AnnotationUtil.hasAnnotation(element, AnnotationUtil.DELEGATE_FQN, model.getCompilationController())
&& AnnotationUtil.hasAnnotation(parent, AnnotationUtil.DECORATOR, model.getCompilationController()))
|| (AnnotationUtil.hasAnnotation(element, AnnotationUtil.DELEGATE_FQN_JAKARTA, model.getCompilationController())
&& AnnotationUtil.hasAnnotation(parent, AnnotationUtil.DECORATOR_JAKARTA, model.getCompilationController()));
return (AnnotationUtil.hasAnnotation(element, model, DELEGATE_FQN)
&& AnnotationUtil.hasAnnotation(parent, model, DECORATOR))
|| (AnnotationUtil.hasAnnotation(element, model, DELEGATE_FQN_JAKARTA)
&& AnnotationUtil.hasAnnotation(parent, model, DECORATOR_JAKARTA));
}

public static boolean isLifecycleCallback( ExecutableElement element ,
CompilationInfo info )
{
AnnotationMirror annotationMirror = getAnnotationMirror(element, info,
return hasAnnotation(element, info,
POST_ACTIVATE, POST_CONSTRUCT, PRE_DESTROY, PRE_PASSIVATE,
POST_ACTIVATE_JAKARTA, POST_CONSTRUCT_JAKARTA, PRE_DESTROY_JAKARTA, PRE_PASSIVATE_JAKARTA
);
return annotationMirror != null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
import org.netbeans.modules.web.beans.analysis.CdiAnalysisResult;
import org.openide.util.NbBundle;

import static org.netbeans.modules.web.beans.analysis.analyzer.AnnotationUtil.DISPOSES_FQN;
import static org.netbeans.modules.web.beans.analysis.analyzer.AnnotationUtil.DISPOSES_FQN_JAKARTA;
import static org.netbeans.modules.web.beans.analysis.analyzer.AnnotationUtil.OBSERVES_FQN;
import static org.netbeans.modules.web.beans.analysis.analyzer.AnnotationUtil.OBSERVES_FQN_JAKARTA;

/**
* @author ads
Expand All @@ -49,16 +53,14 @@ public void analyze( Element element, TypeElement parent,
if ( cancel.get() ){
return;
}
boolean isDisposer = AnnotationUtil.hasAnnotation(param, AnnotationUtil.DISPOSES_FQN, result.getInfo())
|| AnnotationUtil.hasAnnotation(param, AnnotationUtil.DISPOSES_FQN_JAKARTA, result.getInfo());
boolean isObserver = AnnotationUtil.hasAnnotation(param, AnnotationUtil.OBSERVES_FQN, result.getInfo())
|| AnnotationUtil.hasAnnotation(param, AnnotationUtil.OBSERVES_FQN_JAKARTA, result.getInfo());
boolean isDisposer = AnnotationUtil.hasAnnotation(param, result.getInfo(), DISPOSES_FQN_JAKARTA, DISPOSES_FQN);
boolean isObserver = AnnotationUtil.hasAnnotation(param, result.getInfo(), OBSERVES_FQN_JAKARTA, OBSERVES_FQN);
if ( isDisposer || isObserver ){
result.requireCdiEnabled(element);
String annotation = isDisposer ? AnnotationUtil.DISPOSES :
String annotation = isDisposer ? AnnotationUtil.DISPOSES :
AnnotationUtil.OBSERVES;
result.addError( element, NbBundle.getMessage(
CtorAnalyzer.class, "ERR_BadAnnotationParamCtor", annotation)); // NOI18N
CtorAnalyzer.class, "ERR_BadAnnotationParamCtor", annotation)); // NOI18N
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@
import org.netbeans.modules.web.beans.api.model.WebBeansModel;
import org.openide.util.NbBundle;

import static org.netbeans.modules.web.beans.analysis.analyzer.AnnotationUtil.INTERCEPTOR_BINDING_FQN;
import static org.netbeans.modules.web.beans.analysis.analyzer.AnnotationUtil.INTERCEPTOR_BINDING_FQN_JAKARTA;


/**
* @author ads
*
*/
public class InterceptorBindingAnalyzer implements AnnotationAnalyzer {

/* (non-Javadoc)
* @see org.netbeans.modules.web.beans.analysis.analyzer.AnnotationModelAnalyzer.AnnotationAnalyzer#analyze(javax.lang.model.element.TypeElement, org.netbeans.modules.web.beans.api.model.WebBeansModel, java.util.List, org.netbeans.api.java.source.CompilationInfo, java.util.concurrent.atomic.AtomicBoolean)
*/
Expand All @@ -49,8 +52,7 @@ public void analyze( TypeElement element, WebBeansModel model,
AtomicBoolean cancel ,
Result result )
{
if (!(AnnotationUtil.hasAnnotation(element, AnnotationUtil.INTERCEPTOR_BINDING_FQN, model.getCompilationController())
|| AnnotationUtil.hasAnnotation(element, AnnotationUtil.INTERCEPTOR_BINDING_FQN_JAKARTA, model.getCompilationController())))
if (!AnnotationUtil.hasAnnotation(element, model, INTERCEPTOR_BINDING_FQN_JAKARTA, INTERCEPTOR_BINDING_FQN))
{
return;
}
Expand All @@ -61,15 +63,15 @@ public void analyze( TypeElement element, WebBeansModel model,
return;
}
if (!analyzer.hasRuntimeRetention()) {
result.addError(element, model,
result.addError(element, model,
NbBundle.getMessage(InterceptorBindingAnalyzer.class,
INCORRECT_RUNTIME));
}
if ( cancel.get() ){
return;
}
if (!analyzer.hasTarget()) {
result.addError(element, model,
result.addError(element, model,
NbBundle.getMessage(InterceptorBindingAnalyzer.class,
"ERR_IncorrectInterceptorBindingTarget")); // NOI18N
}
Expand All @@ -81,11 +83,11 @@ public void analyze( TypeElement element, WebBeansModel model,
if ( cancel.get() ){
return;
}
checkTransitiveInterceptorBindings( element, declaredTargetTypes,
checkTransitiveInterceptorBindings( element, declaredTargetTypes,
model , result );
}
}

private void checkTransitiveInterceptorBindings( TypeElement element,
Set<ElementType> declaredTargetTypes, WebBeansModel model,
Result result )
Expand All @@ -108,7 +110,7 @@ private void checkTransitiveInterceptorBindings( TypeElement element,
if (bindingTargetTypes.size() == 1
&& bindingTargetTypes.contains(ElementType.TYPE))
{
result.addError(element, model ,
result.addError(element, model ,
NbBundle.getMessage(InterceptorBindingAnalyzer.class,
"ERR_IncorrectTransitiveInterceptorBinding", // NOI18N
((TypeElement) binding).getQualifiedName().toString()));
Expand All @@ -118,7 +120,7 @@ private void checkTransitiveInterceptorBindings( TypeElement element,
}

private static class InterceptorTargetAnalyzer extends CdiAnnotationAnalyzer {

InterceptorTargetAnalyzer( TypeElement element , WebBeansModel model ,
Result result)
{
Expand All @@ -140,7 +142,7 @@ protected String getCdiMetaAnnotation() {
protected TargetVerifier getTargetVerifier() {
return InterceptorBindingVerifier.getInstance();
}

}

}
Loading

0 comments on commit d31d18a

Please sign in to comment.