Esempio n. 1
0
 private static void checkMethodInType(
     IType destinationType, RefactoringStatus result, IMethod method) throws JavaModelException {
   IMethod[] destinationTypeMethods = destinationType.getMethods();
   IMethod found = findMethod(method, destinationTypeMethods);
   if (found != null) {
     RefactoringStatusContext context =
         JavaStatusContext.create(destinationType.getCompilationUnit(), found.getSourceRange());
     String message =
         Messages.format(
             RefactoringCoreMessages.MemberCheckUtil_signature_exists,
             new String[] {
               BasicElementLabels.getJavaElementName(method.getElementName()),
               getQualifiedLabel(destinationType)
             });
     result.addError(message, context);
   } else {
     IMethod similar = Checks.findMethod(method, destinationType);
     if (similar != null) {
       String message =
           Messages.format(
               RefactoringCoreMessages.MemberCheckUtil_same_param_count,
               new String[] {
                 BasicElementLabels.getJavaElementName(method.getElementName()),
                 getQualifiedLabel(destinationType)
               });
       RefactoringStatusContext context =
           JavaStatusContext.create(
               destinationType.getCompilationUnit(), similar.getSourceRange());
       result.addWarning(message, context);
     }
   }
 }
Esempio n. 2
0
  /**
   * Determines is the java element contains a method with a specific annotation.
   *
   * <p>The syntax for the property tester is of the form: qualified or unqualified annotation name,
   * modifiers
   * <li>qualified or unqualified annotation name, required. For example, <code>org.junit.JUnit
   *     </code>.
   * <li>modifiers - optional space separated list of modifiers, for example, <code>public static
   *     </code>.
   * </ol>
   *
   * @param element the element to check for the method
   * @param annotationName the qualified or unqualified name of the annotation to look for
   * @return true if the method is found in the element, false otherwise
   */
  private boolean hasMethodWithAnnotation(IJavaElement element, Object[] args) {
    try {
      String annotationType = (String) args[0];
      int flags = 0;
      if (args.length > 1) {
        String[] modifiers = ((String) args[1]).split(" "); // $NON-NLS-1$
        for (int j = 0; j < modifiers.length; j++) {
          String modifier = modifiers[j];
          Integer flag = (Integer) fgModifiers.get(modifier);
          if (flag != null) {
            flags = flags | flag.intValue();
          }
        }
      } else {
        flags = -1;
      }

      IType type = getType(element);
      if (type == null || !type.exists()) {
        return false;
      }
      IMethod[] methods = type.getMethods();
      if (methods.length == 0) {
        return false;
      }

      IBuffer buffer = null;
      IOpenable openable = type.getOpenable();
      if (openable instanceof ICompilationUnit) {
        buffer = ((ICompilationUnit) openable).getBuffer();
      } else if (openable instanceof IClassFile) {
        buffer = ((IClassFile) openable).getBuffer();
      }
      if (buffer == null) {
        return false;
      }
      IScanner scanner = null; // delay initialization

      for (int i = 0; i < methods.length; i++) {
        IMethod curr = methods[i];
        if (curr.isConstructor() || (flags != -1 && flags != (curr.getFlags() & FLAGS_MASK))) {
          continue;
        }

        ISourceRange sourceRange = curr.getSourceRange();
        ISourceRange nameRange = curr.getNameRange();
        if (sourceRange != null && nameRange != null) {
          if (scanner == null) {
            scanner = ToolFactory.createScanner(false, false, true, false);
            scanner.setSource(buffer.getCharacters());
          }
          scanner.resetTo(sourceRange.getOffset(), nameRange.getOffset());
          if (findAnnotation(scanner, annotationType)) {
            return true;
          }
        }
      }
    } catch (JavaModelException e) {
    } catch (InvalidInputException e) {
    }
    return false;
  }
  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);
    }
  }