Java Code Examples for org.eclipse.jdt.core.Signature#CLASS_TYPE_SIGNATURE

The following examples show how to use org.eclipse.jdt.core.Signature#CLASS_TYPE_SIGNATURE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TypeURIHelper.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected void createFragment(String signature, StringBuilder uriBuilder) {
	int signatureKind = Signature.getTypeSignatureKind(signature);
	switch (signatureKind) {
		case Signature.BASE_TYPE_SIGNATURE:
			createFragmentForPrimitive(signature, uriBuilder);
			return;
		case Signature.CLASS_TYPE_SIGNATURE:
			createFragmentForClass(signature, uriBuilder);
			return;
		case Signature.ARRAY_TYPE_SIGNATURE:
			createFragmentForArray(signature, uriBuilder);
			return;
		case Signature.TYPE_VARIABLE_SIGNATURE:
			createFragmentForTypeVariable(signature, uriBuilder);
			return;
		default:
			throw new IllegalStateException("Unexpected Signature: " + signature);
	}
}
 
Example 2
Source File: TypeURIHelper.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected void createResourceURI(String signature, StringBuilder uriBuilder) {
	try {
		int signatureKind = Signature.getTypeSignatureKind(signature);
		switch (signatureKind) {
			case Signature.BASE_TYPE_SIGNATURE:
				createResourceURIForPrimitive(uriBuilder);
				return;
			case Signature.CLASS_TYPE_SIGNATURE:
				createResourceURIForClass(signature, uriBuilder);
				return;
			case Signature.ARRAY_TYPE_SIGNATURE:
				createResourceURIForArray(signature, uriBuilder);
				return;
			default:
				throw new IllegalStateException("Unexpected Signature: " + signature);
		}
	} catch(IllegalArgumentException e) {
		throw new IllegalArgumentException(e.getMessage() + " was: " + signature, e);
	}
}
 
Example 3
Source File: TypeURIHelper.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public void computeTypeName(String signature, StringBuilder uriBuilder) {
	int signatureKind = Signature.getTypeSignatureKind(signature);
	switch (signatureKind) {
		case Signature.CLASS_TYPE_SIGNATURE:
		case Signature.BASE_TYPE_SIGNATURE:
		case Signature.ARRAY_TYPE_SIGNATURE:
		case Signature.TYPE_VARIABLE_SIGNATURE:
			String erased = getTypeErasure(signature);
			uriBuilder.append(Signature.toString(erased));
			return;
		default:
			throw new IllegalStateException("Unexpected Signature: " + signature);
	}
}
 
Example 4
Source File: JavaElementFinder.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private void toQualifiedTypeName(String signature, IType context, StringBuilder result) throws JavaModelException {
	switch(Signature.getTypeSignatureKind(signature)) {
		case Signature.ARRAY_TYPE_SIGNATURE:
			toQualifiedTypeName(Signature.getElementType(signature), context, result);
			for(int i = 0; i < Signature.getArrayCount(signature); i++) {
				result.append("[]");
			}
			return;
		case Signature.CLASS_TYPE_SIGNATURE:
			if (signature.charAt(0) == Signature.C_UNRESOLVED) {
				String[][] resolved = context.resolveType(Signature.toString(signature));
				if (resolved != null && resolved.length == 1) {
					if (resolved[0][0] != null)
						result.append(resolved[0][0]);
					if (result.length() > 0)
						result.append('.');
					result.append(resolved[0][1]);
				} else {
					result.append(Signature.toString(signature));
				}
			} else {
				result.append(Signature.toString(signature));
			}
			return;
		default:
			result.append(Signature.toString(signature));
	}
}
 
Example 5
Source File: JsniJavaRefParamType.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
public static JsniJavaRefParamType parse(IPath source, int offset,
    String paramTypeString) {
  // Minimal class type signature is of form "LClassName;"
  if (paramTypeString.length() < 3 || !paramTypeString.endsWith(";")) {
    return null;
  }

  // We only consider class types (no primitives)
  if (Signature.getTypeSignatureKind(paramTypeString) != Signature.CLASS_TYPE_SIGNATURE) {
    return null;
  }

  return new JsniJavaRefParamType(source, offset, paramTypeString);
}
 
