Java Code Examples for org.eclipse.jdt.core.dom.IMethodBinding#getTypeParameters()

The following examples show how to use org.eclipse.jdt.core.dom.IMethodBinding#getTypeParameters() . 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: HierarchyProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public final boolean visit(final SimpleName node) {
	final ITypeBinding binding= node.resolveTypeBinding();
	if (binding != null && binding.isTypeVariable()) {
		String name= null;
		for (int index= 0; index < fMapping.length; index++) {
			name= binding.getName();
			if (fMapping[index].getSourceName().equals(name) && node.getIdentifier().equals(name)) {
				final MethodDeclaration declaration= (MethodDeclaration) ASTNodes.getParent(node, MethodDeclaration.class);
				if (declaration != null) {
					final IMethodBinding method= declaration.resolveBinding();
					if (method != null) {
						final ITypeBinding[] bindings= method.getTypeParameters();
						for (int offset= 0; offset < bindings.length; offset++) {
							if (bindings[offset].isEqualTo(binding))
								return true;
						}
					}
				}
				fRewrite.set(node, SimpleName.IDENTIFIER_PROPERTY, fMapping[index].getTargetName(), null);
			}
		}
	}
	return true;
}
 
Example 2
Source File: PromoteTempToFieldRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private RefactoringStatus checkTempTypeForLocalTypeUsage(){
  	VariableDeclarationStatement vds= getTempDeclarationStatement();
  	if (vds == null)
  		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_cannot_promote);
  	Type type= 	vds.getType();
  	ITypeBinding binding= type.resolveBinding();
  	if (binding == null)
  		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_cannot_promote);

IMethodBinding declaringMethodBinding= getMethodDeclaration().resolveBinding();
ITypeBinding[] methodTypeParameters= declaringMethodBinding == null ? new ITypeBinding[0] : declaringMethodBinding.getTypeParameters();
LocalTypeAndVariableUsageAnalyzer analyzer= new LocalTypeAndVariableUsageAnalyzer(methodTypeParameters);
type.accept(analyzer);
boolean usesLocalTypes= ! analyzer.getUsageOfEnclosingNodes().isEmpty();
fTempTypeUsesClassTypeVariables= analyzer.getClassTypeVariablesUsed();
if (usesLocalTypes)
	return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_uses_type_declared_locally);
return null;
  }
 
Example 3
Source File: StubUtility2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static void createTypeParameters(ImportRewrite imports, ImportRewriteContext context, AST ast, IMethodBinding binding, MethodDeclaration decl) {
	ITypeBinding[] typeParams= binding.getTypeParameters();
	List<TypeParameter> typeParameters= decl.typeParameters();
	for (int i= 0; i < typeParams.length; i++) {
		ITypeBinding curr= typeParams[i];
		TypeParameter newTypeParam= ast.newTypeParameter();
		newTypeParam.setName(ast.newSimpleName(curr.getName()));
		ITypeBinding[] typeBounds= curr.getTypeBounds();
		if (typeBounds.length != 1 || !"java.lang.Object".equals(typeBounds[0].getQualifiedName())) {//$NON-NLS-1$
			List<Type> newTypeBounds= newTypeParam.typeBounds();
			for (int k= 0; k < typeBounds.length; k++) {
				newTypeBounds.add(imports.addImport(typeBounds[k], ast, context));
			}
		}
		typeParameters.add(newTypeParam);
	}
}
 
