org.eclipse.jdt.core.Signature.toString()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(153)

本文整理了Java中org.eclipse.jdt.core.Signature.toString()方法的一些代码示例,展示了Signature.toString()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Signature.toString()方法的具体详情如下:
包路径:org.eclipse.jdt.core.Signature
类名称:Signature
方法名:toString

Signature.toString介绍

[英]Converts the given type signature to a readable string. The signature is expected to be dot-based.

For example:

toString("[Ljava.lang.String;") -> "java.lang.String[]" 
toString("I") -> "int" 
toString("+QObject;") -> "? extends Object"

Note: This method assumes that a type signature containing a '$' is an inner type signature. While this is correct in most cases, someone could define a non-inner type name containing a '$'. Handling this correctly in all cases would have required resolving the signature, which generally not feasible.
[中]将给定的类型签名转换为可读字符串。签名应该是基于点的。
例如:

toString("[Ljava.lang.String;") -> "java.lang.String[]" 
toString("I") -> "int" 
toString("+QObject;") -> "? extends Object"

注意:此方法假定包含'$'的类型签名是内部类型签名。虽然在大多数情况下这是正确的,但有人可以定义一个包含'$'的非内部类型名称。在所有情况下正确处理这一点都需要解决签名问题,这通常是不可行的。

代码示例

代码示例来源:origin: io.sarl/io.sarl.eclipse

private String resolveType(IType type, String signature) throws JavaModelException {
  final String[][] resolved = type.resolveType(Signature.toString(signature));
  if (resolved != null) {
    for (final String[] entry : resolved) {
      if (Strings.isNullOrEmpty(entry[0])) {
        return entry[1];
      }
      return entry[0] + "." + entry[1]; //$NON-NLS-1$
    }
  }
  return null;
}

代码示例来源:origin: org.eclipse.recommenders.completion/rcp

private static String[] computeParamTypes(CompletionProposal proposal) {
  // parameter types do not contain any ; and don't start with L:
  String[] paramTypes = Signature.getParameterTypes(String.valueOf(proposal.getSignature()));
  for (int index = 0; index < paramTypes.length; index++)
    paramTypes[index] = Signature.toString(paramTypes[index]);
  return paramTypes;
}

代码示例来源:origin: org.eclipse.jdt/org.eclipse.jdt.core

@Override
protected void toStringInfo(int tab, StringBuffer buffer, Object info, boolean showResolvedInfo) {
  buffer.append(tabString(tab));
  if (info != NO_INFO) {
    buffer.append(Signature.toString(getTypeSignature()));
    buffer.append(" "); //$NON-NLS-1$
  }
  toStringName(buffer);
}

代码示例来源:origin: eclipse/eclipse.jdt.ls

/**
 * Returns the simple name of the given type signature.
 *
 * @param enclosingElement the enclosing element in which to resolve the signature
 * @param typeSig a {@link Signature#CLASS_TYPE_SIGNATURE} or {@link Signature#TYPE_VARIABLE_SIGNATURE}
 * @return the simple name of the given type signature
 */
protected String getSimpleTypeName(IJavaElement enclosingElement, String typeSig) {
  return Signature.getSimpleName(Signature.toString(Signature.getTypeErasure(typeSig)));
}

代码示例来源:origin: org.eclipse.tycho/org.eclipse.jdt.core

protected void toStringInfo(int tab, StringBuffer buffer, Object info, boolean showResolvedInfo) {
  buffer.append(tabString(tab));
  if (info != NO_INFO) {
    buffer.append(Signature.toString(getTypeSignature()));
    buffer.append(" "); //$NON-NLS-1$
  }
  toStringName(buffer);
}

代码示例来源:origin: org.eclipse/org.eclipse.jdt.ui

protected String resolve(TemplateContext context) {
    IJavaElement element= ((CompilationUnitContext) context).findEnclosingElement(IJavaElement.METHOD);
    if (element == null)
      return null;
    try {
      return Signature.toString(((IMethod) element).getReturnType());
    } catch (JavaModelException e) {
      return null;
    }
  }
}

代码示例来源:origin: org.eclipse.jdt/org.eclipse.jdt.ui

@Override
  protected String resolve(TemplateContext context) {
    IJavaElement element= ((CompilationUnitContext) context).findEnclosingElement(IJavaElement.METHOD);
    if (element == null)
      return null;
    try {
      return Signature.toString(((IMethod) element).getReturnType());
    } catch (JavaModelException e) {
      return null;
    }
  }
}

代码示例来源:origin: org.eclipse/org.eclipse.jdt.ui

/**
 * Returns the fully qualified type name of the given signature, with any
 * type parameters and arrays erased.
 * 
 * @param signature the signature
 * @return the fully qualified type name of the signature
 */
public static String stripSignatureToFQN(String signature) throws IllegalArgumentException {
  signature= Signature.getTypeErasure(signature);
  signature= Signature.getElementType(signature);
  return Signature.toString(signature);
}

代码示例来源:origin: org.eclipse.jdt/org.eclipse.jdt.core.manipulation

/**
 * Returns the simple name of the given type signature.
 *
 * @param enclosingElement the enclosing element in which to resolve the signature
 * @param typeSig a {@link Signature#CLASS_TYPE_SIGNATURE} or {@link Signature#TYPE_VARIABLE_SIGNATURE}
 * @return the simple name of the given type signature
 */
