Java 类org.eclipse.jdt.core.ITypeParameter 实例源码

项目:eclemma    文件:SignatureResolver.java   
private static final boolean resolveTypeParameter(final IType context,
    final ITypeParameter[] typeParameters, final String identifier,
    final StringBuilder result) throws JavaModelException {
  for (final ITypeParameter p : typeParameters) {
    if (identifier.equals(p.getElementName())) {
      final String[] bounds = p.getBounds();
      if (bounds.length == 0) {
        result.append(OBJECT);
        return true;
      } else {
        return resolveType(context, bounds[0], result);
      }
    }
  }
  return false;
}
项目:eclipse.jdt.ls    文件:TypeProposalUtils.java   
static int mapTypeParameterIndex(IType[] path, int pathIndex, int paramIndex) throws JavaModelException, ArrayIndexOutOfBoundsException {
    if (pathIndex == 0) {
        // break condition: we've reached the top of the hierarchy
        return paramIndex;
    }

    IType subType= path[pathIndex];
    IType superType= path[pathIndex - 1];

    String superSignature= findMatchingSuperTypeSignature(subType, superType);
    ITypeParameter param= subType.getTypeParameters()[paramIndex];
    int index= findMatchingTypeArgumentIndex(superSignature, param.getElementName());
    if (index == -1) {
        // not mapped through
        return -1;
    }

    return mapTypeParameterIndex(path, pathIndex - 1, index);
}
项目:eclipse.jdt.ls    文件:CompletionProposalReplacementProvider.java   
private String computeTypeProposal(ITypeBinding binding, ITypeParameter parameter) throws JavaModelException {
    final String name = TypeProposalUtils.getTypeQualifiedName(binding);
    if (binding.isWildcardType()) {

        if (binding.isUpperbound()) {
            // replace the wildcard ? with the type parameter name to get "E extends Bound" instead of "? extends Bound"
            //              String contextName= name.replaceFirst("\\?", parameter.getElementName()); //$NON-NLS-1$
            // upper bound - the upper bound is the bound itself
            return binding.getBound().getName();
        }

        // no or upper bound - use the type parameter of the inserted type, as it may be more
        // restrictive (eg. List<?> list= new SerializableList<Serializable>())
        return computeTypeProposal(parameter);
    }

    // not a wildcard but a type or type variable - this is unambigously the right thing to insert
    return name;
}
项目:eclipse.jdt.ls    文件:JDTUtils.java   
private static ISourceRange getNameRange(IJavaElement element) throws JavaModelException {
    ISourceRange nameRange = null;
    if (element instanceof IMember) {
        IMember member = (IMember) element;
        nameRange = member.getNameRange();
        if ( (!SourceRange.isAvailable(nameRange))) {
            nameRange = member.getSourceRange();
        }
    } else if (element instanceof ITypeParameter || element instanceof ILocalVariable) {
        nameRange = ((ISourceReference) element).getNameRange();
    } else if (element instanceof ISourceReference) {
        nameRange = ((ISourceReference) element).getSourceRange();
    }
    if (!SourceRange.isAvailable(nameRange) && element.getParent() != null) {
        nameRange = getNameRange(element.getParent());
    }
    return nameRange;
}
项目:eclipse.jdt.ls    文件:JavaElementLabelComposer.java   
/**
 * Appends the styled label for a type parameter.
 *
 * @param typeParameter the element to render
 * @param flags the rendering flags. Flags with names starting with 'T_' are considered.
 */
