private void validateSortedFields(List<PlasticField> sortedFields) { List<Integer> expectedIndexes = CollectionFactory.newList(); List<Integer> actualIndexes = CollectionFactory.newList(); Set<Boolean> activates = CollectionFactory.newSet(); Set<Boolean> passivates = CollectionFactory.newSet(); for (int i = 0; i < sortedFields.size(); ++i) { PlasticField field = sortedFields.get(i); PageActivationContext annotation = field.getAnnotation(PageActivationContext.class); expectedIndexes.add(i); actualIndexes.add(annotation.index()); activates.add(annotation.activate()); passivates.add(annotation.passivate()); } List<String> errors = CollectionFactory.newList(); if (!expectedIndexes.equals(actualIndexes)) { errors.add(String.format("Index values must start at 0 and increment by 1 (expected [%s], found [%s])", InternalUtils.join(expectedIndexes), InternalUtils.join(actualIndexes))); } if (activates.size() > 1) { errors.add("Illegal values for 'activate' (all fields must have the same value)"); } if (passivates.size() > 1) { errors.add("Illegal values for 'passivate' (all fields must have the same value)"); } if (!errors.isEmpty()) { throw new RuntimeException(String.format("Invalid values for @PageActivationContext: %s", InternalUtils.join(errors))); } }
private void transformFields(TransformationSupport support, List<PlasticField> fields) { List<PlasticField> sortedFields = CollectionFactory.newList(fields); Collections.sort(sortedFields, INDEX_COMPARATOR); validateSortedFields(sortedFields); PlasticField firstField = sortedFields.get(0); PageActivationContext firstAnnotation = firstField.getAnnotation(PageActivationContext.class); // these arrays reduce memory usage and allow the PlasticField instances to be garbage collected FieldHandle[] handles = new FieldHandle[sortedFields.size()]; String[] typeNames = new String[sortedFields.size()]; int i = 0; for (PlasticField field : sortedFields) { handles[i] = field.getHandle(); typeNames[i] = field.getTypeName(); ++i; } if (firstAnnotation.activate()) { support.addEventHandler(EventConstants.ACTIVATE, 1, "PageActivationContextWorker activate event handler", createActivationHandler(handles, typeNames)); } if (firstAnnotation.passivate()) { support.addEventHandler(EventConstants.PASSIVATE, 0, "PageActivationContextWorker passivate event handler", createPassivateHandler(handles)); } // We don't claim the field, and other workers may even replace it with a FieldConduit. }