protected String getSimpleTypeName(IJavaElement enclosingElement, String typeSig) {
  return Signature.getSimpleName(Signature.toString(Signature.getTypeErasure(typeSig)));
}

代码示例来源:origin: org.eclipse.scout.sdk.deps/org.eclipse.jdt.ui

/**
 * Returns the simple name of the given type signature.
 *
 * @param enclosingElement the enclosing element in which to resolve the signature
 * @param typeSig a {@link Signature#CLASS_TYPE_SIGNATURE} or {@link Signature#TYPE_VARIABLE_SIGNATURE}
 * @return the simple name of the given type signature
 */
protected String getSimpleTypeName(IJavaElement enclosingElement, String typeSig) {
  return Signature.getSimpleName(Signature.toString(Signature.getTypeErasure(typeSig)));
}

代码示例来源:origin: org.eclipse.scout.sdk.deps/org.eclipse.jdt.ui

@Override
  protected String resolve(TemplateContext context) {
    IJavaElement element= ((CompilationUnitContext) context).findEnclosingElement(IJavaElement.METHOD);
    if (element == null)
      return null;
    try {
      return Signature.toString(((IMethod) element).getReturnType());
    } catch (JavaModelException e) {
      return null;
    }
  }
}

代码示例来源:origin: org.eclipse.jdt/org.eclipse.jdt.ui

@Override
  protected String doGetValue(Field pi) {
    IField field= fDescriptor.getType().getField(pi.getFieldName());
    try {
      return Signature.toString(field.getTypeSignature());
    } catch (JavaModelException e) {
      return RefactoringMessages.ExtractClassWizard_not_available;
    }
  }
});

代码示例来源:origin: org.eclipse.scout.sdk.deps/org.eclipse.jdt.ui

@Override
  protected String doGetValue(Field pi) {
    IField field= fDescriptor.getType().getField(pi.getFieldName());
    try {
      return Signature.toString(field.getTypeSignature());
    } catch (JavaModelException e) {
      return RefactoringMessages.ExtractClassWizard_not_available;
    }
  }
});

代码示例来源:origin: org.eclipse/org.eclipse.jdt.ui

private String createFieldComment(IField field, String lineDelimiter) throws JavaModelException, CoreException {
  String typeName= Signature.toString(field.getTypeSignature());
  String fieldName= field.getElementName();
  return CodeGeneration.getFieldComment(field.getCompilationUnit(), typeName, fieldName, lineDelimiter);
}

代码示例来源:origin: org.eclipse/org.eclipse.jdt.ui

public static String createMethodSignature(IMethod method){
  try {
    return Signature.toString(method.getSignature(), method.getElementName(), method.getParameterNames(), false, ! method.isConstructor());
  } catch(JavaModelException e) {
    return method.getElementName(); //fallback
  }
}

代码示例来源:origin: org.eclipse.jdt/org.eclipse.jdt.junit.core

public static boolean hasSuiteMethod(IType type) throws JavaModelException {
  IMethod method= type.getMethod("suite", new String[0]); //$NON-NLS-1$
  if (!method.exists())
    return false;
  if (!Flags.isStatic(method.getFlags()) || !Flags.isPublic(method.getFlags())) {
    return false;
  }
  if (!Signature.getSimpleName(Signature.toString(method.getReturnType())).equals(JUnitCorePlugin.SIMPLE_TEST_INTERFACE_NAME)) {
    return false;
  }
  return true;
}

代码示例来源:origin: org.eclipse.jdt.junit/core

public static boolean hasSuiteMethod(IType type) throws JavaModelException {
  IMethod method= type.getMethod("suite", new String[0]); //$NON-NLS-1$
  if (!method.exists())
    return false;
  if (!Flags.isStatic(method.getFlags()) || !Flags.isPublic(method.getFlags())) {
    return false;
  }
  if (!Signature.getSimpleName(Signature.toString(method.getReturnType())).equals(JUnitCorePlugin.SIMPLE_TEST_INTERFACE_NAME)) {
    return false;
  }
  return true;
}

代码示例来源:origin: org.eclipse.pde/org.eclipse.pde.api.tools

@Override
protected String[] getMessageArgs(IReference reference) throws CoreException {
  IApiMethod method = (IApiMethod) reference.getResolvedReference();
  return new String[] {
      getSimpleTypeName(method),
      getSimpleTypeName(reference.getMember()),
      Signature.toString(method.getSignature(), method.getName(), null, false, false) };
}

代码示例来源:origin: eclipse/eclipse.jdt.ls

public static String createMethodSignature(IMethod method){
  try {
    return BasicElementLabels.getJavaElementName(Signature.toString(method.getSignature(), method.getElementName(), method.getParameterNames(), false, ! method.isConstructor()));
  } catch(JavaModelException e) {
    return BasicElementLabels.getJavaElementName(method.getElementName()); //fallback
  }
}

代码示例来源:origin: org.eclipse.jdt/org.eclipse.jdt.core.manipulation

public static String createMethodSignature(IMethod method){
  try {
    return BasicElementLabels.getJavaElementName(Signature.toString(method.getSignature(), method.getElementName(), method.getParameterNames(), false, ! method.isConstructor()));
  } catch(JavaModelException e) {
    return BasicElementLabels.getJavaElementName(method.getElementName()); //fallback
  }
}

相关文章