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()}); }
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); } }