コード例 #1
0
 @SuppressWarnings("deprecation")
 private boolean isParameterNamesAvailable() throws Exception {
   ASTParser parser = ASTParser.newParser(AST.JLS3);
   parser.setIgnoreMethodBodies(true);
   IJavaProject javaProject = projectProvider.getJavaProject(resourceSet);
   parser.setProject(javaProject);
   IType type = javaProject.findType("org.eclipse.xtext.common.types.testSetups.TestEnum");
   IBinding[] bindings = parser.createBindings(new IJavaElement[] {type}, null);
   ITypeBinding typeBinding = (ITypeBinding) bindings[0];
   IMethodBinding[] methods = typeBinding.getDeclaredMethods();
   for (IMethodBinding method : methods) {
     if (method.isConstructor()) {
       IMethod element = (IMethod) method.getJavaElement();
       if (element.exists()) {
         String[] parameterNames = element.getParameterNames();
         if (parameterNames.length == 1 && parameterNames[0].equals("string")) {
           return true;
         }
       } else {
         return false;
       }
     }
   }
   return false;
 }
  /** {@inheritDoc} */
  public IHyperlink createHyperlink(
      String name,
      String target,
      Node node,
      Node parentNode,
      IDocument document,
      ITextViewer textViewer,
      IRegion hyperlinkRegion,
      IRegion cursor) {
    String parentName = null;
    if (parentNode != null) {
      parentName = parentNode.getNodeName();
    }

    List<String> propertyPaths = new ArrayList<String>();
    hyperlinkRegion =
        BeansEditorUtils.extractPropertyPathFromCursorPosition(
            hyperlinkRegion, cursor, target, propertyPaths);
    if ("bean".equals(parentName) && StringUtils.hasText(target)) {
      IFile file = BeansEditorUtils.getFile(document);
      String className =
          BeansEditorUtils.getClassNameForBean(file, node.getOwnerDocument(), parentNode);
      IType type = JdtUtils.getJavaType(file.getProject(), className);

      if (type != null) {
        IBeansConfig config = BeansCorePlugin.getModel().getConfig(file);
        if (config != null && parentNode instanceof Element) {
          IModelElement element = BeansModelUtils.getModelElement((Element) parentNode, config);
          int argIndex = getArgumentIndex(node);
          if (argIndex >= 0) {
            if (element instanceof IBean) {
              IBean bean = (IBean) element;
              int count = bean.getConstructorArguments().size();
              if (count > 0) {
                try {
                  Set<IMethod> methods = Introspector.getConstructors(type, count, false);
                  Iterator<IMethod> iter = methods.iterator();
                  while (iter.hasNext()) {
                    IMethod candidate = iter.next();
                    if (target.equalsIgnoreCase(candidate.getParameterNames()[argIndex])) {
                      // return new JavaElementHyperlink(hyperlinkRegion,
                      // candidate.getParameters()[argIndex]);
                      // TODO: just a temporary workaround for making this Eclipse 3.6 compatible
                      return new JavaElementHyperlink(hyperlinkRegion, candidate);
                    }
                  }
                } catch (JavaModelException e) {
                  // do nothing
                }
              }
            }
          }
        }
      }
    }
    return null;
  }
コード例 #3
0
  /**
   * Create a {@link BeansJavaCompletionProposal} for the given {@link IMethod} and report it on the
   * {@link ContentAssistRequest}.
   */
  protected void createMethodProposal(IContentAssistProposalRecorder recorder, IMethod method) {
    try {
      String[] parameterNames = method.getParameterNames();
      String[] parameterTypes = JdtUtils.getParameterTypesString(method);
      String returnType = JdtUtils.getReturnTypeString(method, true);
      String methodName = JdtUtils.getMethodName(method);

      String replaceText = methodName;

      StringBuilder buf = new StringBuilder();

      // add method name
      buf.append(replaceText);

      // add method parameters
      if (parameterTypes.length > 0 && parameterNames.length > 0) {
        buf.append(" (");
        for (int i = 0; i < parameterTypes.length; i++) {
          buf.append(parameterTypes[i]);
          buf.append(' ');
          buf.append(parameterNames[i]);
          if (i < (parameterTypes.length - 1)) {
            buf.append(", ");
          }
        }
        buf.append(") ");
      } else {
        buf.append("() ");
      }

      // add return type
      if (returnType != null) {
        buf.append(Signature.getSimpleName(returnType));
        buf.append(" - ");
      } else {
        buf.append(" void - ");
      }

      // add class name
      buf.append(JdtUtils.getParentName(method));

      String displayText = buf.toString();
      Image image =
          Activator.getDefault()
              .getJavaElementLabelProvider()
              .getImageLabel(method, method.getFlags() | JavaElementImageProvider.SMALL_ICONS);

      recorder.recordProposal(image, METHOD_RELEVANCE, displayText, replaceText, method);
    } catch (JavaModelException e) {
      // do nothing
    }
  }
コード例 #4
0
  private String toSignature(IMethod method) {
    StringBuilder result = new StringBuilder(method.getElementName());
    String[] types = method.getParameterTypes();
    try {
      String[] names = method.getParameterNames();
      result.append("(");
      for (int i = 0; i < types.length; i++) {
        if (i > 0) result.append(", ");
        result.append(Signature.toString(types[i]) + " " + names[i]);
      }
      result.append(")");
    } catch (JavaModelException ex) {
      // ignore
    }

    return result.toString();
  }
