Java Code Examples for org.eclipse.jdt.core.Signature#createTypeSignature()

The following examples show how to use org.eclipse.jdt.core.Signature#createTypeSignature() . 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: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private IMethod findJavaMethod(IType type) {
	// Locate the method from its signature.
	//
	String[] parameterTypes = Signature.getParameterTypes(new BindingKey("Lx;.x(" + signature + ")").toSignature());
	IMethod javaMethod = type.getMethod(name, parameterTypes);

	// If the method doesn't exist and this is a nested type...
	//
	if (!javaMethod.exists() && type.getDeclaringType() != null) {
		// This special case handles what appears to be a JDT bug 
		// that sometimes it knows when there are implicit constructor arguments for nested types and sometimes it doesn't.
		// Infer one more initial parameter type and locate the method based on that.
		//
		String[] augmented = new String[parameterTypes.length + 1];
		System.arraycopy(parameterTypes, 0, augmented, 1, parameterTypes.length);
		String first = Signature.createTypeSignature(type.getDeclaringType().getFullyQualifiedName(), true);
		augmented[0] = first;
		javaMethod = type.getMethod(name, augmented);
	}
	return javaMethod;
}
 
Example 2
Source File: RefactoringHandleTransplanter.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private String[] resolveParameterTypes(IMethod method) {
	final String[] oldParameterTypes= method.getParameterTypes();
	final String[] newparams= new String[oldParameterTypes.length];

	final String[] possibleOldSigs= new String[4];
	possibleOldSigs[0]= Signature.createTypeSignature(fOldType.getElementName(), false);
	possibleOldSigs[1]= Signature.createTypeSignature(fOldType.getElementName(), true);
	possibleOldSigs[2]= Signature.createTypeSignature(fOldType.getFullyQualifiedName(), false);
	possibleOldSigs[3]= Signature.createTypeSignature(fOldType.getFullyQualifiedName(), true);

	final String[] possibleNewSigs= new String[4];
	possibleNewSigs[0]= Signature.createTypeSignature(fNewType.getElementName(), false);
	possibleNewSigs[1]= Signature.createTypeSignature(fNewType.getElementName(), true);
	possibleNewSigs[2]= Signature.createTypeSignature(fNewType.getFullyQualifiedName(), false);
	possibleNewSigs[3]= Signature.createTypeSignature(fNewType.getFullyQualifiedName(), true);

	// Textually replace all occurrences
	// This handles stuff like Map<SomeClass, some.package.SomeClass>
	for (int i= 0; i < oldParameterTypes.length; i++) {
		newparams[i]= oldParameterTypes[i];
		for (int j= 0; j < possibleOldSigs.length; j++) {
			newparams[i]= replaceAll(newparams[i], possibleOldSigs[j], possibleNewSigs[j]);
		}
	}
	return newparams;
}
 
Example 3
Source File: PairedMethodRenameParticipant.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Computes the paired method enclosed in the paired type contained within the
 * given <code>typeContainer</code>.
 * 
 * @param typeContainer the container for the current type and paired type
 * @param method a method that will be used to form a signature used during
 *          the lookup
 * @return the paired method, or null if one could not be found
 * @throws RemoteServiceException
 * @throws JavaModelException
 */
private static IMethod computePairedMethod(TypeContainer typeContainer,
    IMethod method) throws RemoteServiceException, JavaModelException {
  String[] paramTypeNames = null;
  if (typeContainer.isSync()) {
    paramTypeNames = RemoteServiceUtilities.computeAsyncParameterTypes(method);
  } else {
    paramTypeNames = RemoteServiceUtilities.computeSyncParameterTypes(method);
  }

  // We have qualified parameter type names, but need qualified parameter
  // type signatures
  String[] paramTypeSigs = new String[paramTypeNames.length];
  for (int i = 0; i < paramTypeNames.length; i++) {
    paramTypeSigs[i] = Signature.createTypeSignature(paramTypeNames[i], true);
  }

  IType pairedType = typeContainer.getPairedType();
  return JavaModelSearch.findMethodInHierarchy(
      pairedType.newSupertypeHierarchy(new NullProgressMonitor()),
      pairedType, method.getElementName(), paramTypeSigs);
}
 