public void appendTypeParameterLabel(ITypeParameter typeParameter, long flags) {
    try {
        appendTypeParameterWithBounds(typeParameter, flags);

        // post qualification
        if (getFlag(flags, JavaElementLabels.TP_POST_QUALIFIED)) {
            fBuilder.append(JavaElementLabels.CONCAT_STRING);
            IMember declaringMember= typeParameter.getDeclaringMember();
            appendElementLabel(declaringMember, JavaElementLabels.M_PARAMETER_TYPES | JavaElementLabels.M_FULLY_QUALIFIED | JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
        }

    } catch (JavaModelException e) {
        JavaLanguageServerPlugin.logException("", e); // NotExistsException will not reach this point
    }
}
项目:eclipse.jdt.ls    文件:JavaElementLabelComposer.java   
private void appendTypeParameterWithBounds(ITypeParameter typeParameter, long flags) throws JavaModelException {
    fBuilder.append(getElementName(typeParameter));

    if (typeParameter.exists()) {
        String[] bounds= typeParameter.getBoundsSignatures();
        if (bounds.length > 0 &&
                ! (bounds.length == 1 && "Ljava.lang.Object;".equals(bounds[0]))) { //$NON-NLS-1$
            fBuilder.append(" extends "); //$NON-NLS-1$
            for (int j= 0; j < bounds.length; j++) {
                if (j > 0) {
                    fBuilder.append(" & "); //$NON-NLS-1$
                }
                appendTypeSignatureLabel(typeParameter, bounds[j], flags);
            }
        }
    }
}
项目:Migrate-Skeletal-Implementation-to-Interface-Refactoring    文件:TypeVariableUtil.java   
/**
 * Creates a type variable mapping from a domain to a range.
 *
 * @param domain
 *            the domain of the mapping
 * @param range
 *            the range of the mapping
 * @return a possibly empty type variable mapping
 */
private static TypeVariableMaplet[] signaturesToParameters(final String[] domain, final ITypeParameter[] range) {
    Assert.isNotNull(domain);
    Assert.isNotNull(range);

    final List<TypeVariableMaplet> list = new ArrayList<TypeVariableMaplet>();
    String source = null;
    String target = null;
    for (int index = 0; index < Math.min(domain.length, range.length); index++) {
        source = Signature.toString(domain[index]);
        target = range[index].getElementName();
        list.add(new TypeVariableMaplet(source, index, target, index));
    }
    final TypeVariableMaplet[] result = new TypeVariableMaplet[list.size()];
    list.toArray(result);
    return result;
}
项目:Migrate-Skeletal-Implementation-to-Interface-Refactoring    文件:TypeVariableUtil.java   
/**
 * Returns a type variable mapping from a subclass to a superclass.
 *
 * @param subtype
 *            the type representing the subclass
 * @param supertype
 *            the type representing the superclass
 * @return a type variable mapping. The mapping entries consist of simple
 *         type variable names.
 * @throws JavaModelException
 *             if the signature of one of the types involved could not be
 *             retrieved
 */
public static TypeVariableMaplet[] subTypeToSuperType(final IType subtype, IType implementedInterface,
        final IType supertype) throws JavaModelException {
    Assert.isNotNull(subtype);
    Assert.isNotNull(supertype);

    final TypeVariableMaplet[] mapping = subTypeToInheritedType(subtype, implementedInterface);
    if (mapping.length > 0) {
        final ITypeParameter[] range = supertype.getTypeParameters();
        if (range.length > 0) {
            final String signature = subtype.getSuperclassTypeSignature();
            if (signature != null) {
                final String[] domain = getVariableSignatures(signature);
                if (domain.length > 0)
                    return composeMappings(mapping, signaturesToParameters(domain, range));
            }
        }
    }
    return mapping;
}
项目:che    文件:SourcesFromBytecodeGenerator.java   
private void appendTypeParameterWithBounds(
    ITypeParameter typeParameter, long flags, StringBuilder builder) throws JavaModelException {
  builder.append(getElementName(typeParameter));

  if (typeParameter.exists()) {
    String[] bounds = typeParameter.getBoundsSignatures();
    if (bounds.length > 0
        && !(bounds.length == 1 && "Ljava.lang.Object;".equals(bounds[0]))) { // $NON-NLS-1$
      builder.append(" extends "); // $NON-NLS-1$
      for (int j = 0; j < bounds.length; j++) {
        if (j > 0) {
          builder.append(" & "); // $NON-NLS-1$
        }
        appendTypeSignatureLabel(typeParameter, bounds[j], flags, builder);
      }
    }
  }
}
项目:che    文件:RefactoringManager.java   
private static RenameSupport createRenameSupport(IJavaElement element, String newName, int flags)
    throws CoreException {
  switch (element.getElementType()) {
    case IJavaElement.PACKAGE_FRAGMENT:
      return RenameSupport.create((IPackageFragment) element, newName, flags);
    case IJavaElement.COMPILATION_UNIT:
      return RenameSupport.create((ICompilationUnit) element, newName, flags);
    case IJavaElement.TYPE:
      return RenameSupport.create((IType) element, newName, flags);
    case IJavaElement.METHOD:
      final IMethod method = (IMethod) element;
      if (method.isConstructor())
        return createRenameSupport(method.getDeclaringType(), newName, flags);
      else return RenameSupport.create((IMethod) element, newName, flags);
    case IJavaElement.FIELD:
      return RenameSupport.create((IField) element, newName, flags);
    case IJavaElement.TYPE_PARAMETER:
      return RenameSupport.create((ITypeParameter) element, newName, flags);
    case IJavaElement.LOCAL_VARIABLE:
      return RenameSupport.create((ILocalVariable) element, newName, flags);
  }
  return null;
}
项目:che    文件:MethodOverrideTester.java   
private boolean hasCompatibleTypeParameters(IMethod overriding, IMethod overridden)
    throws JavaModelException {
  ITypeParameter[] overriddenTypeParameters = overridden.getTypeParameters();
  ITypeParameter[] overridingTypeParameters = overriding.getTypeParameters();
  int nOverridingTypeParameters = overridingTypeParameters.length;
  if (overriddenTypeParameters.length != nOverridingTypeParameters) {
    return nOverridingTypeParameters == 0;
  }
  Substitutions overriddenSubst = getMethodSubstitions(overridden);
  Substitutions overridingSubst = getMethodSubstitions(overriding);
  for (int i = 0; i < nOverridingTypeParameters; i++) {
    String erasure1 = overriddenSubst.getErasure(overriddenTypeParameters[i].getElementName());
    String erasure2 = overridingSubst.getErasure(overridingTypeParameters[i].getElementName());
    if (erasure1 == null || !erasure1.equals(erasure2)) {
      return false;
    }
    // comparing only the erasure is not really correct: Need to compare all bounds, that can be
    // in different order
    int nBounds = overriddenTypeParameters[i].getBounds().length;
    if (nBounds > 1 && nBounds != overridingTypeParameters[i].getBounds().length) {
      return false;
    }
  }
  return true;
}
项目:che    文件:MethodOverrideTester.java   
private Substitutions getMethodSubstitions(IMethod method) throws JavaModelException {
  if (fMethodSubstitutions == null) {
    fMethodSubstitutions = new LRUMap<IMethod, Substitutions>(3);
  }

  Substitutions s = fMethodSubstitutions.get(method);
  if (s == null) {
    ITypeParameter[] typeParameters = method.getTypeParameters();
    if (typeParameters.length == 0) {
      s = Substitutions.EMPTY_SUBST;
    } else {
      IType instantiatedType = method.getDeclaringType();
      s = new Substitutions();
      for (int i = 0; i < typeParameters.length; i++) {
        ITypeParameter curr = typeParameters[i];
        s.addSubstitution(
            curr.getElementName(),
            '+' + String.valueOf(i),
            getTypeParameterErasure(curr, instantiatedType));
      }
    }
    fMethodSubstitutions.put(method, s);
  }
  return s;
}
项目:che    文件:StubCreator.java   
protected void appendTypeParameters(final ITypeParameter[] parameters) throws JavaModelException {
  final int length = parameters.length;
  if (length > 0) fBuffer.append("<"); // $NON-NLS-1$
  for (int index = 0; index < length; index++) {
    if (index > 0) fBuffer.append(","); // $NON-NLS-1$
    final ITypeParameter parameter = parameters[index];
    fBuffer.append(parameter.getElementName());
    final String[] bounds = parameter.getBounds();
    final int size = bounds.length;
    if (size > 0) fBuffer.append(" extends "); // $NON-NLS-1$
    for (int offset = 0; offset < size; offset++) {
      if (offset > 0) fBuffer.append(" & "); // $NON-NLS-1$
      fBuffer.append(bounds[offset]);
    }
  }
  if (length > 0) fBuffer.append(">"); // $NON-NLS-1$
}
项目:che    文件:JavaMatchFilter.java   
private static boolean isParameterizedElement(IJavaElement element) {
  while (element != null) {
    ITypeParameter[] typeParameters = null;
    try {
      if (element instanceof IType) {
        typeParameters = ((IType) element).getTypeParameters();
      } else if (element instanceof IMethod) {
        typeParameters = ((IMethod) element).getTypeParameters();
      }
    } catch (JavaModelException e) {
      return false;
    }
    if (typeParameters == null) return false;

    if (typeParameters.length > 0) return true;

    element = element.getParent();
  }
  return false;
}
项目:che    文件:LazyGenericTypeProposal.java   
/**
 * Returns a type argument proposal for a given type binding. The proposal is:
 *
 * <ul>
 *   <li>the simple type name for normal types or type variables (unambigous proposal)
 *   <li>for wildcard types (ambigous proposals):
 *       <ul>
 *         <li>the upper bound for wildcards with an upper bound
 *         <li>the {@linkplain #computeTypeProposal(org.eclipse.jdt.core.ITypeParameter) parameter
 *             proposal} for unbounded wildcards or wildcards with a lower bound
 *       </ul>
 * </ul>
 *
 * @param binding the type argument binding in the expected type
 * @param parameter the type parameter of the inserted type
 * @return a type argument proposal for <code>binding</code>
 * @throws org.eclipse.jdt.core.JavaModelException if this element does not exist or if an
 *     exception occurs while accessing its corresponding resource
 * @see #computeTypeProposal(org.eclipse.jdt.core.ITypeParameter)
 */
private TypeArgumentProposal computeTypeProposal(ITypeBinding binding, ITypeParameter parameter)
    throws JavaModelException {
  final String name = Bindings.getTypeQualifiedName(binding);
  if (binding.isWildcardType()) {

    if (binding.isUpperbound()) {
      // replace the wildcard ? with the type parameter name to get "E extends Bound" instead of
      // "? extends Bound"
      String contextName = name.replaceFirst("\\?", parameter.getElementName()); // $NON-NLS-1$
      // upper bound - the upper bound is the bound itself
      return new TypeArgumentProposal(binding.getBound().getName(), true, contextName);
    }

    // no or upper bound - use the type parameter of the inserted type, as it may be more
    // restrictive (eg. List<?> list= new SerializableList<Serializable>())
    return computeTypeProposal(parameter);
  }

  // not a wildcard but a type or type variable - this is unambigously the right thing to insert
  return new TypeArgumentProposal(name, false, name);
}
项目:che    文件:LazyGenericTypeProposal.java   
/**
 * For the type parameter at <code>paramIndex</code> in the type at <code>path[pathIndex]</code> ,
 * this method computes the corresponding type parameter index in the type at <code>path[0]</code>
 * . If the type parameter does not map to a type parameter of the super type, <code>-1</code> is
 * returned.
 *
 * @param path the type inheritance path, a non-empty array of consecutive sub types
 * @param pathIndex an index into <code>path</code> specifying the type to start with
 * @param paramIndex the index of the type parameter to map - <code>path[pathIndex]</code> must
 *     have a type parameter at that index, lest an <code>ArrayIndexOutOfBoundsException</code> is
 *     thrown
 * @return the index of the type parameter in <code>path[0]</code> corresponding to the type
 *     parameter at <code>paramIndex</code> in <code>path[pathIndex]</code>, or -1 if there is no
 *     corresponding type parameter
 * @throws org.eclipse.jdt.core.JavaModelException if this element does not exist or if an
 *     exception occurs while accessing its corresponding resource
 * @throws ArrayIndexOutOfBoundsException if <code>path[pathIndex]</code> has &lt;= <code>
 *     paramIndex</code> parameters
 */
private int mapTypeParameterIndex(IType[] path, int pathIndex, int paramIndex)
    throws JavaModelException, ArrayIndexOutOfBoundsException {
  if (pathIndex == 0)
    // break condition: we've reached the top of the hierarchy
    return paramIndex;

  IType subType = path[pathIndex];
  IType superType = path[pathIndex - 1];

  String superSignature = findMatchingSuperTypeSignature(subType, superType);
  ITypeParameter param = subType.getTypeParameters()[paramIndex];
  int index = findMatchingTypeArgumentIndex(superSignature, param.getElementName());
  if (index == -1) {
    // not mapped through
    return -1;
  }

  return mapTypeParameterIndex(path, pathIndex - 1, index);
}
项目:che    文件:MethodProposalInfo.java   
/**
 * The type and method signatures received in <code>CompletionProposals</code> of type <code>
 * METHOD_REF</code> contain concrete type bounds. When comparing parameters of the signature with
 * an <code>IMethod</code>, we have to make sure that we match the case where the formal method
 * declaration uses a type variable which in the signature is already substituted with a concrete
 * type (bound).
 *
 * <p>This method creates a map from type variable names to type signatures based on the position
 * they appear in the type declaration. The type signatures are filtered through {@link
 * SignatureUtil#getLowerBound(char[])}.
 *
 * @param type the type to get the variables from
 * @return a map from type variables to concrete type signatures
 * @throws org.eclipse.jdt.core.JavaModelException if accessing the java model fails
 */
private Map<String, char[]> computeTypeVariables(IType type) throws JavaModelException {
  Map<String, char[]> map = new HashMap<String, char[]>();
  char[] declarationSignature = fProposal.getDeclarationSignature();
  if (declarationSignature == null) // array methods don't contain a declaration signature
  return map;
  char[][] concreteParameters = Signature.getTypeArguments(declarationSignature);

  ITypeParameter[] typeParameters = type.getTypeParameters();
  for (int i = 0; i < typeParameters.length; i++) {
    String variable = typeParameters[i].getElementName();
    if (concreteParameters.length > i)
      // use lower bound since method equality is only parameter based
      map.put(variable, SignatureUtil.getLowerBound(concreteParameters[i]));
    else
      // fProposal.getDeclarationSignature() is a raw type - use Object
      map.put(variable, "Ljava.lang.Object;".toCharArray()); // $NON-NLS-1$
  }

  return map;
}
项目:che    文件:JavaElementLabelComposer.java   
/**
 * Appends the styled label for a type parameter.
 *
 * @param typeParameter the element to render
 * @param flags the rendering flags. Flags with names starting with 'T_' are considered.
 */
public void appendTypeParameterLabel(ITypeParameter typeParameter, long flags) {
  try {
    appendTypeParameterWithBounds(typeParameter, flags);

    // post qualification
    if (getFlag(flags, JavaElementLabels.TP_POST_QUALIFIED)) {
      fBuffer.append(JavaElementLabels.CONCAT_STRING);
      IMember declaringMember = typeParameter.getDeclaringMember();
      appendElementLabel(
          declaringMember,
          JavaElementLabels.M_PARAMETER_TYPES
              | JavaElementLabels.M_FULLY_QUALIFIED
              | JavaElementLabels.T_FULLY_QUALIFIED
              | (flags & QUALIFIER_FLAGS));
    }

  } catch (JavaModelException e) {
    JavaPlugin.log(e); // NotExistsException will not reach this point
  }
}
项目:che    文件:JavaElementLabelComposer.java   
private void appendTypeParameterWithBounds(ITypeParameter typeParameter, long flags)
    throws JavaModelException {
  fBuffer.append(getElementName(typeParameter));

  if (typeParameter.exists()) {
    String[] bounds = typeParameter.getBoundsSignatures();
    if (bounds.length > 0
        && !(bounds.length == 1 && "Ljava.lang.Object;".equals(bounds[0]))) { // $NON-NLS-1$
      fBuffer.append(" extends "); // $NON-NLS-1$
      for (int j = 0; j < bounds.length; j++) {
        if (j > 0) {
          fBuffer.append(" & "); // $NON-NLS-1$
        }
        appendTypeSignatureLabel(typeParameter, bounds[j], flags);
      }
    }
  }
}
项目:vsminecraft    文件:JavaElementLabelComposer.java   
/**
 * Appends the styled label for a type parameter.
 *
 * @param typeParameter the element to render
 * @param flags the rendering flags. Flags with names starting with 'T_' are considered.
 */
public void appendTypeParameterLabel(ITypeParameter typeParameter, long flags) {
    try {
        appendTypeParameterWithBounds(typeParameter, flags);

        // post qualification
        if (getFlag(flags, TP_POST_QUALIFIED)) {
            fBuffer.append(CONCAT_STRING);
            IMember declaringMember= typeParameter.getDeclaringMember();
            appendElementLabel(declaringMember, M_PARAMETER_TYPES | M_FULLY_QUALIFIED | T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
        }

    } catch (JavaModelException e) {
        e.printStackTrace();
    }
}
项目:vsminecraft    文件:JavaElementLabelComposer.java   
private void appendTypeParameterWithBounds(ITypeParameter typeParameter, long flags) throws JavaModelException {
    fBuffer.append(getElementName(typeParameter));

    if (typeParameter.exists()) {
        String[] bounds= typeParameter.getBoundsSignatures();
        if (bounds.length > 0 &&
                ! (bounds.length == 1 && "Ljava.lang.Object;".equals(bounds[0]))) { //$NON-NLS-1$
            fBuffer.append(" extends "); //$NON-NLS-1$
            for (int j= 0; j < bounds.length; j++) {
                if (j > 0) {
                    fBuffer.append(" & "); //$NON-NLS-1$
                }
                appendTypeSignatureLabel(typeParameter, bounds[j], flags);
            }
        }
    }
}
项目:Eclipse-Postfix-Code-Completion    文件:TypeVariableUtil.java   
/**
 * Creates a type variable mapping from a domain to a range.
 *
 * @param domain
 *        the domain of the mapping
 * @param range
 *        the range of the mapping
 * @return a possibly empty type variable mapping
 */
private static TypeVariableMaplet[] signaturesToParameters(final String[] domain, final ITypeParameter[] range) {
    Assert.isNotNull(domain);
    Assert.isNotNull(range);
    Assert.isTrue(domain.length == 0 || domain.length == range.length);

    final List<TypeVariableMaplet> list= new ArrayList<TypeVariableMaplet>();
    String source= null;
    String target= null;
    for (int index= 0; index < domain.length; index++) {
        source= Signature.toString(domain[index]);
        target= range[index].getElementName();
        list.add(new TypeVariableMaplet(source, index, target, index));
    }
    final TypeVariableMaplet[] result= new TypeVariableMaplet[list.size()];
    list.toArray(result);
    return result;
}
项目:Eclipse-Postfix-Code-Completion    文件:TypeVariableUtil.java   
/**
 * Returns a type variable mapping from a subclass to a superclass.
 *
 * @param subtype
 *        the type representing the subclass
 * @param supertype
 *        the type representing the superclass
 * @return a type variable mapping. The mapping entries consist of simple type variable names.
 * @throws JavaModelException
 *         if the signature of one of the types involved could not be retrieved
 */
public static TypeVariableMaplet[] subTypeToSuperType(final IType subtype, final IType supertype) throws JavaModelException {
    Assert.isNotNull(subtype);
    Assert.isNotNull(supertype);

    final TypeVariableMaplet[] mapping= subTypeToInheritedType(subtype);
    if (mapping.length > 0) {
        final ITypeParameter[] range= supertype.getTypeParameters();
        if (range.length > 0) {
            final String signature= subtype.getSuperclassTypeSignature();
            if (signature != null) {
                final String[] domain= getVariableSignatures(signature);
                if (domain.length > 0)
                    return composeMappings(mapping, signaturesToParameters(domain, range));
            }
        }
    }
    return mapping;
}
项目:Eclipse-Postfix-Code-Completion    文件:TypeVariableUtil.java   
/**
 * Returns a type variable mapping from a superclass to a subclass.
 *
 * @param supertype
 *        the type representing the superclass
 * @param subtype
 *        the type representing the subclass
 * @return a type variable mapping. The mapping entries consist of simple type variable names.
 * @throws JavaModelException
 *         if the signature of one of the types involved could not be retrieved
 */
public static TypeVariableMaplet[] superTypeToInheritedType(final IType supertype, final IType subtype) throws JavaModelException {
    Assert.isNotNull(subtype);
    Assert.isNotNull(supertype);

    final ITypeParameter[] domain= supertype.getTypeParameters();
    if (domain.length > 0) {
        final String signature= subtype.getSuperclassTypeSignature();
        if (signature != null) {
            final String[] range= getVariableSignatures(signature);
            if (range.length > 0)
                return parametersToSignatures(domain, range, true);
        }
    }
    return new TypeVariableMaplet[0];
}
项目:Eclipse-Postfix-Code-Completion-Juno38    文件:MethodProposalInfo.java   
/**
 * The type and method signatures received in
 * <code>CompletionProposals</code> of type <code>METHOD_REF</code>
 * contain concrete type bounds. When comparing parameters of the signature
 * with an <code>IMethod</code>, we have to make sure that we match the
 * case where the formal method declaration uses a type variable which in
 * the signature is already substituted with a concrete type (bound).
 * <p>
 * This method creates a map from type variable names to type signatures
 * based on the position they appear in the type declaration. The type
 * signatures are filtered through
 * {@link SignatureUtil#getLowerBound(char[])}.
 * </p>
 *
 * @param type the type to get the variables from
 * @return a map from type variables to concrete type signatures
 * @throws JavaModelException if accessing the java model fails
 */
private Map<String, char[]> computeTypeVariables(IType type) throws JavaModelException {
    Map<String, char[]> map= new HashMap<String, char[]>();
    char[] declarationSignature= fProposal.getDeclarationSignature();
    if (declarationSignature == null) // array methods don't contain a declaration signature
        return map;
    char[][] concreteParameters= Signature.getTypeArguments(declarationSignature);

    ITypeParameter[] typeParameters= type.getTypeParameters();
    for (int i= 0; i < typeParameters.length; i++) {
        String variable= typeParameters[i].getElementName();
        if (concreteParameters.length > i)
            // use lower bound since method equality is only parameter based
            map.put(variable, SignatureUtil.getLowerBound(concreteParameters[i]));
        else
            // fProposal.getDeclarationSignature() is a raw type - use Object
            map.put(variable, "Ljava.lang.Object;".toCharArray()); //$NON-NLS-1$
    }

    return map;
}
项目:Eclipse-Postfix-Code-Completion    文件:StubCreator.java   
protected void appendTypeParameters(final ITypeParameter[] parameters) throws JavaModelException {
    final int length= parameters.length;
    if (length > 0)
        fBuffer.append("<"); //$NON-NLS-1$
    for (int index= 0; index < length; index++) {
        if (index > 0)
            fBuffer.append(","); //$NON-NLS-1$
        final ITypeParameter parameter= parameters[index];
        fBuffer.append(parameter.getElementName());
        final String[] bounds= parameter.getBounds();
        final int size= bounds.length;
        if (size > 0)
            fBuffer.append(" extends "); //$NON-NLS-1$
        for (int offset= 0; offset < size; offset++) {
            if (offset > 0)
                fBuffer.append(" & "); //$NON-NLS-1$
            fBuffer.append(bounds[offset]);
        }
    }
    if (length > 0)
        fBuffer.append(">"); //$NON-NLS-1$
}
项目:Eclipse-Postfix-Code-Completion    文件:MethodOverrideTester.java   
private boolean hasCompatibleTypeParameters(IMethod overriding, IMethod overridden) throws JavaModelException {
    ITypeParameter[] overriddenTypeParameters= overridden.getTypeParameters();
    ITypeParameter[] overridingTypeParameters= overriding.getTypeParameters();
    int nOverridingTypeParameters= overridingTypeParameters.length;
    if (overriddenTypeParameters.length != nOverridingTypeParameters) {
        return nOverridingTypeParameters == 0;
    }
    Substitutions overriddenSubst= getMethodSubstitions(overridden);
    Substitutions overridingSubst= getMethodSubstitions(overriding);
    for (int i= 0; i < nOverridingTypeParameters; i++) {
        String erasure1= overriddenSubst.getErasure(overriddenTypeParameters[i].getElementName());
        String erasure2= overridingSubst.getErasure(overridingTypeParameters[i].getElementName());
        if (erasure1 == null || !erasure1.equals(erasure2)) {
            return false;
        }
        // comparing only the erasure is not really correct: Need to compare all bounds, that can be in different order
        int nBounds= overriddenTypeParameters[i].getBounds().length;
        if (nBounds > 1 && nBounds != overridingTypeParameters[i].getBounds().length) {
            return false;
        }
    }
    return true;
}
项目:Eclipse-Postfix-Code-Completion    文件:MethodOverrideTester.java   
private Substitutions getMethodSubstitions(IMethod method) throws JavaModelException {
    if (fMethodSubstitutions == null) {
        fMethodSubstitutions= new LRUMap<IMethod, Substitutions>(3);
    }

    Substitutions s= fMethodSubstitutions.get(method);
    if (s == null) {
        ITypeParameter[] typeParameters= method.getTypeParameters();
        if (typeParameters.length == 0) {
            s= Substitutions.EMPTY_SUBST;
        } else {
            IType instantiatedType= method.getDeclaringType();
            s= new Substitutions();
            for (int i= 0; i < typeParameters.length; i++) {
                ITypeParameter curr= typeParameters[i];
                s.addSubstitution(curr.getElementName(), '+' + String.valueOf(i), getTypeParameterErasure(curr, instantiatedType));
            }
        }
        fMethodSubstitutions.put(method, s);
    }
    return s;
}
项目:Eclipse-Postfix-Code-Completion    文件:JavaMatchFilter.java   
private static boolean isParameterizedElement(IJavaElement element) {
    while (element != null) {
        ITypeParameter[] typeParameters= null;
        try {
            if (element instanceof IType) {
                typeParameters= ((IType)element).getTypeParameters();
            } else if (element instanceof IMethod) {
                typeParameters= ((IMethod)element).getTypeParameters();
            }
        } catch (JavaModelException e) {
            return false;
        }
        if (typeParameters == null)
            return false;

        if (typeParameters.length > 0)
            return true;

        element= element.getParent();
    }
    return false;
}
项目:Eclipse-Postfix-Code-Completion-Juno38    文件:JavaElementLabelComposer.java   
/**
 * Appends the styled label for a type parameter.
 *
 * @param typeParameter the element to render
 * @param flags the rendering flags. Flags with names starting with 'T_' are considered.
 */
public void appendTypeParameterLabel(ITypeParameter typeParameter, long flags) {
    try {
        appendTypeParameterWithBounds(typeParameter, flags);

        // post qualification
        if (getFlag(flags, JavaElementLabels.TP_POST_QUALIFIED)) {
            fBuffer.append(JavaElementLabels.CONCAT_STRING);
            IMember declaringMember= typeParameter.getDeclaringMember();
            appendElementLabel(declaringMember, JavaElementLabels.M_PARAMETER_TYPES | JavaElementLabels.M_FULLY_QUALIFIED | JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
        }

    } catch (JavaModelException e) {
        JavaPlugin.log(e); // NotExistsException will not reach this point
    }
}
项目:Eclipse-Postfix-Code-Completion    文件:MethodProposalInfo.java   
/**
 * The type and method signatures received in
 * <code>CompletionProposals</code> of type <code>METHOD_REF</code>
 * contain concrete type bounds. When comparing parameters of the signature
 * with an <code>IMethod</code>, we have to make sure that we match the
 * case where the formal method declaration uses a type variable which in
 * the signature is already substituted with a concrete type (bound).
 * <p>
 * This method creates a map from type variable names to type signatures
 * based on the position they appear in the type declaration. The type
 * signatures are filtered through
 * {@link SignatureUtil#getLowerBound(char[])}.
 * </p>
 *
 * @param type the type to get the variables from
 * @return a map from type variables to concrete type signatures
 * @throws JavaModelException if accessing the java model fails
 */
private Map<String, char[]> computeTypeVariables(IType type) throws JavaModelException {
    Map<String, char[]> map= new HashMap<String, char[]>();
    char[] declarationSignature= fProposal.getDeclarationSignature();
    if (declarationSignature == null) // array methods don't contain a declaration signature
        return map;
    char[][] concreteParameters= Signature.getTypeArguments(declarationSignature);

    ITypeParameter[] typeParameters= type.getTypeParameters();
    for (int i= 0; i < typeParameters.length; i++) {
        String variable= typeParameters[i].getElementName();
        if (concreteParameters.length > i)
            // use lower bound since method equality is only parameter based
            map.put(variable, SignatureUtil.getLowerBound(concreteParameters[i]));
        else
            // fProposal.getDeclarationSignature() is a raw type - use Object
            map.put(variable, "Ljava.lang.Object;".toCharArray()); //$NON-NLS-1$
    }

    return map;
}
项目:Eclipse-Postfix-Code-Completion    文件:JavaElementLabelComposer.java   
/**
 * Appends the styled label for a type parameter.
 *
 * @param typeParameter the element to render
 * @param flags the rendering flags. Flags with names starting with 'T_' are considered.
 */
public void appendTypeParameterLabel(ITypeParameter typeParameter, long flags) {
    try {
        appendTypeParameterWithBounds(typeParameter, flags);

        // post qualification
        if (getFlag(flags, JavaElementLabels.TP_POST_QUALIFIED)) {
            fBuffer.append(JavaElementLabels.CONCAT_STRING);
            IMember declaringMember= typeParameter.getDeclaringMember();
            appendElementLabel(declaringMember, JavaElementLabels.M_PARAMETER_TYPES | JavaElementLabels.M_FULLY_QUALIFIED | JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
        }

    } catch (JavaModelException e) {
        JavaPlugin.log(e); // NotExistsException will not reach this point
    }
}
项目:Eclipse-Postfix-Code-Completion    文件:JavaElementLabelComposer.java   
private void appendTypeParameterWithBounds(ITypeParameter typeParameter, long flags) throws JavaModelException {
    fBuffer.append(getElementName(typeParameter));

    if (typeParameter.exists()) {
        String[] bounds= typeParameter.getBoundsSignatures();
        if (bounds.length > 0 &&
                ! (bounds.length == 1 && "Ljava.lang.Object;".equals(bounds[0]))) { //$NON-NLS-1$
            fBuffer.append(" extends "); //$NON-NLS-1$
            for (int j= 0; j < bounds.length; j++) {
                if (j > 0) {
                    fBuffer.append(" & "); //$NON-NLS-1$
                }
                appendTypeSignatureLabel(typeParameter, bounds[j], flags);
            }
        }
    }
}
项目:Eclipse-Postfix-Code-Completion    文件:SuperInterfaceSelectionDialog.java   
public static String getNameWithTypeParameters(IType type) {
    String superName= type.getFullyQualifiedName('.');
    if (!JavaModelUtil.is50OrHigher(type.getJavaProject())) {
        return superName;
    }
    try {
        ITypeParameter[] typeParameters= type.getTypeParameters();
        if (typeParameters.length > 0) {
            StringBuffer buf= new StringBuffer(superName);
            buf.append('<');
            for (int k= 0; k < typeParameters.length; k++) {
                if (k != 0) {
                    buf.append(',').append(' ');
                }
                buf.append(typeParameters[k].getElementName());
            }
            buf.append('>');
            return buf.toString();
        }
    } catch (JavaModelException e) {
        // ignore
    }
    return superName;

}
项目:Eclipse-Postfix-Code-Completion    文件:TypeParameterPattern.java   
/**
 * @param findDeclarations
 * @param findReferences
 * @param typeParameter
 * @param matchRule
 */
public TypeParameterPattern(boolean findDeclarations, boolean findReferences, ITypeParameter typeParameter, int matchRule) {
    super(TYPE_PARAM_PATTERN, matchRule);

    this.findDeclarations = findDeclarations; // set to find declarations & all occurences
    this.findReferences = findReferences; // set to find references & all occurences
    this.typeParameter = typeParameter;
    this.name = typeParameter.getElementName().toCharArray(); // store type parameter name
    IMember member = typeParameter.getDeclaringMember();
    this.declaringMemberName = member.getElementName().toCharArray(); // store type parameter declaring member name

    // For method type parameter, store also declaring class name and parameters type names
    if (member instanceof IMethod) {
        IMethod method = (IMethod) member;
        this.methodDeclaringClassName = method.getParent().getElementName().toCharArray();
        String[] parameters = method.getParameterTypes();
        int length = parameters.length;
        this.methodArgumentTypes = new char[length][];
        for (int i=0; i<length; i++) {
            this.methodArgumentTypes[i] = Signature.toCharArray(parameters[i].toCharArray());
        }
    }
}
项目:Eclipse-Postfix-Code-Completion    文件:NamedMember.java   
private void appendTypeParameters(StringBuffer buffer) throws JavaModelException {
    ITypeParameter[] typeParameters = getTypeParameters();
    int length = typeParameters.length;
    if (length == 0) return;
    buffer.append('<');
    for (int i = 0; i < length; i++) {
        ITypeParameter typeParameter = typeParameters[i];
        buffer.append(typeParameter.getElementName());
        String[] bounds = typeParameter.getBounds();
        int boundsLength = bounds.length;
        if (boundsLength > 0) {
            buffer.append(" extends "); //$NON-NLS-1$
            for (int j = 0; j < boundsLength; j++) {
                buffer.append(bounds[j]);
                if (j < boundsLength-1)
                    buffer.append(" & "); //$NON-NLS-1$
            }
        }
        if (i < length-1)
            buffer.append(", "); //$NON-NLS-1$
    }
    buffer.append('>');
}
项目:Eclipse-Postfix-Code-Completion    文件:SelectionRequestor.java   
public void acceptLocalTypeParameter(TypeVariableBinding typeVariableBinding) {
    IJavaElement res;
    if(typeVariableBinding.declaringElement instanceof ParameterizedTypeBinding) {
        LocalTypeBinding localTypeBinding = (LocalTypeBinding)((ParameterizedTypeBinding)typeVariableBinding.declaringElement).genericType();
        res = findLocalElement(localTypeBinding.sourceStart());
    } else {
        SourceTypeBinding typeBinding = (SourceTypeBinding)typeVariableBinding.declaringElement;
        res = findLocalElement(typeBinding.sourceStart());
    }
    if (res != null && res.getElementType() == IJavaElement.TYPE) {
        IType type = (IType) res;
        ITypeParameter typeParameter = type.getTypeParameter(new String(typeVariableBinding.sourceName));
        if (typeParameter.exists()) {
            addElement(typeParameter);
            if(SelectionEngine.DEBUG){
                System.out.print("SELECTION - accept type parameter("); //$NON-NLS-1$
                System.out.print(typeParameter.toString());
                System.out.println(")"); //$NON-NLS-1$
            }
        }
    }
}
项目:Eclipse-Postfix-Code-Completion    文件:SelectionRequestor.java   
public void acceptLocalMethodTypeParameter(TypeVariableBinding typeVariableBinding) {
    MethodBinding methodBinding = (MethodBinding)typeVariableBinding.declaringElement;
    IJavaElement res = findLocalElement(methodBinding.sourceStart());
    if(res != null && res.getElementType() == IJavaElement.METHOD) {
        IMethod method = (IMethod) res;

        ITypeParameter typeParameter = method.getTypeParameter(new String(typeVariableBinding.sourceName));
        if (typeParameter.exists()) {
            addElement(typeParameter);
            if(SelectionEngine.DEBUG){
                System.out.print("SELECTION - accept type parameter("); //$NON-NLS-1$
                System.out.print(typeParameter.toString());
                System.out.println(")"); //$NON-NLS-1$
            }
        }
    }
}
项目:Eclipse-Postfix-Code-Completion-Juno38    文件:TypeVariableUtil.java   
/**
 * Returns a type variable mapping from a subclass to a superclass.
 *
 * @param subtype
 *        the type representing the subclass
 * @param supertype
 *        the type representing the superclass
 * @return a type variable mapping. The mapping entries consist of simple type variable names.
 * @throws JavaModelException
 *         if the signature of one of the types involved could not be retrieved
 */
public static TypeVariableMaplet[] subTypeToSuperType(final IType subtype, final IType supertype) throws JavaModelException {
    Assert.isNotNull(subtype);
    Assert.isNotNull(supertype);

    final TypeVariableMaplet[] mapping= subTypeToInheritedType(subtype);
    if (mapping.length > 0) {
        final ITypeParameter[] range= supertype.getTypeParameters();
        if (range.length > 0) {
            final String signature= subtype.getSuperclassTypeSignature();
            if (signature != null) {
                final String[] domain= getVariableSignatures(signature);
                if (domain.length > 0)
                    return composeMappings(mapping, signaturesToParameters(domain, range));
            }
        }
    }
    return mapping;
}
项目:Eclipse-Postfix-Code-Completion-Juno38    文件:TypeVariableUtil.java   
/**
 * Returns a type variable mapping from a superclass to a subclass.
 *
 * @param supertype
 *        the type representing the superclass
 * @param subtype
 *        the type representing the subclass
 * @return a type variable mapping. The mapping entries consist of simple type variable names.
 * @throws JavaModelException
 *         if the signature of one of the types involved could not be retrieved
 */
public static TypeVariableMaplet[] superTypeToInheritedType(final IType supertype, final IType subtype) throws JavaModelException {
    Assert.isNotNull(subtype);
    Assert.isNotNull(supertype);

    final ITypeParameter[] domain= supertype.getTypeParameters();
    if (domain.length > 0) {
        final String signature= subtype.getSuperclassTypeSignature();
        if (signature != null) {
            final String[] range= getVariableSignatures(signature);
            if (range.length > 0)
                return parametersToSignatures(domain, range, true);
        }
    }
    return new TypeVariableMaplet[0];
}