Example 4
Source File: ExtractFieldRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private RefactoringStatus checkTempTypeForLocalTypeUsage() throws JavaModelException {
	Expression expression = getSelectedExpression().getAssociatedExpression();
	Type resultingType = null;
	ITypeBinding typeBinding = expression.resolveTypeBinding();
	AST ast = fCURewrite.getAST();

	if (expression instanceof ClassInstanceCreation && (typeBinding == null || typeBinding.getTypeArguments().length == 0)) {
		resultingType = ((ClassInstanceCreation) expression).getType();
	} else if (expression instanceof CastExpression) {
		resultingType = ((CastExpression) expression).getType();
	} else {
		if (typeBinding == null) {
			typeBinding = ASTResolving.guessBindingForReference(expression);
		}
		if (typeBinding != null) {
			typeBinding = Bindings.normalizeForDeclarationUse(typeBinding, ast);
			ImportRewrite importRewrite = fCURewrite.getImportRewrite();
			ImportRewriteContext context = new ContextSensitiveImportRewriteContext(expression, importRewrite);
			resultingType = importRewrite.addImport(typeBinding, ast, context, TypeLocation.LOCAL_VARIABLE);
		} else {
			resultingType = ast.newSimpleType(ast.newSimpleName("Object")); //$NON-NLS-1$
		}
	}

	IMethodBinding declaringMethodBinding = getMethodDeclaration().resolveBinding();
	ITypeBinding[] methodTypeParameters = declaringMethodBinding == null ? new ITypeBinding[0] : declaringMethodBinding.getTypeParameters();
	LocalTypeAndVariableUsageAnalyzer analyzer = new LocalTypeAndVariableUsageAnalyzer(methodTypeParameters);
	resultingType.accept(analyzer);
	boolean usesLocalTypes = !analyzer.getUsageOfEnclosingNodes().isEmpty();
	if (usesLocalTypes) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractFieldRefactoring_uses_type_declared_locally);
	}
	return null;
}
 
Example 5
Source File: ExtractFieldRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void checkTempInitializerForLocalTypeUsage() throws JavaModelException {
	Expression initializer = getSelectedExpression().getAssociatedExpression();
	IMethodBinding declaringMethodBinding = getMethodDeclaration().resolveBinding();
	ITypeBinding[] methodTypeParameters = declaringMethodBinding == null ? new ITypeBinding[0] : declaringMethodBinding.getTypeParameters();
	LocalTypeAndVariableUsageAnalyzer localTypeAnalyer = new LocalTypeAndVariableUsageAnalyzer(methodTypeParameters);
	initializer.accept(localTypeAnalyer);
	fInitializerUsesLocalTypes = !localTypeAnalyer.getUsageOfEnclosingNodes().isEmpty();
}
 
Example 6
Source File: MoveInstanceMethodProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates the necessary changes to remove method type parameters if they
 * match with enclosing type parameters.
 *
 * @param rewrite
 *            the ast rewrite to use
 * @param declaration
 *            the method declaration to remove type parameters
 * @param status
 *            the refactoring status
 */
protected void createMethodTypeParameters(final ASTRewrite rewrite, final MethodDeclaration declaration, final RefactoringStatus status) {
	ITypeBinding binding= fTarget.getType();
	if (binding != null && binding.isParameterizedType()) {
		final IMethodBinding method= declaration.resolveBinding();
		if (method != null) {
			final ITypeBinding[] parameters= method.getTypeParameters();
			if (parameters.length > 0) {
				final ListRewrite rewriter= rewrite.getListRewrite(declaration, MethodDeclaration.TYPE_PARAMETERS_PROPERTY);
				boolean foundStatic= false;
				while (binding != null && !foundStatic) {
					if (Flags.isStatic(binding.getModifiers()))
						foundStatic= true;
					final ITypeBinding[] bindings= binding.getTypeArguments();
					for (int index= 0; index < bindings.length; index++) {
						for (int offset= 0; offset < parameters.length; offset++) {
							if (parameters[offset].getName().equals(bindings[index].getName())) {
								rewriter.remove((ASTNode) rewriter.getOriginalList().get(offset), null);
								status.addWarning(Messages.format(RefactoringCoreMessages.MoveInstanceMethodProcessor_present_type_parameter_warning, new Object[] { BasicElementLabels.getJavaElementName(parameters[offset].getName()), BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED) }), JavaStatusContext.create(fMethod));
							}
						}
					}
					binding= binding.getDeclaringClass();
				}
			}
		}
	}
}
 