Example 6
Source File: CompilationUnitCompletion.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns <code>true</code> if <code>subTypeSignature</code>
 * describes a type which is a true sub type of the type described by
 * <code>superTypeSignature</code>.
 *
 * @param subTypeSignature the potential subtype's signature
 * @param superTypeSignature the potential supertype's signature
 * @return <code>true</code> if the inheritance relationship holds
 */
private boolean isTrueSubtypeOf(String subTypeSignature, String superTypeSignature) {
	// try cheap test first
	if (subTypeSignature.equals(superTypeSignature))
		return true;

	if (SignatureUtil.isJavaLangObject(subTypeSignature))
		return false; // Object has no super types

	if (Signature.getTypeSignatureKind(subTypeSignature) != Signature.BASE_TYPE_SIGNATURE && SignatureUtil.isJavaLangObject(superTypeSignature))
		return true;

	IJavaProject project= fUnit.getJavaProject();

	try {

		if ((Signature.getTypeSignatureKind(subTypeSignature) & (Signature.TYPE_VARIABLE_SIGNATURE | Signature.CLASS_TYPE_SIGNATURE)) == 0)
			return false;
		IType subType= project.findType(SignatureUtil.stripSignatureToFQN(subTypeSignature));
		if (subType == null)
			return false;

		if ((Signature.getTypeSignatureKind(superTypeSignature) & (Signature.TYPE_VARIABLE_SIGNATURE | Signature.CLASS_TYPE_SIGNATURE)) == 0)
			return false;
		IType superType= project.findType(SignatureUtil.stripSignatureToFQN(superTypeSignature));
		if (superType == null)
			return false;

		ITypeHierarchy hierarchy= subType.newSupertypeHierarchy(null);
		IType[] types= hierarchy.getAllSupertypes(subType);

		for (int i= 0; i < types.length; i++)
			if (types[i].equals(superType))
				return true;
	} catch (JavaModelException e) {
		// ignore and return false
	}

	return false;
}
 
Example 7
Source File: JavaElementReturnTypeHyperlink.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public void open() {
	try {
		String returnTypeSignature= fMethod.getReturnType();
		int kind= Signature.getTypeSignatureKind(returnTypeSignature);
		if (kind == Signature.ARRAY_TYPE_SIGNATURE) {
			returnTypeSignature= Signature.getElementType(returnTypeSignature);
		} else if (kind == Signature.CLASS_TYPE_SIGNATURE) {
			returnTypeSignature= Signature.getTypeErasure(returnTypeSignature);
		}
		String returnType= Signature.toString(returnTypeSignature);

		String[][] resolvedType= fMethod.getDeclaringType().resolveType(returnType);
		if (resolvedType == null || resolvedType.length == 0) {
			openMethodAndShowErrorInStatusLine();
			return;
		}

		String typeName= JavaModelUtil.concatenateName(resolvedType[0][0], resolvedType[0][1]);
		IType type= fMethod.getJavaProject().findType(typeName, (IProgressMonitor)null);
		if (type != null) {
			fOpenAction.run(new StructuredSelection(type));
			return;
		}
		openMethodAndShowErrorInStatusLine();
	} catch (JavaModelException e) {
		JavaPlugin.log(e);
		return;
	}
}
 
