Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow variable shadowing to affect component plugin annotations @Dial… #54

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@

import javassist.CannotCompileException;
import javassist.CtMember;
import javassist.CtMethod;
import javassist.NotFoundException;

import org.codehaus.plexus.util.StringUtils;

import com.citytechinc.cq.component.annotations.DialogField;
import com.citytechinc.cq.component.annotations.IgnoreDialogField;
import com.citytechinc.cq.component.annotations.widgets.DialogFieldSet;
import com.citytechinc.cq.component.dialog.AbstractWidget;
Expand Down Expand Up @@ -92,17 +90,10 @@ private List<DialogElement> buildWidgetCollection(DialogFieldSet dialogFieldSetA
for (CtMember member : fieldsAndMethods) {
if (!member.hasAnnotation(IgnoreDialogField.class)) {
DialogFieldConfig dialogFieldConfig = null;
if (member instanceof CtMethod) {
try {
dialogFieldConfig = DialogUtil.getDialogFieldFromSuperClasses((CtMethod) member);
} catch (InvalidComponentClassException e) {
throw new InvalidComponentFieldException(e.getMessage(), e);
}
} else {
if (member.hasAnnotation(DialogField.class)) {
dialogFieldConfig =
new DialogFieldConfig((DialogField) member.getAnnotation(DialogField.class), member);
}
try {
dialogFieldConfig = DialogUtil.getDialogFieldFromSuperClasses(member);
} catch (InvalidComponentClassException e) {
throw new InvalidComponentFieldException(e.getMessage(), e);
}

if (dialogFieldConfig != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMember;
import javassist.CtMethod;
import javassist.NotFoundException;

import org.codehaus.plexus.util.StringUtils;

import com.citytechinc.cq.component.annotations.Component;
import com.citytechinc.cq.component.annotations.DialogField;
import com.citytechinc.cq.component.annotations.IgnoreDialogField;
import com.citytechinc.cq.component.annotations.Listener;
import com.citytechinc.cq.component.dialog.Dialog;
Expand Down Expand Up @@ -127,15 +125,7 @@ public static Dialog make(CtClass componentClass, WidgetRegistry widgetRegistry,
*/
for (CtMember member : fieldsAndMethods) {
if (!member.hasAnnotation(IgnoreDialogField.class)) {
DialogFieldConfig dialogFieldConfig = null;
if (member instanceof CtMethod) {
dialogFieldConfig = DialogUtil.getDialogFieldFromSuperClasses((CtMethod) member);
} else {
if (member.hasAnnotation(DialogField.class)) {
dialogFieldConfig =
new DialogFieldConfig((DialogField) member.getAnnotation(DialogField.class), member);
}
}
DialogFieldConfig dialogFieldConfig = DialogUtil.getDialogFieldFromSuperClasses(member);

if (dialogFieldConfig != null) {
WidgetMakerParameters parameters =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtField;
import javassist.CtMember;
import javassist.CtMethod;
import javassist.NotFoundException;
Expand Down Expand Up @@ -196,42 +197,79 @@ private static CtMember getMemberForAnnotatedInterfaceMethod(CtMethod member)
return newMember;
}

/**
* This method is kept for backwards compatibility. (i.e. MultiCompositeField)
*
* @param method the method with dialog annotation
* @return a dialog field configuration
* @throws NotFoundException
* @throws ClassNotFoundException
* @throws InvalidComponentClassException
*/
public static DialogFieldConfig getDialogFieldFromSuperClasses(CtMethod method) throws NotFoundException,
ClassNotFoundException, InvalidComponentClassException {
return getDialogFieldFromSuperClasses((CtMember) method);
}

public static DialogFieldConfig getDialogFieldFromSuperClasses(CtMember member) throws NotFoundException,
ClassNotFoundException, InvalidComponentClassException {
DialogFieldConfig dialogFieldConfig = null;
List<CtClass> classes = new ArrayList<CtClass>();
CtClass clazz = method.getDeclaringClass();
CtClass clazz = member.getDeclaringClass();
classes.add(clazz);
while (clazz.getSuperclass() != null) {
classes.add(clazz.getSuperclass());
clazz = clazz.getSuperclass();
}
Collections.reverse(classes);
CtMember interfaceMember = getMemberForAnnotatedInterfaceMethod(method);
if (interfaceMember != null) {
dialogFieldConfig =
new DialogFieldConfig((DialogField) interfaceMember.getAnnotation(DialogField.class), interfaceMember);
}
for (CtClass ctclass : classes) {
try {
CtMethod superClassMethod = ctclass.getDeclaredMethod(method.getName(), method.getParameterTypes());
if (superClassMethod.hasAnnotation(DialogField.class)) {
dialogFieldConfig =
new DialogFieldConfig((DialogField) superClassMethod.getAnnotation(DialogField.class),
superClassMethod);
} else if (superClassMethod.hasAnnotation(DialogFieldOverride.class)) {
mergeDialogFields(dialogFieldConfig, superClassMethod);

if (member instanceof CtMethod) {
CtMember interfaceMember = getMemberForAnnotatedInterfaceMethod((CtMethod) member);
if (interfaceMember != null) {
dialogFieldConfig =
new DialogFieldConfig((DialogField) interfaceMember.getAnnotation(DialogField.class),
interfaceMember);
}
for (CtClass ctClass : classes) {
try {
CtMethod superClassMethod = ctClass
.getDeclaredMethod(member.getName(), ((CtMethod) member).getParameterTypes());
if (superClassMethod.hasAnnotation(DialogField.class)) {
dialogFieldConfig =
new DialogFieldConfig((DialogField) superClassMethod.getAnnotation(DialogField.class),
superClassMethod);
} else if (superClassMethod.hasAnnotation(DialogFieldOverride.class)) {
mergeDialogFields(dialogFieldConfig, superClassMethod);
}
} catch (NotFoundException e) {
}
} catch (NotFoundException e) {
}
} else if (member instanceof CtField) {
for (CtClass ctClass : classes) {
try {
CtField superClassField = ctClass
.getDeclaredField(member.getName());

if (superClassField.hasAnnotation(DialogField.class)) {
dialogFieldConfig = new DialogFieldConfig(
(DialogField) superClassField.getAnnotation(DialogField.class), superClassField);
} else if (superClassField.hasAnnotation(DialogFieldOverride.class)) {
mergeDialogFields(dialogFieldConfig, superClassField);
}
} catch (NotFoundException e) {
}
}
} else {
dialogFieldConfig = new DialogFieldConfig((DialogField) member.getAnnotation(DialogField.class), member);
}

return dialogFieldConfig;
}

private static void mergeDialogFields(DialogFieldConfig dialogFieldConfig, CtMethod method)
private static void mergeDialogFields(DialogFieldConfig dialogFieldConfig, CtMember member)
throws ClassNotFoundException {
if (dialogFieldConfig != null && method.hasAnnotation(DialogFieldOverride.class)) {
DialogFieldOverride dialogField = (DialogFieldOverride) method.getAnnotation(DialogFieldOverride.class);
if (dialogFieldConfig != null && member.hasAnnotation(DialogFieldOverride.class)) {
DialogFieldOverride dialogField = (DialogFieldOverride) member.getAnnotation(DialogFieldOverride.class);
if (StringUtils.isNotEmpty(dialogField.fieldLabel())) {
dialogFieldConfig.setFieldLabel(dialogField.fieldLabel());
}
Expand Down Expand Up @@ -305,15 +343,7 @@ private static boolean isWidgetInComponentClass(CtClass componentClass) throws N
*/
for (CtMember member : fieldsAndMethods) {
if (!member.hasAnnotation(IgnoreDialogField.class)) {
DialogFieldConfig dialogFieldConfig = null;
if (member instanceof CtMethod) {
dialogFieldConfig = DialogUtil.getDialogFieldFromSuperClasses((CtMethod) member);
} else {
if (member.hasAnnotation(DialogField.class)) {
dialogFieldConfig =
new DialogFieldConfig((DialogField) member.getAnnotation(DialogField.class), member);
}
}
DialogFieldConfig dialogFieldConfig = DialogUtil.getDialogFieldFromSuperClasses(member);;

if (dialogFieldConfig != null) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,17 +544,23 @@ public static List<CtClass> getAllComponentAnnotations(ClassPool classPool, Refl
/**
* Constructs a list of all fields contained in the provided CtClass and any
* of its parent classes.
*
*
* @param ctClass
* @return The constructed list of fields
* @throws NotFoundException
*/
public static List<CtField> collectFields(CtClass ctClass, boolean suppressInheritedFields) throws NotFoundException {
public static List<CtField> collectFields(CtClass ctClass, boolean suppressInheritedFields)
throws NotFoundException {

List<CtField> fields = new ArrayList<CtField>();
if (ctClass != null) {
fields.addAll(Arrays.asList(ctClass.getDeclaredFields()));
if (!suppressInheritedFields) {
fields.addAll(collectFields(ctClass.getSuperclass()));
List<CtField> superFields = collectFields(ctClass.getSuperclass());

for (final CtField superField : superFields) {
addField(superField, fields);
}
}
}
return fields;
Expand Down Expand Up @@ -678,4 +684,25 @@ public static File writeElementToFile(ComponentNameTransformer transformer, Abst
return file;
}

/**
* Add field to target field list only if it does not already exist.
*
* @param sourceField the field to add
* @param targetFields the list to add to
*/
private static void addField(CtField sourceField, List<CtField> targetFields) {

boolean fieldExists = false;
for (CtField targetField : targetFields) {
if (StringUtils.equals(targetField.getName(), sourceField.getName())) {
fieldExists = true;
break;
}
}

if (!fieldExists) {
targetFields.add(sourceField);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMember;
import javassist.CtMethod;
import javassist.NotFoundException;

import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.codehaus.plexus.util.StringUtils;

import com.citytechinc.cq.component.annotations.Component;
import com.citytechinc.cq.component.annotations.DialogField;
import com.citytechinc.cq.component.annotations.IgnoreDialogField;
import com.citytechinc.cq.component.annotations.widgets.Selection;
import com.citytechinc.cq.component.dialog.ComponentNameTransformer;
Expand Down Expand Up @@ -123,15 +121,7 @@ public static List<TouchUIWidgetMakerParameters> getWidgetMakerParametersForComp
// preparing the necessary widget maker parameters
for (CtMember member : fieldsAndMethods) {
if (!member.hasAnnotation(IgnoreDialogField.class)) {
DialogFieldConfig dialogFieldConfig = null;
if (member instanceof CtMethod) {
dialogFieldConfig = DialogUtil.getDialogFieldFromSuperClasses((CtMethod) member);
} else {
if (member.hasAnnotation(DialogField.class)) {
dialogFieldConfig =
new DialogFieldConfig((DialogField) member.getAnnotation(DialogField.class), member);
}
}
DialogFieldConfig dialogFieldConfig = DialogUtil.getDialogFieldFromSuperClasses(member);

if (dialogFieldConfig != null && !dialogFieldConfig.isSuppressTouchUI()) {
TouchUIWidgetMakerParameters touchUIWidgetMakerParameters = new TouchUIWidgetMakerParameters();
Expand Down Expand Up @@ -164,15 +154,7 @@ public static boolean isWidgetInComponentClass(CtClass componentClass) throws No
// preparing the necessary widget maker parameters
for (CtMember member : fieldsAndMethods) {
if (!member.hasAnnotation(IgnoreDialogField.class)) {
DialogFieldConfig dialogFieldConfig = null;
if (member instanceof CtMethod) {
dialogFieldConfig = DialogUtil.getDialogFieldFromSuperClasses((CtMethod) member);
} else {
if (member.hasAnnotation(DialogField.class)) {
dialogFieldConfig =
new DialogFieldConfig((DialogField) member.getAnnotation(DialogField.class), member);
}
}
DialogFieldConfig dialogFieldConfig = DialogUtil.getDialogFieldFromSuperClasses(member);;

if (dialogFieldConfig != null && !dialogFieldConfig.isSuppressTouchUI()) {
return true;
Expand All @@ -181,7 +163,6 @@ public static boolean isWidgetInComponentClass(CtClass componentClass) throws No
}

return false;

}

public static final List<com.citytechinc.cq.component.touchuidialog.widget.selection.options.Option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.citytechinc.cq.component.touchuidialog.widget.dialogfieldset;

import com.citytechinc.cq.component.annotations.DialogField;
import com.citytechinc.cq.component.annotations.IgnoreDialogField;
import com.citytechinc.cq.component.annotations.widgets.DialogFieldSet;
import com.citytechinc.cq.component.dialog.DialogFieldConfig;
Expand All @@ -30,7 +29,6 @@
import com.citytechinc.cq.component.touchuidialog.widget.maker.AbstractTouchUIWidgetMaker;
import com.citytechinc.cq.component.touchuidialog.widget.maker.TouchUIWidgetMakerParameters;
import javassist.CtMember;
import javassist.CtMethod;
import javassist.NotFoundException;
import org.codehaus.plexus.util.StringUtils;

Expand Down Expand Up @@ -91,18 +89,11 @@ private List<TouchUIDialogElement> buildLayoutItems(DialogFieldSet dialogFieldSe
if (!member.hasAnnotation(IgnoreDialogField.class)) {
DialogFieldConfig dialogFieldConfig = null;

if (member instanceof CtMethod) {
try {
dialogFieldConfig = DialogUtil.getDialogFieldFromSuperClasses((CtMethod) member);
} catch (InvalidComponentClassException e) {
throw new InvalidComponentFieldException(e.getMessage(), e);
}
} else {
if (member.hasAnnotation(DialogField.class)) {
dialogFieldConfig =
new DialogFieldConfig((DialogField) member.getAnnotation(DialogField.class), member);
}
}
try {
dialogFieldConfig = DialogUtil.getDialogFieldFromSuperClasses(member);
} catch (InvalidComponentClassException e) {
throw new InvalidComponentFieldException(e.getMessage(), e);
}

if (dialogFieldConfig != null && !dialogFieldConfig.isSuppressTouchUI()) {
if (StringUtils.isNotBlank(dialogFieldSetAnnotation.namePrefix())) {
Expand Down