Example 4
Source File: RefactoringHandleTransplanter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private String[] resolveParameterTypes(IMethod method) {
	final String[] oldParameterTypes= method.getParameterTypes();
	final String[] newparams= new String[oldParameterTypes.length];

	final String[] possibleOldSigs= new String[4];
	possibleOldSigs[0]= Signature.createTypeSignature(fOldType.getElementName(), false);
	possibleOldSigs[1]= Signature.createTypeSignature(fOldType.getElementName(), true);
	possibleOldSigs[2]= Signature.createTypeSignature(fOldType.getFullyQualifiedName(), false);
	possibleOldSigs[3]= Signature.createTypeSignature(fOldType.getFullyQualifiedName(), true);

	final String[] possibleNewSigs= new String[4];
	possibleNewSigs[0]= Signature.createTypeSignature(fNewType.getElementName(), false);
	possibleNewSigs[1]= Signature.createTypeSignature(fNewType.getElementName(), true);
	possibleNewSigs[2]= Signature.createTypeSignature(fNewType.getFullyQualifiedName(), false);
	possibleNewSigs[3]= Signature.createTypeSignature(fNewType.getFullyQualifiedName(), true);

	// Textually replace all occurrences
	// This handles stuff like Map<SomeClass, some.package.SomeClass>
	for (int i= 0; i < oldParameterTypes.length; i++) {
		newparams[i]= oldParameterTypes[i];
		for (int j= 0; j < possibleOldSigs.length; j++) {
			newparams[i]= replaceAll(newparams[i], possibleOldSigs[j], possibleNewSigs[j]);
		}
	}
	return newparams;
}
 
Example 5
Source File: CompilationUnitCompletion.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Resets the completion requester.
 *
 * @param unit the compilation unit, may be <code>null</code>.
 */
private void reset(ICompilationUnit unit) {
	fUnit= unit;
	fLocalVariables.clear();
	fFields.clear();
	fLocalTypes.clear();

	if (fUnit != null) {
		try {
			IType[] cuTypes= fUnit.getAllTypes();
			for (int i= 0; i < cuTypes.length; i++) {
				String fqn= cuTypes[i].getFullyQualifiedName();
				String sig= Signature.createTypeSignature(fqn, true);
				fLocalTypes.put(sig, cuTypes[i].getElementName());
			}
		} catch (JavaModelException e) {
			// ignore
		}
	}
	fError= false;
}
 
Example 6
Source File: DOMMethod.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * @see IDOMNode#getJavaElement
 */
public IJavaElement getJavaElement(IJavaElement parent) throws IllegalArgumentException {
	if (parent.getElementType() == IJavaElement.TYPE) {
		// translate parameter types to signatures
		String[] sigs= null;
		if (this.fParameterTypes != null) {
			sigs= new String[this.fParameterTypes.length];
			int i;
			for (i= 0; i < this.fParameterTypes.length; i++) {
				sigs[i]= Signature.createTypeSignature(this.fParameterTypes[i].toCharArray(), false);
			}
		}
		String name= null;
		if (isConstructor()) {
			name= getConstructorName();
		} else {
			name= getName();
		}
		return ((IType)parent).getMethod(name, sigs);
	} else {
		throw new IllegalArgumentException(Messages.element_illegalParent);
	}
}
 
Example 7
Source File: JdtTypeProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private String getSignature(String name) {
	if (Strings.isEmpty(name))
		throw new IllegalArgumentException("null");
	String signature = null;
	try {
		signature = name.startsWith("[") ? name : Signature.createTypeSignature(name, true);
	} catch (IllegalArgumentException e) {
		return null;
	}
	return signature;
}
 
Example 8
Source File: Util.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates a GWT RPC async callback parameter declaration based on the sync
 * method return type.
 *
 * @param ast {@link AST} associated with the destination compilation unit
 * @param syncReturnType the sync method return type
 * @param callbackParameterName name of the callback parameter
 * @param imports {@link ImportsRewrite} for the destination compilation unit
 * @return callback paramter declaration
 */