Example 8
Source File: JavaElementLabelComposer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
protected void appendTypeSignatureLabel(IJavaElement enclosingElement, String typeSig, long flags) {
	int sigKind= Signature.getTypeSignatureKind(typeSig);
	switch (sigKind) {
	case Signature.BASE_TYPE_SIGNATURE:
		fBuilder.append(Signature.toString(typeSig));
		break;
	case Signature.ARRAY_TYPE_SIGNATURE:
		appendTypeSignatureLabel(enclosingElement, Signature.getElementType(typeSig), flags);
		for (int dim= Signature.getArrayCount(typeSig); dim > 0; dim--) {
			fBuilder.append('[').append(']');
		}
		break;
	case Signature.CLASS_TYPE_SIGNATURE:
		String baseType= getSimpleTypeName(enclosingElement, typeSig);
		fBuilder.append(baseType);

		String[] typeArguments= Signature.getTypeArguments(typeSig);
		appendTypeArgumentSignaturesLabel(enclosingElement, typeArguments, flags);
		break;
	case Signature.TYPE_VARIABLE_SIGNATURE:
		fBuilder.append(getSimpleTypeName(enclosingElement, typeSig));
		break;
	case Signature.WILDCARD_TYPE_SIGNATURE:
		char ch= typeSig.charAt(0);
		if (ch == Signature.C_STAR) { //workaround for bug 85713
			fBuilder.append('?');
		} else {
			if (ch == Signature.C_EXTENDS) {
				fBuilder.append("? extends "); //$NON-NLS-1$
				appendTypeSignatureLabel(enclosingElement, typeSig.substring(1), flags);
			} else if (ch == Signature.C_SUPER) {
				fBuilder.append("? super "); //$NON-NLS-1$
				appendTypeSignatureLabel(enclosingElement, typeSig.substring(1), flags);
			}
		}
		break;
	case Signature.CAPTURE_TYPE_SIGNATURE:
		appendTypeSignatureLabel(enclosingElement, typeSig.substring(1), flags);
		break;
	case Signature.INTERSECTION_TYPE_SIGNATURE:
		String[] typeBounds= Signature.getIntersectionTypeBounds(typeSig);
		appendTypeBoundsSignaturesLabel(enclosingElement, typeBounds, flags);
		break;
	default:
		// unknown
	}
}
 
Example 9
Source File: JavaTypeCompletionProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void accept(CompletionProposal proposal) {
	switch (proposal.getKind()) {
		case CompletionProposal.PACKAGE_REF :
			char[] packageName= proposal.getDeclarationSignature();
			if (TypeFilter.isFiltered(packageName))
				return;
			addAdjustedCompletion(
					new String(packageName),
					new String(proposal.getCompletion()),
					proposal.getReplaceStart(),
					proposal.getReplaceEnd(),
					proposal.getRelevance(),
					JavaPluginImages.DESC_OBJS_PACKAGE);
			return;

		case CompletionProposal.TYPE_REF :
			char[] signature= proposal.getSignature();
			char[] fullName= Signature.toCharArray(signature);
			if (TypeFilter.isFiltered(fullName))
				return;
			StringBuffer buf= new StringBuffer();
			buf.append(Signature.getSimpleName(fullName));
			if (buf.length() == 0)
				return; // this is the dummy class, whose $ have been converted to dots
			char[] typeQualifier= Signature.getQualifier(fullName);
			if (typeQualifier.length > 0) {
				buf.append(JavaElementLabels.CONCAT_STRING);
				buf.append(typeQualifier);
			}
			String name= buf.toString();

			// Only fully qualify if it's a top level type:
			boolean fullyQualify= fFullyQualify && CharOperation.equals(proposal.getDeclarationSignature(), typeQualifier);

			ImageDescriptor typeImageDescriptor;
			switch (Signature.getTypeSignatureKind(signature)) {
				case Signature.TYPE_VARIABLE_SIGNATURE :
					typeImageDescriptor= JavaPluginImages.DESC_OBJS_TYPEVARIABLE;
					break;
				case Signature.CLASS_TYPE_SIGNATURE :
					typeImageDescriptor= JavaElementImageProvider.getTypeImageDescriptor(false, false, proposal.getFlags(), false);
					break;
				default :
					typeImageDescriptor= null;
			}

			addAdjustedTypeCompletion(
					name,
					new String(proposal.getCompletion()),
					proposal.getReplaceStart(),
					proposal.getReplaceEnd(),
					proposal.getRelevance(),
					typeImageDescriptor,
					fullyQualify ? new String(fullName) : null);
			return;

		case CompletionProposal.KEYWORD:
			if (! fEnableBaseTypes)
				return;
			String keyword= new String(proposal.getName());
			if ( (fEnableVoid && VOID.equals(keyword)) || (fEnableBaseTypes && BASE_TYPES.contains(keyword)) )
				addAdjustedCompletion(
						keyword,
						new String(proposal.getCompletion()),
						proposal.getReplaceStart(),
						proposal.getReplaceEnd(),
						proposal.getRelevance(),
						null);
			return;

		default :
			return;
	}

}
 
