コード例 #1
0
 private void printMethods(IType type, StringBuilder sb) throws JavaModelException {
   IMethod[] methods = type.getMethods();
   if (methods.length > 0) {
     sb.append("<METHODS>");
     for (IMethod method : methods) {
       sb.append("<METHOD NAME=\"" + method.getElementName() + "\">");
       ILocalVariable[] vars = method.getParameters();
       sb.append("<PARAMETERS>");
       if (vars.length > 0) {
         for (int i = 0; i < vars.length; i += 1) {
           System.out.println(vars[i].toString());
           System.out.println(vars[i].getSource());
           sb.append("<PARAMETER>" + vars[i].getSource() + "</PARAMETER>");
         }
       } else {
         sb.append("<PARAMETER>void</PARAMETER>");
       }
       sb.append("</PARAMETERS>");
       // method.gets
       sb.append("<SIGNATURE>" + method.getSignature() + "</SIGNATURE>");
       sb.append("<RETURNTYPE>" + method.getReturnType() + "</RETURNTYPE>");
       sb.append("</METHOD>");
     }
     sb.append("</METHODS>");
   }
 }
コード例 #2
0
  @Override
  public IJavaCompletionProposal[] getAssists(
      IInvocationContext context, IProblemLocation[] locations) throws CoreException {
    ICompilationUnit compilationUnit = context.getCompilationUnit();
    IType primaryType = compilationUnit.findPrimaryType();
    if (primaryType == null || !primaryType.isInterface()) ; // return null;

    IJavaElement[] elements =
        compilationUnit.codeSelect(context.getSelectionOffset(), context.getSelectionLength());
    for (IJavaElement element : elements) {
      if (element.getElementType() == IJavaElement.METHOD) {
        IMethod method = (IMethod) element;
        if (!method.getDeclaringType().isInterface()) return null;

        final String statementAnnotation = getStatementAnnotation(method);
        if (method.getParameters().length == 0 && statementAnnotation == null) return null;

        CompilationUnit astNode = getAstNode(compilationUnit);
        astNode.recordModifications();
        final MapperMethod mapperMethod = getMapperMethod(astNode, method);
        if (mapperMethod == null) return null;

        List<IJavaCompletionProposal> proposals = new ArrayList<IJavaCompletionProposal>();

        if (method.getParameters().length > 0) {
          proposals.add(
              new QuickAssistCompletionProposal("Add @Param to parameters") {
                private CompilationUnit astNode;

                private MapperMethod method;

                @SuppressWarnings("unchecked")
                @Override
                public void apply(IDocument document) {
                  List<SingleVariableDeclaration> params = method.parameters();
                  for (SingleVariableDeclaration param : params) {
                    List<IExtendedModifier> modifiers = param.modifiers();
                    if (!hasParamAnnotation(modifiers)) {
                      if (JavaMapperUtil.TYPE_ROW_BOUNDS.equals(
                          param.resolveBinding().getType().getQualifiedName())) continue;
                      AST ast = param.getAST();
                      SingleMemberAnnotation annotation = ast.newSingleMemberAnnotation();
                      annotation.setTypeName(ast.newName("Param"));
                      StringLiteral paramValue = ast.newStringLiteral();
                      paramValue.setLiteralValue(param.getName().getFullyQualifiedName());
                      annotation.setValue(paramValue);
                      param.modifiers().add(annotation);
                    }
                  }
                  TextEdit textEdit = astNode.rewrite(document, null);
                  try {
                    textEdit.apply(document);
                  } catch (MalformedTreeException e) {
                    Activator.log(Status.ERROR, e.getMessage(), e);
                  } catch (BadLocationException e) {
                    Activator.log(Status.ERROR, e.getMessage(), e);
                  }
                }

                private boolean hasParamAnnotation(List<IExtendedModifier> modifiers) {
                  for (IExtendedModifier modifier : modifiers) {
                    if (modifier.isAnnotation()
                        && "Param"
                            .equals(
                                ((Annotation) modifier).getTypeName().getFullyQualifiedName())) {
                      return true;
                    }
                  }
                  return false;
                }

                private QuickAssistCompletionProposal init(
                    CompilationUnit astNode, MapperMethod method) {
                  this.astNode = astNode;
                  this.method = method;
                  return this;
                }
              }.init(astNode, mapperMethod));
        }

        if (mapperMethod.getStatement() != null) {
          proposals.add(
              new QuickAssistCompletionProposal(
                  "Copy @" + statementAnnotation + " statement to clipboard") {
                @Override
                public void apply(IDocument document) {
                  Clipboard clipboard = new Clipboard(Display.getCurrent());
                  clipboard.setContents(
                      new Object[] {mapperMethod.getStatement()},
                      new Transfer[] {TextTransfer.getInstance()});
                }
              });
        }

        return proposals.toArray(new IJavaCompletionProposal[proposals.size()]);
      }
    }
    return null;
  }