private void markReferences() { fCaughtExceptions= new ArrayList<>(); boolean isVoid= true; Type returnType= fMethodDeclaration.getReturnType2(); if (returnType != null) { ITypeBinding returnTypeBinding= returnType.resolveBinding(); isVoid= returnTypeBinding != null && Bindings.isVoidType(returnTypeBinding); } fMethodDeclaration.accept(this); Block block= fMethodDeclaration.getBody(); if (block != null) { List<Statement> statements= block.statements(); if (statements.size() > 0) { Statement last= statements.get(statements.size() - 1); int maxVariableId= LocalVariableIndex.perform(fMethodDeclaration); FlowContext flowContext= new FlowContext(0, maxVariableId + 1); flowContext.setConsiderAccessMode(false); flowContext.setComputeMode(FlowContext.ARGUMENTS); InOutFlowAnalyzer flowAnalyzer= new InOutFlowAnalyzer(flowContext); FlowInfo info= flowAnalyzer.perform(new ASTNode[] {last}); if (!info.isNoReturn() && !isVoid) { if (!info.isPartialReturn()) return; } } int offset= fMethodDeclaration.getStartPosition() + fMethodDeclaration.getLength() - 1; // closing bracket fResult.add(new OccurrenceLocation(offset, 1, 0, fExitDescription)); } }
private void markReferences() { fCaughtExceptions= new ArrayList<>(); boolean isVoid= true; Type returnType= fMethodDeclaration.getReturnType2(); if (returnType != null) { ITypeBinding returnTypeBinding= returnType.resolveBinding(); isVoid= returnTypeBinding != null && Bindings.isVoidType(returnTypeBinding); } fMethodDeclaration.accept(this); Block block= fMethodDeclaration.getBody(); if (block != null) { List<Statement> statements= block.statements(); if (statements.size() > 0) { Statement last= statements.get(statements.size() - 1); int maxVariableId= LocalVariableIndex.perform(fMethodDeclaration); FlowContext flowContext= new FlowContext(0, maxVariableId + 1); flowContext.setConsiderAccessMode(false); flowContext.setComputeMode(FlowContext.ARGUMENTS); InOutFlowAnalyzer flowAnalyzer= new InOutFlowAnalyzer(flowContext); FlowInfo info= flowAnalyzer.perform(new ASTNode[] {last}); if (!info.isNoReturn() && !isVoid) { if (!info.isPartialReturn()) return; } } int offset= fMethodDeclaration.getStartPosition() + fMethodDeclaration.getLength() - 1; // closing bracket fResult.add(new OccurrenceLocation(offset, 1, 0, fExitDescription)); } }
/** * Normalizes a type binding received from an expression to a type binding that can be used in a declaration signature. * Anonymous types are normalized, to the super class or interface. For null or void bindings * <code>null</code> is returned. * @param binding the binding to normalize * @return the normalized binding */ public static ITypeBinding normalizeTypeBinding(ITypeBinding binding) { if (binding != null && !binding.isNullType() && !isVoidType(binding)) { if (binding.isAnonymous()) { ITypeBinding[] baseBindings= binding.getInterfaces(); if (baseBindings.length > 0) { return baseBindings[0]; } return binding.getSuperclass(); } if (binding.isCapture()) { return binding.getWildcard(); } return binding; } return null; }
private void markReferences() { fCatchedExceptions= new ArrayList(); boolean isVoid= true; Type returnType= fMethodDeclaration.getReturnType2(); if (returnType != null) { ITypeBinding returnTypeBinding= returnType.resolveBinding(); isVoid= returnTypeBinding != null && Bindings.isVoidType(returnTypeBinding); } fMethodDeclaration.accept(this); Block block= fMethodDeclaration.getBody(); if (block != null) { List statements= block.statements(); if (statements.size() > 0) { Statement last= (Statement)statements.get(statements.size() - 1); int maxVariableId= LocalVariableIndex.perform(fMethodDeclaration); FlowContext flowContext= new FlowContext(0, maxVariableId + 1); flowContext.setConsiderAccessMode(false); flowContext.setComputeMode(FlowContext.ARGUMENTS); InOutFlowAnalyzer flowAnalyzer= new InOutFlowAnalyzer(flowContext); FlowInfo info= flowAnalyzer.perform(new ASTNode[] {last}); if (!info.isNoReturn() && !isVoid) { if (!info.isPartialReturn()) return; } } SimpleName name= fAST.newSimpleName("x"); //$NON-NLS-1$ name.setSourceRange(fMethodDeclaration.getStartPosition() + fMethodDeclaration.getLength() - 1, 1); fResult.add(name); } }
/** * Normalizes a type binding received from an expression to a type binding that can be used inside a * declaration signature, but <em>not</em> as type of a declaration (use {@link #normalizeForDeclarationUse(ITypeBinding, AST)} for that). * <p> * Anonymous types are normalized to the super class or interface. For * null or void bindings, <code>null</code> is returned. * </p> * * @param binding the binding to normalize * @return the normalized binding, can be <code>null</code> * * @see #normalizeForDeclarationUse(ITypeBinding, AST) */ public static ITypeBinding normalizeTypeBinding(ITypeBinding binding) { if (binding != null && !binding.isNullType() && !isVoidType(binding)) { if (binding.isAnonymous()) { ITypeBinding[] baseBindings= binding.getInterfaces(); if (baseBindings.length > 0) { return baseBindings[0]; } return binding.getSuperclass(); } if (binding.isCapture()) { return binding.getWildcard(); } return binding; } return null; }
/** * Normalizes a type binding received from an expression to a type binding that can be used inside a * declaration signature, but <em>not</em> as type of a declaration (use {@link #normalizeForDeclarationUse(ITypeBinding, AST)} for that). * <p> * Anonymous types are normalized to the super class or interface. For * null or void bindings, <code>null</code> is returned. * </p> * * @param binding the binding to normalize * @return the normalized binding, can be <code>null</code> * * @see #normalizeForDeclarationUse(ITypeBinding, AST) */ public static ITypeBinding normalizeTypeBinding(ITypeBinding binding) { if (binding != null && !binding.isNullType() && !isVoidType(binding)) { if (binding.isAnonymous()) { ITypeBinding[] baseBindings= binding.getInterfaces(); if (baseBindings.length > 0) { return baseBindings[0]; } return binding.getSuperclass(); } if (binding.isCapture()) { return binding.getWildcard(); } return binding; } return null; }
if (binding == null || Bindings.isVoidType(binding)) { return false;
private Statement createAddArrayHashCode(IVariableBinding binding) { MethodInvocation invoc= fAst.newMethodInvocation(); if (JavaModelUtil.is50OrHigher(fRewrite.getCu().getJavaProject())) { invoc.setName(fAst.newSimpleName(METHODNAME_HASH_CODE)); invoc.setExpression(getQualifiedName(JAVA_UTIL_ARRAYS)); invoc.arguments().add(getThisAccessForHashCode(binding.getName())); } else { invoc.setName(fAst.newSimpleName(METHODNAME_HASH_CODE)); final IJavaElement element= fType.getJavaElement(); if (element != null && !"".equals(element.getElementName())) //$NON-NLS-1$ invoc.setExpression(fAst.newSimpleName(element.getElementName())); invoc.arguments().add(getThisAccessForHashCode(binding.getName())); final ITypeBinding type= binding.getType().getElementType(); if (!Bindings.isVoidType(type)) { if (type.isPrimitive() && binding.getType().getDimensions() < 2) fCustomHashCodeTypes.add(type); else fCustomHashCodeTypes.add(fAst.resolveWellKnownType(JAVA_LANG_OBJECT)); } } return prepareAssignment(invoc); }
private void replaceSelectedExpressionWithTempDeclaration() throws CoreException { ASTRewrite rewrite = fCURewrite.getASTRewrite(); Expression selectedExpression = getSelectedExpression().getAssociatedExpression(); // whole expression selected Expression initializer = (Expression) rewrite.createMoveTarget(selectedExpression); VariableDeclarationStatement tempDeclaration = createTempDeclaration(initializer); ASTNode replacement; ASTNode parent = selectedExpression.getParent(); boolean isParentLambda = parent instanceof LambdaExpression; AST ast = rewrite.getAST(); if (isParentLambda) { Block blockBody = ast.newBlock(); blockBody.statements().add(tempDeclaration); if (!Bindings.isVoidType(((LambdaExpression) parent).resolveMethodBinding().getReturnType())) { List<VariableDeclarationFragment> fragments = tempDeclaration.fragments(); SimpleName varName = fragments.get(0).getName(); ReturnStatement returnStatement = ast.newReturnStatement(); returnStatement.setExpression(ast.newSimpleName(varName.getIdentifier())); blockBody.statements().add(returnStatement); } replacement = blockBody; } else if (ASTNodes.isControlStatementBody(parent.getLocationInParent())) { Block block = ast.newBlock(); block.statements().add(tempDeclaration); replacement = block; } else { replacement = tempDeclaration; } ASTNode replacee = isParentLambda || !ASTNodes.hasSemicolon((ExpressionStatement) parent, fCu) ? selectedExpression : parent; rewrite.replace(replacee, replacement, fCURewrite.createGroupDescription(RefactoringCoreMessages.ExtractTempRefactoring_declare_local_variable)); }
private void replaceSelectedExpressionWithTempDeclaration() throws CoreException { ASTRewrite rewrite= fCURewrite.getASTRewrite(); Expression selectedExpression= getSelectedExpression().getAssociatedExpression(); // whole expression selected Expression initializer= (Expression) rewrite.createMoveTarget(selectedExpression); VariableDeclarationStatement tempDeclaration= createTempDeclaration(initializer); ASTNode replacement; ASTNode parent= selectedExpression.getParent(); boolean isParentLambda= parent instanceof LambdaExpression; AST ast= rewrite.getAST(); if (isParentLambda) { Block blockBody= ast.newBlock(); blockBody.statements().add(tempDeclaration); if (!Bindings.isVoidType(((LambdaExpression) parent).resolveMethodBinding().getReturnType())) { List<VariableDeclarationFragment> fragments= tempDeclaration.fragments(); SimpleName varName= fragments.get(0).getName(); ReturnStatement returnStatement= ast.newReturnStatement(); returnStatement.setExpression(ast.newSimpleName(varName.getIdentifier())); blockBody.statements().add(returnStatement); } replacement= blockBody; } else if (ASTNodes.isControlStatementBody(parent.getLocationInParent())) { Block block= ast.newBlock(); block.statements().add(tempDeclaration); replacement= block; } else { replacement= tempDeclaration; } ASTNode replacee= isParentLambda || !ASTNodes.hasSemicolon((ExpressionStatement) parent, fCu) ? selectedExpression : parent; rewrite.replace(replacee, replacement, fCURewrite.createGroupDescription(RefactoringCoreMessages.ExtractTempRefactoring_declare_local_variable)); }
private void replaceSelectedExpressionWithTempDeclaration() throws CoreException { ASTRewrite rewrite= fCURewrite.getASTRewrite(); Expression selectedExpression= getSelectedExpression().getAssociatedExpression(); // whole expression selected Expression initializer= (Expression) rewrite.createMoveTarget(selectedExpression); VariableDeclarationStatement tempDeclaration= createTempDeclaration(initializer); ASTNode replacement; ASTNode parent= selectedExpression.getParent(); boolean isParentLambda= parent instanceof LambdaExpression; AST ast= rewrite.getAST(); if (isParentLambda) { Block blockBody= ast.newBlock(); blockBody.statements().add(tempDeclaration); if (!Bindings.isVoidType(((LambdaExpression) parent).resolveMethodBinding().getReturnType())) { List<VariableDeclarationFragment> fragments= tempDeclaration.fragments(); SimpleName varName= fragments.get(0).getName(); ReturnStatement returnStatement= ast.newReturnStatement(); returnStatement.setExpression(ast.newSimpleName(varName.getIdentifier())); blockBody.statements().add(returnStatement); } replacement= blockBody; } else if (ASTNodes.isControlStatementBody(parent.getLocationInParent())) { Block block= ast.newBlock(); block.statements().add(tempDeclaration); replacement= block; } else { replacement= tempDeclaration; } ASTNode replacee= isParentLambda || !ASTNodes.hasSemicolon((ExpressionStatement) parent, fCu) ? selectedExpression : parent; rewrite.replace(replacee, replacement, fCURewrite.createGroupDescription(RefactoringCoreMessages.ExtractTempRefactoring_declare_local_variable)); }
private Statement createAddArrayHashCode(IVariableBinding binding) { MethodInvocation invoc= fAst.newMethodInvocation(); if (JavaModelUtil.is50OrHigher(fRewrite.getCu().getJavaProject())) { if (needsDeepMethod(binding.getType())) { invoc.setName(fAst.newSimpleName(METHODNAME_DEEP_HASH_CODE)); } else { invoc.setName(fAst.newSimpleName(METHODNAME_HASH_CODE)); } invoc.setExpression(getQualifiedName(JAVA_UTIL_ARRAYS)); invoc.arguments().add(getThisAccessForHashCode(binding.getName())); } else { invoc.setName(fAst.newSimpleName(METHODNAME_HASH_CODE)); final IJavaElement element= fType.getJavaElement(); if (element != null && !"".equals(element.getElementName())) //$NON-NLS-1$ invoc.setExpression(fAst.newSimpleName(element.getElementName())); invoc.arguments().add(getThisAccessForHashCode(binding.getName())); ITypeBinding type= binding.getType().getElementType(); if (!Bindings.isVoidType(type)) { if (!type.isPrimitive() || binding.getType().getDimensions() >= 2) type= fAst.resolveWellKnownType(JAVA_LANG_OBJECT); if (!fCustomHashCodeTypes.contains(type)) fCustomHashCodeTypes.add(type); } } return prepareAssignment(invoc); }
private Statement createAddArrayHashCode(IVariableBinding binding) { MethodInvocation invoc= fAst.newMethodInvocation(); if (JavaModelUtil.is50OrHigher(fRewrite.getCu().getJavaProject())) { if (binding.getType().getDimensions() > 1) { invoc.setName(fAst.newSimpleName(METHODNAME_DEEP_HASH_CODE)); } else { invoc.setName(fAst.newSimpleName(METHODNAME_HASH_CODE)); } invoc.setExpression(getQualifiedName(JAVA_UTIL_ARRAYS)); invoc.arguments().add(getThisAccessForHashCode(binding.getName())); } else { invoc.setName(fAst.newSimpleName(METHODNAME_HASH_CODE)); final IJavaElement element= fType.getJavaElement(); if (element != null && !"".equals(element.getElementName())) //$NON-NLS-1$ invoc.setExpression(fAst.newSimpleName(element.getElementName())); invoc.arguments().add(getThisAccessForHashCode(binding.getName())); ITypeBinding type= binding.getType().getElementType(); if (!Bindings.isVoidType(type)) { if (!type.isPrimitive() || binding.getType().getDimensions() >= 2) type= fAst.resolveWellKnownType(JAVA_LANG_OBJECT); if (!fCustomHashCodeTypes.contains(type)) fCustomHashCodeTypes.add(type); } } return prepareAssignment(invoc); }
ITypeBinding declaringType= context.variableBinding.getDeclaringClass(); IMethodBinding method= Bindings.findMethodInHierarchy(declaringType, setterName, new ITypeBinding[] { context.variableBinding.getType() }); if (method != null && Bindings.isVoidType(method.getReturnType()) && (Modifier.isStatic(method.getModifiers()) == Modifier.isStatic(context.variableBinding.getModifiers()))) { Expression assignedValue= getAssignedValue(context); if (assignedValue == null)
if (method != null && Bindings.isVoidType(method.getReturnType()) && (Modifier.isStatic(method.getModifiers()) == Modifier.isStatic(context.variableBinding.getModifiers()))) { Expression assignedValue = getAssignedValue(context); if (assignedValue == null) {
if (method != null && Bindings.isVoidType(method.getReturnType()) && (Modifier.isStatic(method.getModifiers()) == Modifier.isStatic(context.variableBinding.getModifiers()))) { Expression assignedValue= getAssignedValue(context); if (assignedValue == null)
if (method != null && Bindings.isVoidType(method.getReturnType()) && (Modifier.isStatic(method.getModifiers()) == Modifier.isStatic(context.variableBinding.getModifiers()))) { Expression assignedValue= getAssignedValue(context); if (assignedValue == null)
ASTNode moveTarget = rewrite.createMoveTarget(target); AST ast = rewrite.getAST(); if (Bindings.isVoidType(((LambdaExpression) parent).resolveMethodBinding().getReturnType())) { ExpressionStatement expressionStatement = ast.newExpressionStatement((Expression) moveTarget); moveTarget = expressionStatement;
ASTNode moveTarget= rewrite.createMoveTarget(target); AST ast= rewrite.getAST(); if (Bindings.isVoidType(((LambdaExpression) parent).resolveMethodBinding().getReturnType())) { ExpressionStatement expressionStatement= ast.newExpressionStatement((Expression) moveTarget); moveTarget= expressionStatement;
ASTNode moveTarget= rewrite.createMoveTarget(target); AST ast= rewrite.getAST(); if (Bindings.isVoidType(((LambdaExpression) parent).resolveMethodBinding().getReturnType())) { ExpressionStatement expressionStatement= ast.newExpressionStatement((Expression) moveTarget); moveTarget= expressionStatement;