Example 10
Source File: CompletionProposalLabelProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Creates and returns a decorated image descriptor for a completion proposal.
 *
 * @param proposal the proposal for which to create an image descriptor
 * @return the created image descriptor, or <code>null</code> if no image is available
 */
public ImageDescriptor createImageDescriptor(CompletionProposal proposal) {
	final int flags= proposal.getFlags();

	ImageDescriptor descriptor;
	switch (proposal.getKind()) {
		case CompletionProposal.METHOD_DECLARATION:
		case CompletionProposal.METHOD_NAME_REFERENCE:
		case CompletionProposal.METHOD_REF:
		case CompletionProposal.CONSTRUCTOR_INVOCATION:
		case CompletionProposal.METHOD_REF_WITH_CASTED_RECEIVER:
		case CompletionProposal.ANNOTATION_ATTRIBUTE_REF:
		case CompletionProposal.POTENTIAL_METHOD_DECLARATION:
		case CompletionProposal.ANONYMOUS_CLASS_DECLARATION:
		case CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION:
			descriptor= JavaElementImageProvider.getMethodImageDescriptor(false, flags);
			break;
		case CompletionProposal.TYPE_REF:
			switch (Signature.getTypeSignatureKind(proposal.getSignature())) {
				case Signature.CLASS_TYPE_SIGNATURE:
					descriptor= JavaElementImageProvider.getTypeImageDescriptor(false, false, flags, false);
					break;
				case Signature.TYPE_VARIABLE_SIGNATURE:
					descriptor= JavaPluginImages.DESC_OBJS_TYPEVARIABLE;
					break;
				default:
					descriptor= null;
			}
			break;
		case CompletionProposal.FIELD_REF:
		case CompletionProposal.FIELD_REF_WITH_CASTED_RECEIVER:
			descriptor= JavaElementImageProvider.getFieldImageDescriptor(false, flags);
			break;
		case CompletionProposal.LOCAL_VARIABLE_REF:
		case CompletionProposal.VARIABLE_DECLARATION:
			descriptor= JavaPluginImages.DESC_OBJS_LOCAL_VARIABLE;
			break;
		case CompletionProposal.PACKAGE_REF:
			descriptor= JavaPluginImages.DESC_OBJS_PACKAGE;
			break;
		case CompletionProposal.KEYWORD:
		case CompletionProposal.LABEL_REF:
			descriptor= null;
			break;
		case CompletionProposal.JAVADOC_METHOD_REF:
		case CompletionProposal.JAVADOC_TYPE_REF:
		case CompletionProposal.JAVADOC_FIELD_REF:
		case CompletionProposal.JAVADOC_VALUE_REF:
		case CompletionProposal.JAVADOC_BLOCK_TAG:
		case CompletionProposal.JAVADOC_INLINE_TAG:
		case CompletionProposal.JAVADOC_PARAM_REF:
			descriptor = JavaPluginImages.DESC_OBJS_JAVADOCTAG;
			break;
		default:
			descriptor= null;
			Assert.isTrue(false);
	}

	if (descriptor == null)
		return null;
	return decorateImageDescriptor(descriptor, proposal);
}
 
