private ISeamProperty findSeamProperty(IFile file, int start, int end) { if (file == null) return null; IProject project = file.getProject(); if (project == null) return null; ISeamProject seamProject = SeamCorePlugin.getSeamProject(project, true); if (seamProject == null) return null; Set<ISeamComponent> components = seamProject.getComponentsByPath(file.getFullPath()); for (ISeamComponent component : components) { Set<ISeamXmlComponentDeclaration> declarations = component.getXmlDeclarations(); for (ISeamXmlComponentDeclaration declaration : declarations) { Collection<ISeamProperty> properties = declaration.getProperties(); for (ISeamProperty property : properties) { ITextSourceReference location = property.getLocationFor(ISeamXmlComponentDeclaration.NAME); if (location.getStartPosition() <= start && (location.getStartPosition() + location.getLength()) >= end) return property; } } } return null; }
public AddSetterMarkerResolution( ISeamProperty property, ISeamJavaComponentDeclaration javaDeclaration) { this.property = property; this.javaDeclaration = javaDeclaration; this.label = MessageFormat.format( SeamCoreMessages.ADD_SETTER_MARKER_RESOLUTION_TITLE, new Object[] {property.getName(), javaDeclaration.getClassName()}); }
private IMarkerResolution[] findResolutions(IMarker marker) throws CoreException { int messageId = getMessageID(marker); if (messageId == -1) return new IMarkerResolution[] {}; IFile file = (IFile) marker.getResource(); Integer attribute = ((Integer) marker.getAttribute(IMarker.CHAR_START)); if (attribute == null) return new IMarkerResolution[] {}; int start = attribute.intValue(); attribute = ((Integer) marker.getAttribute(IMarker.CHAR_END)); if (attribute == null) return new IMarkerResolution[] {}; int end = attribute.intValue(); if (JAVA_EXTENSION.equals(file.getFileExtension())) { if (messageId == SeamCoreValidator.NONUNIQUE_COMPONENT_NAME_MESSAGE_ID) { return new IMarkerResolution[] { new RenameAnnotationMarkerResolution( SeamCoreMessages.RENAME_NAME_ANNOTATION_MARKER_RESOLUTION_TITLE, "org.jboss.seam.annotations.Name", file, start, end), new DeleteAnnotationMarkerResolution( SeamCoreMessages.DELETE_NAME_ANNOTATION_MARKER_RESOLUTION_TITLE, "org.jboss.seam.annotations.Name", file, start, end) }; } else if (messageId == SeamCoreValidator.DUPLICATE_REMOVE_MESSAGE_ID) return new IMarkerResolution[] { new DeleteAnnotationMarkerResolution( SeamCoreMessages.DELETE_REMOVE_ANNOTATION_MARKER_RESOLUTION_TITLE, "javax.ejb.Remove", file, start, end) }; else if (messageId == SeamCoreValidator.DUPLICATE_DESTROY_MESSAGE_ID) return new IMarkerResolution[] { new DeleteAnnotationMarkerResolution( SeamCoreMessages.DELETE_DESTROY_ANNOTATION_MARKER_RESOLUTION_TITLE, "org.jboss.seam.annotations.Destroy", file, start, end) }; else if (messageId == SeamCoreValidator.DUPLICATE_CREATE_MESSAGE_ID) return new IMarkerResolution[] { new DeleteAnnotationMarkerResolution( SeamCoreMessages.DELETE_CREATE_ANNOTATION_MARKER_RESOLUTION_TITLE, "org.jboss.seam.annotations.Create", file, start, end) }; else if (messageId == SeamCoreValidator.DUPLICATE_UNWRAP_MESSAGE_ID) return new IMarkerResolution[] { new DeleteAnnotationMarkerResolution( SeamCoreMessages.DELETE_UNWRAP_ANNOTATION_MARKER_RESOLUTION_TITLE, "org.jboss.seam.annotations.Unwrap", file, start, end) }; else if (messageId == SeamCoreValidator.DESTROY_METHOD_BELONGS_TO_STATELESS_SESSION_BEAN_MESSAGE_ID) return new IMarkerResolution[] { new DeleteAnnotationMarkerResolution( SeamCoreMessages.DELETE_DESTROY_ANNOTATION_MARKER_RESOLUTION_TITLE, "org.jboss.seam.annotations.Destroy", file, start, end) }; else if (messageId == SeamCoreValidator.CREATE_DOESNT_BELONG_TO_COMPONENT_MESSAGE_ID) return new IMarkerResolution[] { new AddAnnotationMarkerResolution( SeamCoreMessages.ADD_NAME_ANNOTATION_MARKER_RESOLUTION_TITLE, "org.jboss.seam.annotations.Name", file, start, end, true), new DeleteAnnotationMarkerResolution( SeamCoreMessages.DELETE_CREATE_ANNOTATION_MARKER_RESOLUTION_TITLE, "org.jboss.seam.annotations.Create", file, start, end) }; else if (messageId == SeamCoreValidator.UNWRAP_DOESNT_BELONG_TO_COMPONENT_MESSAGE_ID) return new IMarkerResolution[] { new AddAnnotationMarkerResolution( SeamCoreMessages.ADD_NAME_ANNOTATION_MARKER_RESOLUTION_TITLE, "org.jboss.seam.annotations.Name", file, start, end, true), new DeleteAnnotationMarkerResolution( SeamCoreMessages.DELETE_UNWRAP_ANNOTATION_MARKER_RESOLUTION_TITLE, "org.jboss.seam.annotations.Unwrap", file, start, end) }; else if (messageId == SeamCoreValidator.OBSERVER_DOESNT_BELONG_TO_COMPONENT_MESSAGE_ID) return new IMarkerResolution[] { new AddAnnotationMarkerResolution( SeamCoreMessages.ADD_NAME_ANNOTATION_MARKER_RESOLUTION_TITLE, "org.jboss.seam.annotations.Name", file, start, end, true), new DeleteAnnotationMarkerResolution( SeamCoreMessages.DELETE_OBSERVER_ANNOTATION_MARKER_RESOLUTION_TITLE, "org.jboss.seam.annotations.Observer", file, start, end) }; else if (messageId == SeamCoreValidator.STATEFUL_COMPONENT_DOES_NOT_CONTAIN_REMOVE_ID) return new IMarkerResolution[] { new AddAnnotatedMethodMarkerResolution( SeamCoreMessages.ADD_ANNOTATED_REMOVE_METHOD_MARKER_RESOLUTION_TITLE, "javax.ejb.Remove", file, start, end) }; else if (messageId == SeamCoreValidator.STATEFUL_COMPONENT_DOES_NOT_CONTAIN_DESTROY_ID) return new IMarkerResolution[] { new AddAnnotatedMethodMarkerResolution( SeamCoreMessages.ADD_ANNOTATED_DESTROY_METHOD_MARKER_RESOLUTION_TITLE, "org.jboss.seam.annotations.Destroy", file, start, end) }; else if (messageId == SeamCoreValidator.STATEFUL_COMPONENT_WRONG_SCOPE_ID) return new IMarkerResolution[] { new ChangeScopeMarkerResolution( SeamCoreMessages.CHANGE_SCOPETYPE_MARKER_RESOLUTION_TITLE, "ScopeType.APPLICATION", file, start, end), new ChangeScopeMarkerResolution( SeamCoreMessages.CHANGE_SCOPETYPE_MARKER_RESOLUTION_TITLE, "ScopeType.BUSINESS_PROCESS", file, start, end), new ChangeScopeMarkerResolution( SeamCoreMessages.CHANGE_SCOPETYPE_MARKER_RESOLUTION_TITLE, "ScopeType.CONVERSATION", file, start, end), new ChangeScopeMarkerResolution( SeamCoreMessages.CHANGE_SCOPETYPE_MARKER_RESOLUTION_TITLE, "ScopeType.EVENT", file, start, end), new ChangeScopeMarkerResolution( SeamCoreMessages.CHANGE_SCOPETYPE_MARKER_RESOLUTION_TITLE, "ScopeType.METHOD", file, start, end), new ChangeScopeMarkerResolution( SeamCoreMessages.CHANGE_SCOPETYPE_MARKER_RESOLUTION_TITLE, "ScopeType.SESSION", file, start, end), new ChangeScopeMarkerResolution( SeamCoreMessages.CHANGE_SCOPETYPE_MARKER_RESOLUTION_TITLE, "ScopeType.UNSPECIFIED", file, start, end) }; else if (messageId == SeamCoreValidator.ENTITY_COMPONENT_WRONG_SCOPE_ID) return new IMarkerResolution[] { new ChangeScopeMarkerResolution( SeamCoreMessages.CHANGE_SCOPETYPE_MARKER_RESOLUTION_TITLE, "ScopeType.APPLICATION", file, start, end), new ChangeScopeMarkerResolution( SeamCoreMessages.CHANGE_SCOPETYPE_MARKER_RESOLUTION_TITLE, "ScopeType.BUSINESS_PROCESS", file, start, end), new ChangeScopeMarkerResolution( SeamCoreMessages.CHANGE_SCOPETYPE_MARKER_RESOLUTION_TITLE, "ScopeType.CONVERSATION", file, start, end), new ChangeScopeMarkerResolution( SeamCoreMessages.CHANGE_SCOPETYPE_MARKER_RESOLUTION_TITLE, "ScopeType.EVENT", file, start, end), new ChangeScopeMarkerResolution( SeamCoreMessages.CHANGE_SCOPETYPE_MARKER_RESOLUTION_TITLE, "ScopeType.METHOD", file, start, end), new ChangeScopeMarkerResolution( SeamCoreMessages.CHANGE_SCOPETYPE_MARKER_RESOLUTION_TITLE, "ScopeType.PAGE", file, start, end), new ChangeScopeMarkerResolution( SeamCoreMessages.CHANGE_SCOPETYPE_MARKER_RESOLUTION_TITLE, "ScopeType.SESSION", file, start, end), new ChangeScopeMarkerResolution( SeamCoreMessages.CHANGE_SCOPETYPE_MARKER_RESOLUTION_TITLE, "ScopeType.UNSPECIFIED", file, start, end) }; } else if (XML_EXTENSION.equals(file.getFileExtension())) { if (messageId == SeamCoreValidator.UNKNOWN_COMPONENT_PROPERTY_ID) { ISeamProperty property = findSeamProperty(file, start, end); if (property != null) { if (property.getParent() != null && property.getParent() instanceof SeamComponentDeclaration) { SeamComponentDeclaration xmlDeclaration = (SeamComponentDeclaration) property.getParent(); if (xmlDeclaration == null) { return new IMarkerResolution[] {}; } for (ISeamComponent component : xmlDeclaration.getComponents()) { ISeamJavaComponentDeclaration javaDeclaration = component.getJavaDeclaration(); if (javaDeclaration != null) return new IMarkerResolution[] { new AddSetterMarkerResolution(property, javaDeclaration) }; } } } } } return new IMarkerResolution[] {}; }
public void run(IMarker marker) { IFile file = (IFile) javaDeclaration.getResource(); try { ICompilationUnit original = EclipseUtil.getCompilationUnit(file); if (original == null) { return; } ICompilationUnit compilationUnit = original.getWorkingCopy(new NullProgressMonitor()); String lineDelim = JavaPropertyGenerator.getLineDelimiterUsed(compilationUnit); Hashtable<String, String> options = JavaCore.getOptions(); int tabSize = new Integer(options.get("org.eclipse.jdt.core.formatter.tabulation.size")); StringBuffer tabBuf = new StringBuffer(); for (int i = 0; i < tabSize; i++) tabBuf.append(" "); String tab = tabBuf.toString(); IType type = compilationUnit.findPrimaryType(); IField field = type.getField(property.getName()); String propertyType = ""; if (field != null && field.exists()) { propertyType = field.getTypeSignature(); } else { propertyType = "String"; // $NON-NLS-1$ StringBuffer buf = new StringBuffer(); buf.append( tab + "private " + propertyType + " " + property.getName() + ";"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ buf.append(lineDelim); field = type.createField(buf.toString(), null, false, new NullProgressMonitor()); if (field != null) { IBuffer buffer = compilationUnit.getBuffer(); buffer.replace( field.getSourceRange().getOffset(), field.getSourceRange().getLength(), buf.toString()); synchronized (compilationUnit) { compilationUnit.reconcile(ICompilationUnit.NO_AST, true, null, null); } } } IMethod oldMethod = GetterSetterUtil.getSetter(field); if (oldMethod == null || !oldMethod.exists()) { String setterName = GetterSetterUtil.getSetterName(field, null); // JavaPropertyGenerator.createSetter(compilationUnit, type, "public", // field.getTypeSignature(), setterName, lineDelim); String stub = GetterSetterUtil.getSetterStub(field, setterName, true, Flags.AccPublic); IMethod newMethod = type.createMethod(stub, null, false, new NullProgressMonitor()); if (newMethod != null) { IBuffer buffer = compilationUnit.getBuffer(); // format StringBuffer buf = new StringBuffer(); buf.append(lineDelim); buf.append( tab + "public void " + setterName + "(" + propertyType + " " + property.getName() + "){"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ buf.append(lineDelim); buf.append(tab + tab + "this." + property.getName() + " = " + property.getName() + ";"); buf.append(lineDelim); buf.append(tab + "}"); buf.append(lineDelim); buffer.replace( newMethod.getSourceRange().getOffset(), newMethod.getSourceRange().getLength(), buf.toString()); } } compilationUnit.commitWorkingCopy(false, new NullProgressMonitor()); compilationUnit.discardWorkingCopy(); } catch (CoreException ex) { SeamGuiPlugin.getPluginLog().logError(ex); } }