コード例 #5
0
  @Override
  public List<ICompletionProposal> computeCompletionProposals(
      final ContentAssistInvocationContext context, final IProgressMonitor monitor) {
    final List<ICompletionProposal> list = new ArrayList<ICompletionProposal>();
    boolean extendContext = false;
    try {
      if (context instanceof JavaContentAssistInvocationContext) {
        final ITextViewer viewer = context.getViewer();
        final List<ScriptVariable> scriptVariables = getScriptVariables(viewer);
        if (scriptVariables.isEmpty()) {
          return list;
        }
        final CompletionContext coreContext =
            ((JavaContentAssistInvocationContext) context).getCoreContext();
        if (coreContext != null && !coreContext.isExtended()) {
          // must use reflection to set the fields
          ReflectionUtils.setPrivateField(
              InternalCompletionContext.class, "isExtended", coreContext, true);
          extendContext = true;
        }
        final ICompilationUnit unit =
            ((JavaContentAssistInvocationContext) context).getCompilationUnit();
        if (unit instanceof GroovyCompilationUnit) {
          if (((GroovyCompilationUnit) unit).getModuleNode() == null) {
            return Collections.emptyList();
          }
          final ContentAssistContext assistContext =
              new GroovyCompletionProposalComputer()
                  .createContentAssistContext(
                      (GroovyCompilationUnit) unit,
                      context.getInvocationOffset(),
                      context.getDocument());
          CharSequence prefix = null;
          try {
            prefix = context.computeIdentifierPrefix();
          } catch (final BadLocationException e) {
            BonitaStudioLog.error(e);
          }

          if (assistContext != null && assistContext.completionNode instanceof VariableExpression) {
            try {
              final VariableExpression expr = (VariableExpression) assistContext.completionNode;
              if (scriptVariables != null) {
                for (final ScriptVariable f : scriptVariables) {
                  if (expr.getName().equals(f.getName())) {
                    final IType type = javaProject.findType(f.getType());
                    if (type == null) {
                      return list;
                    }
                    for (final IMethod m : type.getMethods()) {
                      if (m.getElementName().startsWith(prefix.toString())) {
                        final GroovyCompletionProposal proposal =
                            new GroovyCompletionProposal(
                                CompletionProposal.METHOD_REF, context.getInvocationOffset());
                        proposal.setName(m.getElementName().toCharArray());
                        proposal.setCompletion(
                            m.getElementName().substring(prefix.length()).toCharArray());
                        proposal.setFlags(m.getFlags());

                        if (prefix.length() == m.getElementName().length()) {
                          proposal.setReplaceRange(
                              context.getInvocationOffset(), context.getInvocationOffset());
                          proposal.setReceiverRange(0, 0);
                        } else {
                          proposal.setReplaceRange(
                              context.getInvocationOffset() - prefix.length(),
                              context.getInvocationOffset());
                          proposal.setReceiverRange(prefix.length(), prefix.length());
                        }

                        final char[][] parametersArray =
                            new char[m.getParameterNames().length][256];
                        final List<Parameter> parameters = new ArrayList<Parameter>();
                        for (int i = 0; i < m.getParameterNames().length; i++) {
                          parametersArray[i] = m.getParameterNames()[i].toCharArray();
                          parameters.add(
                              new Parameter(
                                  ClassHelper.make(
                                      Signature.getSignatureSimpleName(m.getParameterTypes()[i])),
                                  m.getParameterNames()[i]));
                        }

                        final ClassNode classNode =
                            ClassHelper.make(m.getDeclaringType().getFullyQualifiedName());
                        proposal.setDeclarationSignature(
                            ProposalUtils.createTypeSignature(classNode));
                        proposal.setParameterNames(parametersArray);
                        if (m.getDeclaringType().getFullyQualifiedName().equals(f.getType())) {
                          proposal.setRelevance(100);
                        }

                        final MethodNode methodNode =
                            new MethodNode(
                                m.getElementName(),
                                m.getFlags(),
                                ClassHelper.make(
                                    Signature.getSignatureSimpleName(m.getReturnType())),
                                parameters.toArray(new Parameter[parameters.size()]),
                                new ClassNode[0],
                                null);
                        final char[] methodSignature =
                            ProposalUtils.createMethodSignature(methodNode);
                        proposal.setSignature(methodSignature);

                        final GroovyJavaGuessingCompletionProposal groovyProposal =
                            GroovyJavaGuessingCompletionProposal.createProposal(
                                proposal,
                                (JavaContentAssistInvocationContext) context,
                                true,
                                "Groovy",
                                ProposalFormattingOptions.newFromOptions());
                        if (groovyProposal != null) {
                          list.add(groovyProposal);
                        }
                      }
                    }
                  }
                }
              }
            } catch (final JavaModelException e) {
              BonitaStudioLog.error(e);
            }
          }
        }

        return list;
      }
    } finally {
      final CompletionContext coreContext =
          ((JavaContentAssistInvocationContext) context).getCoreContext();
      if (extendContext && coreContext != null && coreContext.isExtended()) {
        // must use reflection to set the fields
        ReflectionUtils.setPrivateField(
            InternalCompletionContext.class, "isExtended", coreContext, false);
      }
    }

    return Collections.emptyList();
  }