Example 11
Source File: JavaElementLabelComposer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected void appendTypeSignatureLabel(IJavaElement enclosingElement, String typeSig, long flags) {
	int sigKind= Signature.getTypeSignatureKind(typeSig);
	switch (sigKind) {
		case Signature.BASE_TYPE_SIGNATURE:
			fBuffer.append(Signature.toString(typeSig));
			break;
		case Signature.ARRAY_TYPE_SIGNATURE:
			appendTypeSignatureLabel(enclosingElement, Signature.getElementType(typeSig), flags);
			for (int dim= Signature.getArrayCount(typeSig); dim > 0; dim--) {
				fBuffer.append('[').append(']');
			}
			break;
		case Signature.CLASS_TYPE_SIGNATURE:
			String baseType= getSimpleTypeName(enclosingElement, typeSig);
			fBuffer.append(baseType);

			String[] typeArguments= Signature.getTypeArguments(typeSig);
			appendTypeArgumentSignaturesLabel(enclosingElement, typeArguments, flags);
			break;
		case Signature.TYPE_VARIABLE_SIGNATURE:
			fBuffer.append(getSimpleTypeName(enclosingElement, typeSig));
			break;
		case Signature.WILDCARD_TYPE_SIGNATURE:
			char ch= typeSig.charAt(0);
			if (ch == Signature.C_STAR) { //workaround for bug 85713
				fBuffer.append('?');
			} else {
				if (ch == Signature.C_EXTENDS) {
					fBuffer.append("? extends "); //$NON-NLS-1$
					appendTypeSignatureLabel(enclosingElement, typeSig.substring(1), flags);
				} else if (ch == Signature.C_SUPER) {
					fBuffer.append("? super "); //$NON-NLS-1$
					appendTypeSignatureLabel(enclosingElement, typeSig.substring(1), flags);
				}
			}
			break;
		case Signature.CAPTURE_TYPE_SIGNATURE:
			appendTypeSignatureLabel(enclosingElement, typeSig.substring(1), flags);
			break;
		case Signature.INTERSECTION_TYPE_SIGNATURE:
			String[] typeBounds= Signature.getIntersectionTypeBounds(typeSig);
			appendTypeBoundsSignaturesLabel(enclosingElement, typeBounds, flags);
			break;
		default:
			// unknown
	}
}
 
Example 12
Source File: ImportRewrite.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Adds a new import to the rewriter's record and returns a {@link Type} node that can be used
 * in the code as a reference to the type. The type binding can be an array binding, type variable or wildcard.
 * If the binding is a generic type, the type parameters are ignored. For parameterized types, also the type
 * arguments are processed and imports added if necessary. Anonymous types inside type arguments are normalized to their base type, wildcard
 * of wildcards are ignored.
 * 	<p>
	 * No imports are added for types that are already known. If a import for a type is recorded to be removed, this record is discarded instead.
 * </p>
 * <p>
 * The content of the compilation unit itself is actually not modified
 * in any way by this method; rather, the rewriter just records that a new import has been added.
 * </p>
 * @param typeSig the signature of the type to be added.
 * @param ast the AST to create the returned type for.
 * @param context an optional context that knows about types visible in the current scope or <code>null</code>
 * to use the default context only using the available imports.
 * @return a type node for the given type signature. Type names are simple names if an import could be used,
 * or else qualified names if an import conflict prevented an import.
 */