@SuppressWarnings("unchecked")
public static SingleVariableDeclaration createAsyncCallbackParameter(AST ast,
    Type syncReturnType, String callbackParameterName, ImportRewrite imports) {
  ITypeBinding syncReturnTypeBinding = syncReturnType.resolveBinding();

  SingleVariableDeclaration parameter = ast.newSingleVariableDeclaration();

  String gwtCallbackTypeSig = Signature.createTypeSignature(
      RemoteServiceUtilities.ASYNCCALLBACK_QUALIFIED_NAME, true);
  Type gwtCallbackType = imports.addImportFromSignature(gwtCallbackTypeSig,
      ast);

  if (syncReturnTypeBinding.isPrimitive()) {
    String wrapperName = JavaASTUtils.getWrapperTypeName(syncReturnTypeBinding.getName());
    String wrapperTypeSig = Signature.createTypeSignature(wrapperName, true);
    syncReturnType = imports.addImportFromSignature(wrapperTypeSig, ast);
  } else {
    syncReturnType = JavaASTUtils.normalizeTypeAndAddImport(ast,
        syncReturnType, imports);
  }

  ParameterizedType type = ast.newParameterizedType(gwtCallbackType);
  List<Type> typeArgs = type.typeArguments();
  typeArgs.add(syncReturnType);

  parameter.setType(type);
  parameter.setName(ast.newSimpleName(callbackParameterName));

  return parameter;
}
 
Example 9
Source File: SignatureUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the qualified signature corresponding to
 * <code>signature</code>.
 *
 * @param signature the signature to qualify
 * @param context the type inside which an unqualified type will be
 *        resolved to find the qualifier, or <code>null</code> if no
 *        context is available
 * @return the qualified signature
 */
public static String qualifySignature(final String signature, final IType context) {
	if (context == null)
		return signature;

	String qualifier= Signature.getSignatureQualifier(signature);
	if (qualifier.length() > 0)
		return signature;

	String elementType= Signature.getElementType(signature);
	String erasure= Signature.getTypeErasure(elementType);
	String simpleName= Signature.getSignatureSimpleName(erasure);
	String genericSimpleName= Signature.getSignatureSimpleName(elementType);

	int dim= Signature.getArrayCount(signature);

	try {
		String[][] strings= context.resolveType(simpleName);
		if (strings != null && strings.length > 0)
			qualifier= strings[0][0];
	} catch (JavaModelException e) {
		// ignore - not found
	}

	if (qualifier.length() == 0)
		return signature;

	String qualifiedType= Signature.toQualifiedName(new String[] {qualifier, genericSimpleName});
	String qualifiedSignature= Signature.createTypeSignature(qualifiedType, true);
	String newSignature= Signature.createArraySignature(qualifiedSignature, dim);

	return newSignature;
}
 
Example 10
Source File: SelectionRequestor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public void acceptLocalVariable(LocalVariableBinding binding, org.eclipse.jdt.internal.compiler.env.ICompilationUnit unit) {
	LocalDeclaration local = binding.declaration;
	IJavaElement parent = null;
	if (binding.declaringScope.isLambdaSubscope() && unit instanceof ICompilationUnit) {
		HashSet existingElements = new HashSet();
		HashMap knownScopes = new HashMap();
		parent = this.handleFactory.createElement(binding.declaringScope, local.sourceStart, (ICompilationUnit) unit, existingElements, knownScopes);
	} else {		
		parent = findLocalElement(local.sourceStart, binding.declaringScope.methodScope()); // findLocalElement() cannot find local variable
	}
	LocalVariable localVar = null;
	if(parent != null) {
		localVar = new LocalVariable(
				(JavaElement)parent,
				new String(local.name),
				local.declarationSourceStart,
				local.declarationSourceEnd,
				local.sourceStart,
				local.sourceEnd,
				local.type == null ? Signature.createTypeSignature(binding.type.readableName(), true) : Util.typeSignature(local.type),
				local.annotations,
				local.modifiers,
				local.getKind() == AbstractVariableDeclaration.PARAMETER);
	}
	if (localVar != null) {
		addElement(localVar);
		if(SelectionEngine.DEBUG){
			System.out.print("SELECTION - accept local variable("); //$NON-NLS-1$
			System.out.print(localVar.toString());
			System.out.println(")"); //$NON-NLS-1$
		}
	}
}
 
Example 11
Source File: VoidType.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected VoidType(TypeEnvironment environment) {
	super(environment, Signature.createTypeSignature("void", true)); //$NON-NLS-1$
}
 
Example 12
Source File: SourceFieldElementInfo.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Returns the type signature of the field.
 *
 * @see Signature
 */
protected String getTypeSignature() {
	return Signature.createTypeSignature(this.typeName, false);
}