Example 7
Source File: PromoteTempToFieldRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void checkTempInitializerForLocalTypeUsage() {
  	Expression initializer= fTempDeclarationNode.getInitializer();
  	if (initializer == null)
       return;

IMethodBinding declaringMethodBinding= getMethodDeclaration().resolveBinding();
ITypeBinding[] methodTypeParameters= declaringMethodBinding == null ? new ITypeBinding[0] : declaringMethodBinding.getTypeParameters();
   LocalTypeAndVariableUsageAnalyzer localTypeAnalyer= new LocalTypeAndVariableUsageAnalyzer(methodTypeParameters);
   initializer.accept(localTypeAnalyer);
   fInitializerUsesLocalTypes= ! localTypeAnalyer.getUsageOfEnclosingNodes().isEmpty();
  }
 
Example 8
Source File: NewAsyncRemoteServiceInterfaceCreationWizardPage.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
public static String createMethodContents(IType newType,
    ImportManagerAdapter imports, IMethodBinding overridableSyncMethod,
    boolean addComments) throws CoreException, JavaModelException {
  StringBuilder sb = new StringBuilder();

  IMethod syncMethod = (IMethod) overridableSyncMethod.getJavaElement();

  if (addComments) {
    String lineDelimiter = "\n"; // OK, since content is formatted afterwards

    // Don't go through CodeGeneration type since it can't deal with delegates
    String comment = StubUtility.getMethodComment(
        newType.getCompilationUnit(), newType.getFullyQualifiedName(),
        syncMethod.getElementName(), NO_STRINGS, NO_STRINGS,
        Signature.SIG_VOID, NO_STRINGS, syncMethod, true, lineDelimiter);
    if (comment != null) {
      sb.append(comment);
      sb.append(lineDelimiter);
    }
  }

  // Expand the type parameters
  ITypeParameter[] typeParameters = syncMethod.getTypeParameters();
  ITypeBinding[] typeParameterBindings = overridableSyncMethod.getTypeParameters();
  if (typeParameters.length > 0) {
    sb.append("<");
    for (int i = 0; i < typeParameters.length; ++i) {
      sb.append(typeParameters[i].getElementName());
      ITypeBinding typeParameterBinding = typeParameterBindings[i];
      ITypeBinding[] typeBounds = typeParameterBinding.getTypeBounds();
      if (typeBounds.length > 0) {
        sb.append(" extends ");
        for (int j = 0; j < typeBounds.length; ++j) {
          if (j != 0) {
            sb.append(" & ");
          }
          expandTypeBinding(typeBounds[j], sb, imports);
        }
      }
    }
    sb.append(">");
  }

  // Default to a void return type for the async method
  sb.append("void ");

  // Expand the method name
  sb.append(overridableSyncMethod.getName());

  // Expand the arguments
  sb.append("(");
  String[] parameterNames = syncMethod.getParameterNames();
  ITypeBinding[] parameterTypes = overridableSyncMethod.getParameterTypes();
  for (int i = 0; i < parameterNames.length; ++i) {
    if (i != 0) {
      sb.append(", ");
    }

    expandTypeBinding(parameterTypes[i], sb, imports);
    sb.append(" ");
    sb.append(parameterNames[i]);
  }

  if (parameterNames.length > 0) {
    sb.append(", ");
  }

  sb.append(imports.addImport(RemoteServiceUtilities.ASYNCCALLBACK_QUALIFIED_NAME));
  sb.append("<");
  ITypeBinding syncReturnType = overridableSyncMethod.getReturnType();
  if (syncReturnType.isPrimitive()) {
    String wrapperTypeName = JavaASTUtils.getWrapperTypeName(syncReturnType.getName());
    sb.append(imports.addImport(wrapperTypeName));
  } else {
    expandTypeBinding(syncReturnType, sb, imports);
  }
  sb.append("> ");
  sb.append(StringUtilities.computeUniqueName(parameterNames, "callback"));
  sb.append(");");

  return sb.toString();
}