public Type addImportFromSignature(String typeSig, AST ast, ImportRewriteContext context) {
	if (typeSig == null || typeSig.length() == 0) {
		throw new IllegalArgumentException("Invalid type signature: empty or null"); //$NON-NLS-1$
	}
	int sigKind= Signature.getTypeSignatureKind(typeSig);
	switch (sigKind) {
		case Signature.BASE_TYPE_SIGNATURE:
			return ast.newPrimitiveType(PrimitiveType.toCode(Signature.toString(typeSig)));
		case Signature.ARRAY_TYPE_SIGNATURE:
			Type elementType= addImportFromSignature(Signature.getElementType(typeSig), ast, context);
			return ast.newArrayType(elementType, Signature.getArrayCount(typeSig));
		case Signature.CLASS_TYPE_SIGNATURE:
			String erasureSig= Signature.getTypeErasure(typeSig);

			String erasureName= Signature.toString(erasureSig);
			if (erasureSig.charAt(0) == Signature.C_RESOLVED) {
				erasureName= internalAddImport(erasureName, context);
			}
			Type baseType= ast.newSimpleType(ast.newName(erasureName));
			String[] typeArguments= Signature.getTypeArguments(typeSig);
			if (typeArguments.length > 0) {
				ParameterizedType type= ast.newParameterizedType(baseType);
				List argNodes= type.typeArguments();
				for (int i= 0; i < typeArguments.length; i++) {
					String curr= typeArguments[i];
					if (containsNestedCapture(curr)) { // see bug 103044
						argNodes.add(ast.newWildcardType());
					} else {
						argNodes.add(addImportFromSignature(curr, ast, context));
					}
				}
				return type;
			}
			return baseType;
		case Signature.TYPE_VARIABLE_SIGNATURE:
			return ast.newSimpleType(ast.newSimpleName(Signature.toString(typeSig)));
		case Signature.WILDCARD_TYPE_SIGNATURE:
			WildcardType wildcardType= ast.newWildcardType();
			char ch= typeSig.charAt(0);
			if (ch != Signature.C_STAR) {
				Type bound= addImportFromSignature(typeSig.substring(1), ast, context);
				wildcardType.setBound(bound, ch == Signature.C_EXTENDS);
			}
			return wildcardType;
		case Signature.CAPTURE_TYPE_SIGNATURE:
			return addImportFromSignature(typeSig.substring(1), ast, context);
		default:
			throw new IllegalArgumentException("Unknown type signature kind: " + typeSig); //$NON-NLS-1$
	}
}
 
Example 13
Source File: JavaStatementPostfixContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private Type createType(String typeSig, AST ast) {
	int sigKind = Signature.getTypeSignatureKind(typeSig);
       switch (sigKind) {
           case Signature.BASE_TYPE_SIGNATURE:
               return ast.newPrimitiveType(PrimitiveType.toCode(Signature.toString(typeSig)));
           case Signature.ARRAY_TYPE_SIGNATURE:
               Type elementType = createType(Signature.getElementType(typeSig), ast);
               return ast.newArrayType(elementType, Signature.getArrayCount(typeSig));
           case Signature.CLASS_TYPE_SIGNATURE:
               String erasureSig = Signature.getTypeErasure(typeSig);

               String erasureName = Signature.toString(erasureSig);
               if (erasureSig.charAt(0) == Signature.C_RESOLVED) {
                   erasureName = addImport(erasureName);
               }
               
               Type baseType= ast.newSimpleType(ast.newName(erasureName));
               String[] typeArguments = Signature.getTypeArguments(typeSig);
               if (typeArguments.length > 0) {
                   ParameterizedType type = ast.newParameterizedType(baseType);
                   List argNodes = type.typeArguments();
                   for (int i = 0; i < typeArguments.length; i++) {
                       String curr = typeArguments[i];
                       if (containsNestedCapture(curr)) {
                           argNodes.add(ast.newWildcardType());
                       } else {
                           argNodes.add(createType(curr, ast));
                       }
                   }
                   return type;
               }
               return baseType;
           case Signature.TYPE_VARIABLE_SIGNATURE:
               return ast.newSimpleType(ast.newSimpleName(Signature.toString(typeSig)));
           case Signature.WILDCARD_TYPE_SIGNATURE:
               WildcardType wildcardType= ast.newWildcardType();
               char ch = typeSig.charAt(0);
               if (ch != Signature.C_STAR) {
                   Type bound= createType(typeSig.substring(1), ast);
                   wildcardType.setBound(bound, ch == Signature.C_EXTENDS);
               }
               return wildcardType;
           case Signature.CAPTURE_TYPE_SIGNATURE:
               return createType(typeSig.substring(1), ast);
       }
       
       return ast.newSimpleType(ast.newName("java.lang.Object"));
}