示例#1
0
 public String getNewSetterName() throws CoreException {
   return GetterSetterUtil.getSetterName(
       fField.getJavaProject(),
       getNewElementName(),
       fField.getFlags(),
       JavaModelUtil.isBoolean(fField),
       null);
 }
示例#2
0
 public String getNewGetterName() throws CoreException {
   IMethod primaryGetterCandidate =
       JavaModelUtil.findMethod(
           GetterSetterUtil.getGetterName(fField, new String[0]),
           new String[0],
           false,
           fField.getDeclaringType());
   if (!JavaModelUtil.isBoolean(fField)
       || (primaryGetterCandidate != null && primaryGetterCandidate.exists()))
     return GetterSetterUtil.getGetterName(
         fField.getJavaProject(),
         getNewElementName(),
         fField.getFlags(),
         JavaModelUtil.isBoolean(fField),
         null);
   // bug 30906 describes why we need to look for other alternatives here
   return GetterSetterUtil.getGetterName(
       fField.getJavaProject(), getNewElementName(), fField.getFlags(), false, null);
 }
  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);
    }
  }
示例#4
0
 public IMethod getSetter() throws CoreException {
   return GetterSetterUtil.getSetter(fField);
 }