use of org.springframework.roo.classpath.details.ItdTypeDetails in project spring-roo by spring-projects.
the class SerializableMetadataTest method assertItdContents.
/**
* Asserts that the ITD has the expected contents when the governor does or
* does not contain the required members
*
* @param alreadySerializable
* @param alreadyHasVersionField
*/
private void assertItdContents(final boolean alreadySerializable, final boolean alreadyHasVersionField) {
// Set up
when(mockClassDetails.implementsType(JdkJavaType.SERIALIZABLE)).thenReturn(alreadySerializable);
when(mockClassDetails.declaresField(SerializableMetadata.SERIAL_VERSION_FIELD)).thenReturn(alreadyHasVersionField);
final SerializableMetadata metadata = new SerializableMetadata(METADATA_ID, mockAspectName, mockGovernor);
// Invoke
final ItdTypeDetails itd = metadata.getItdTypeDetails();
// Check
assertEquals(alreadySerializable ? 0 : 1, itd.getImplementsTypes().size());
assertEquals(alreadyHasVersionField ? 0 : 1, itd.getDeclaredFields().size());
}
use of org.springframework.roo.classpath.details.ItdTypeDetails in project spring-roo by spring-projects.
the class AbstractItdMetadataProvider method get.
public final MetadataItem get(final String metadataIdentificationString) {
Validate.isTrue(MetadataIdentificationUtils.getMetadataClass(metadataIdentificationString).equals(MetadataIdentificationUtils.getMetadataClass(getProvidesType())), "Unexpected request for '%s' to this provider (which uses '%s')", metadataIdentificationString, getProvidesType());
// Remove the upstream dependencies for this instance (we'll be
// recreating them later, if needed)
getMetadataDependencyRegistry().deregisterDependencies(metadataIdentificationString);
// Compute the identifier for the Physical Type Metadata we're
// correlated with
final String governorPhysicalTypeIdentifier = getGovernorPhysicalTypeIdentifier(metadataIdentificationString);
// Obtain the physical type
final PhysicalTypeMetadata governorPhysicalTypeMetadata = (PhysicalTypeMetadata) getMetadataService().get(governorPhysicalTypeIdentifier);
if (governorPhysicalTypeMetadata == null || !governorPhysicalTypeMetadata.isValid()) {
// abort (the ITD will be deleted by ItdFileDeletionService)
return null;
}
// Flag to indicate whether we'll even try to create this metadata
boolean produceMetadata = false;
// Determine if we should generate the metadata on the basis of it
// containing a trigger annotation
final ClassOrInterfaceTypeDetails cid = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails();
if (cid != null) {
// metadata triggers
for (final JavaType trigger : metadataTriggers) {
if (cid.getAnnotation(trigger) != null) {
produceMetadata = true;
break;
}
}
}
// Fall back to ignoring trigger annotations
if (ignoreTriggerAnnotations) {
produceMetadata = true;
}
// aren't available
if (dependsOnGovernorTypeDetailAvailability && cid == null) {
produceMetadata = false;
}
// only wants to know about classes
if (cid != null && dependsOnGovernorBeingAClass && cid.getPhysicalTypeCategory() != PhysicalTypeCategory.CLASS) {
produceMetadata = false;
}
final String itdFilename = governorPhysicalTypeMetadata.getItdCanonicalPath(this);
if (!produceMetadata && isGovernor(cid) && getFileManager().exists(itdFilename)) {
// We don't seem to want metadata anymore, yet the ITD physically
// exists, so get rid of it
// This might be because the trigger annotation has been removed,
// the governor is missing a class declaration, etc.
deleteItd(metadataIdentificationString, itdFilename, "not required for governor " + cid.getName(), true);
return null;
}
if (produceMetadata) {
// This type contains an annotation we were configured to detect, or
// there is an ITD (which may need deletion), so we need to produce
// the metadata
final JavaType aspectName = governorPhysicalTypeMetadata.getItdJavaType(this);
final ItdTypeDetailsProvidingMetadataItem metadata = getMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, itdFilename);
if (metadata == null || !metadata.isValid()) {
// The metadata couldn't be created properly
deleteItd(metadataIdentificationString, itdFilename, "", false);
return null;
}
// By this point we have a valid MetadataItem, but it might not
// contain any members for the resulting ITD etc
// Handle the management of the ITD file
boolean deleteItdFile = false;
final ItdTypeDetails itdTypeDetails = metadata.getMemberHoldingTypeDetails();
if (itdTypeDetails == null) {
// The ITD has no members
deleteItdFile = true;
}
if (!deleteItdFile) {
// We have some members in the ITD, so decide if we're to write
// something to disk
final ItdSourceFileComposer itdSourceFileComposer = new ItdSourceFileComposer(metadata.getMemberHoldingTypeDetails());
// is physical content to write
if (itdSourceFileComposer.isContent()) {
// We have content to write
getItdDiscoveryService().addItdTypeDetails(itdTypeDetails);
final String itd = itdSourceFileComposer.getOutput();
getFileManager().createOrUpdateTextFileIfRequired(itdFilename, itd, false);
} else {
// We don't have content to write
deleteItdFile = true;
}
}
if (deleteItdFile) {
deleteItd(metadataIdentificationString, itdFilename, null, false);
}
// Eagerly notify that the metadata has been updated; this also
// registers the metadata hash code in the superclass' cache to
// avoid
// unnecessary subsequent notifications if it hasn't changed
notifyIfRequired(metadata);
return metadata;
}
return null;
}
use of org.springframework.roo.classpath.details.ItdTypeDetails in project spring-roo by spring-projects.
the class AbstractMemberDiscoveringItdMetadataProvider method notifyForGenericListener.
/**
* Receives generic notification events arising from our calling of
* MetadataDependencyRegistry.addNotificationListener(this). You must still
* register in the activate method to receive these events, as described in
* the JavaDocs for the superclass method of the same name.
*
* @see AbstractItdMetadataProvider#notifyForGenericListener(String)
*/
@Override
protected final void notifyForGenericListener(final String upstreamDependency) {
if (MetadataIdentificationUtils.isIdentifyingClass(upstreamDependency)) {
// don't care about it
return;
}
// We have an instance-specific identifier; try to get its metadata
final MetadataItem metadata = getMetadataService().get(upstreamDependency);
// the relevant .java once the DOD governor is first detected
if (!(metadata instanceof ItdTypeDetailsProvidingMetadataItem) || !metadata.isValid()) {
// It's not for an ITD, or there's something wrong with it
return;
}
// Get the details of the ITD
final ItdTypeDetails itdTypeDetails = ((ItdTypeDetailsProvidingMetadataItem) metadata).getMemberHoldingTypeDetails();
if (itdTypeDetails == null) {
return;
}
// Ask the subclass if they'd like us to request a MetadataItem in
// response
final String localMid = getLocalMidToRequest(itdTypeDetails);
if (localMid != null) {
Validate.isTrue(MetadataIdentificationUtils.isIdentifyingInstance(localMid), "Metadata identification string '%s' should identify a specific instance to request", localMid);
Validate.isTrue(MetadataIdentificationUtils.getMetadataClass(localMid).equals(MetadataIdentificationUtils.getMetadataClass(getProvidesType())), "Metadata identification string '%s' is incompatible with this metadata provider's class '%s'", MetadataIdentificationUtils.getMetadataClass(localMid), MetadataIdentificationUtils.getMetadataClass(getProvidesType()));
getMetadataService().evictAndGet(localMid);
}
}
use of org.springframework.roo.classpath.details.ItdTypeDetails in project spring-roo by spring-projects.
the class JspMetadataListener method notify.
public void notify(final String upstreamDependency, String downstreamDependency) {
if (MetadataIdentificationUtils.isIdentifyingClass(downstreamDependency)) {
// been
if (WebScaffoldMetadata.isValid(upstreamDependency)) {
final JavaType javaType = WebScaffoldMetadata.getJavaType(upstreamDependency);
final LogicalPath path = WebScaffoldMetadata.getPath(upstreamDependency);
downstreamDependency = JspMetadata.createIdentifier(javaType, path);
} else if (WebFinderMetadata.isValid(upstreamDependency)) {
final JavaType javaType = WebFinderMetadata.getJavaType(upstreamDependency);
final LogicalPath path = WebFinderMetadata.getPath(upstreamDependency);
downstreamDependency = JspMetadata.createIdentifier(javaType, path);
}
// later on)
if (getMetadataDependencyRegistry().getDownstream(upstreamDependency).contains(downstreamDependency)) {
return;
}
} else if (MetadataIdentificationUtils.isIdentifyingInstance(upstreamDependency)) {
// This is the generic fallback listener, ie from
// MetadataDependencyRegistry.addListener(this) in the activate()
// method
// Get the metadata that just changed
final MetadataItem metadataItem = getMetadataService().get(upstreamDependency);
// detected
if (metadataItem == null || !metadataItem.isValid() || !(metadataItem instanceof ItdTypeDetailsProvidingMetadataItem)) {
// let's gracefully abort
return;
}
// Let's ensure we have some ITD type details to actually work with
final ItdTypeDetailsProvidingMetadataItem itdMetadata = (ItdTypeDetailsProvidingMetadataItem) metadataItem;
final ItdTypeDetails itdTypeDetails = itdMetadata.getMemberHoldingTypeDetails();
if (itdTypeDetails == null) {
return;
}
final String localMid = formBackingObjectTypesToLocalMids.get(itdTypeDetails.getGovernor().getName());
if (localMid != null) {
getMetadataService().evictAndGet(localMid);
}
return;
}
if (MetadataIdentificationUtils.isIdentifyingInstance(downstreamDependency)) {
getMetadataService().evictAndGet(downstreamDependency);
}
}
Aggregations