Beispiel #1
0
 public static IMethod[] removeGeneratedMethods(IMethod[] methods) throws Exception {
   List<IMethod> result = new ArrayList<IMethod>();
   for (IMethod m : methods) {
     if (m.getNameRange().getLength() > 0) result.add(m);
   }
   return result.size() == methods.length ? methods : result.toArray(new IMethod[0]);
 }
Beispiel #2
0
 /**
  * Returns the source range for the given {@link IApiMethod} within the given {@link IType}
  *
  * @param type the type to look for the method within
  * @param reference the reference the method comes from
  * @param method the {@link IApiMethod} to look for the source range for
  * @return the {@link ISourceRange} in the {@link IType} enclosing the given {@link IApiMethod}
  * @throws CoreException
  * @throws JavaModelException
  */
 protected Position getSourceRangeForMethod(IType type, IReference reference, IApiMethod method)
     throws CoreException, JavaModelException {
   IMethod match = findMethodInType(type, method);
   Position pos = null;
   if (match != null) {
     ISourceRange range = match.getNameRange();
     if (range != null) {
       pos = new Position(range.getOffset(), range.getLength());
     }
   }
   if (pos == null) {
     return defaultSourcePosition(type, reference);
   }
   return pos;
 }
Beispiel #3
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;
  }
Beispiel #4
0
  public static int getLineNumber(IJavaElement element) {
    if (element != null && element instanceof IMethod) {
      try {
        IMethod method = (IMethod) element;
        int lines = 0;
        if (method.getDeclaringType() != null
            && method.getDeclaringType().getCompilationUnit() != null) {
          String targetsource = method.getDeclaringType().getCompilationUnit().getSource();
          if (targetsource != null) {
            String sourceuptomethod = targetsource.substring(0, method.getNameRange().getOffset());

            char[] chars = new char[sourceuptomethod.length()];
            sourceuptomethod.getChars(0, sourceuptomethod.length(), chars, 0);
            for (char element0 : chars) {
              if (element0 == '\n') {
                lines++;
              }
            }
            return new Integer(lines + 1);
          }
        }
      } catch (JavaModelException e) {
      }
    } else if (element != null
        && element instanceof IType
        && ((IType) element).getCompilationUnit() != null) {
      try {
        IType type = (IType) element;
        int lines = 0;
        String targetsource = type.getCompilationUnit().getSource();
        if (targetsource != null) {
          String sourceuptomethod = targetsource.substring(0, type.getNameRange().getOffset());

          char[] chars = new char[sourceuptomethod.length()];
          sourceuptomethod.getChars(0, sourceuptomethod.length(), chars, 0);
          for (char element0 : chars) {
            if (element0 == '\n') {
              lines++;
            }
          }
          return new Integer(lines + 1);
        }
      } catch (JavaModelException e) {
      }
    } else if (element != null && element instanceof IField) {
      try {
        IField type = (IField) element;
        int lines = 0;
        ICompilationUnit cu = type.getCompilationUnit();
        if (cu != null) {
          String targetsource = cu.getSource();
          if (targetsource != null) {
            String sourceuptomethod = targetsource.substring(0, type.getNameRange().getOffset());

            char[] chars = new char[sourceuptomethod.length()];
            sourceuptomethod.getChars(0, sourceuptomethod.length(), chars, 0);
            for (char element0 : chars) {
              if (element0 == '\n') {
                lines++;
              }
            }
            return new Integer(lines + 1);
          }
        }
      } catch (JavaModelException e) {
      }
    }
    return new Integer(-1);
  }