org.eclipse.jdt.core.dom.VariableDeclarationStatement Java Examples

The following examples show how to use org.eclipse.jdt.core.dom.VariableDeclarationStatement. 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 Project: SimFix   Author: xgdsmileboy   File: NodeUtils.java    License: GNU General Public License v2.0 6 votes vote down vote up
public boolean visit(VariableDeclarationStatement node) {
	ASTNode parent = node.getParent();
	while(parent != null){
		if(parent instanceof Block){
			break;
		}
		parent = parent.getParent();
	}
	if(parent != null) {
		int start = _unit.getLineNumber(node.getStartPosition());
		int end = _unit.getLineNumber(parent.getStartPosition() + parent.getLength());
		for (Object o : node.fragments()) {
			VariableDeclarationFragment vdf = (VariableDeclarationFragment) o;
			Pair<String, Type> pair = new Pair<String, Type>(vdf.getName().getFullyQualifiedName(), node.getType());
			Pair<Integer, Integer> range = new Pair<Integer, Integer>(start, end);
			_tmpVars.put(pair, range);
		}
	}
	return true;
}
 
Example #2
Source Project: JDeodorant   Author: tsantalis   File: RefactoringUtility.java    License: MIT License 6 votes vote down vote up
private static Type extractType(VariableDeclaration variableDeclaration) {
	Type returnedVariableType = null;
	if(variableDeclaration instanceof SingleVariableDeclaration) {
		SingleVariableDeclaration singleVariableDeclaration = (SingleVariableDeclaration)variableDeclaration;
		returnedVariableType = singleVariableDeclaration.getType();
	}
	else if(variableDeclaration instanceof VariableDeclarationFragment) {
		VariableDeclarationFragment fragment = (VariableDeclarationFragment)variableDeclaration;
		if(fragment.getParent() instanceof VariableDeclarationStatement) {
			VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement)fragment.getParent();
			returnedVariableType = variableDeclarationStatement.getType();
		}
		else if(fragment.getParent() instanceof VariableDeclarationExpression) {
			VariableDeclarationExpression variableDeclarationExpression = (VariableDeclarationExpression)fragment.getParent();
			returnedVariableType = variableDeclarationExpression.getType();
		}
		else if(fragment.getParent() instanceof FieldDeclaration) {
			FieldDeclaration fieldDeclaration = (FieldDeclaration)fragment.getParent();
			returnedVariableType = fieldDeclaration.getType();
		}
	}
	return returnedVariableType;
}
 
Example #3
Source Project: JDeodorant   Author: tsantalis   File: StyledStringVisitor.java    License: MIT License 6 votes vote down vote up
public boolean visit(VariableDeclarationStatement stmnt) {
	/*
	  { ExtendedModifier } Type VariableDeclarationFragment
       		{ , VariableDeclarationFragment } ;
	 */
	// Append modifiers if applicable
	for (int i = 0; i < stmnt.modifiers().size(); i++) {
		handleModifier((IExtendedModifier) stmnt.modifiers().get(i));
		appendSpace();
	}
	// Append Type
	handleType(stmnt.getType());
	appendSpace();
	// Visit Fragments
	for (int i = 0; i < stmnt.fragments().size(); i++) {
		visit((VariableDeclarationFragment) stmnt.fragments().get(i));
		if(i < stmnt.fragments().size() - 1) {
			appendComma();
		}
	}
	appendSemicolon();
	return false;
}
 
Example #4
Source Project: spotbugs   Author: spotbugs   File: CreateDoPrivilegedBlockResolution.java    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
protected void updateLocalVariableDeclarations(final ASTRewrite rewrite, final Set<String> variables, Block block) {
    Assert.isNotNull(rewrite);
    Assert.isNotNull(block);
    Assert.isNotNull(variables);

    final AST ast = rewrite.getAST();
    block.accept(new ASTVisitor() {

        @Override
        public boolean visit(VariableDeclarationFragment fragment) {
            if (variables.contains(fragment.getName().getFullyQualifiedName())) {
                ASTNode parent = fragment.getParent();
                if (parent instanceof VariableDeclarationStatement) {
                    ListRewrite listRewrite = rewrite
                            .getListRewrite(parent, VariableDeclarationStatement.MODIFIERS2_PROPERTY);
                    listRewrite.insertLast(ast.newModifier(ModifierKeyword.FINAL_KEYWORD), null);
                }
            }
            return true;
        }

    });

}
 
Example #5
Source Project: Eclipse-Postfix-Code-Completion   Author: trylimits   File: VariableDeclarationFix.java    License: Eclipse Public License 1.0 6 votes vote down vote up
private static ModifierChangeOperation createAddFinalOperation(SimpleName name, ASTNode decl) {
	if (decl == null)
		return null;

	IBinding binding= name.resolveBinding();
	if (!canAddFinal(binding, decl))
		return null;

	if (decl instanceof SingleVariableDeclaration) {
		return new ModifierChangeOperation(decl, new ArrayList<VariableDeclarationFragment>(), Modifier.FINAL, Modifier.NONE);
	} else if (decl instanceof VariableDeclarationExpression) {
		return new ModifierChangeOperation(decl, new ArrayList<VariableDeclarationFragment>(), Modifier.FINAL, Modifier.NONE);
	} else if (decl instanceof VariableDeclarationFragment){
		VariableDeclarationFragment frag= (VariableDeclarationFragment)decl;
		decl= decl.getParent();
		if (decl instanceof FieldDeclaration || decl instanceof VariableDeclarationStatement) {
			List<VariableDeclarationFragment> list= new ArrayList<VariableDeclarationFragment>();
			list.add(frag);
			return new ModifierChangeOperation(decl, list, Modifier.FINAL, Modifier.NONE);
		} else if (decl instanceof VariableDeclarationExpression) {
			return new ModifierChangeOperation(decl, new ArrayList<VariableDeclarationFragment>(), Modifier.FINAL, Modifier.NONE);
		}
	}

	return null;
}
 
Example #6
Source Project: RefactoringMiner   Author: tsantalis   File: VariableDeclaration.java    License: MIT License 6 votes vote down vote up
private static CodeElementType extractVariableDeclarationType(org.eclipse.jdt.core.dom.VariableDeclaration variableDeclaration) {
	if(variableDeclaration instanceof SingleVariableDeclaration) {
		return CodeElementType.SINGLE_VARIABLE_DECLARATION;
	}
	else if(variableDeclaration instanceof VariableDeclarationFragment) {
		VariableDeclarationFragment fragment = (VariableDeclarationFragment)variableDeclaration;
		if(fragment.getParent() instanceof VariableDeclarationStatement) {
			return CodeElementType.VARIABLE_DECLARATION_STATEMENT;
		}
		else if(fragment.getParent() instanceof VariableDeclarationExpression) {
			return CodeElementType.VARIABLE_DECLARATION_EXPRESSION;
		}
		else if(fragment.getParent() instanceof FieldDeclaration) {
			return CodeElementType.FIELD_DECLARATION;
		}
	}
	return null;
}
 
Example #7
Source Project: Eclipse-Postfix-Code-Completion   Author: trylimits   File: ChangeTypeRefactoring.java    License: Eclipse Public License 1.0 6 votes vote down vote up
private static Type getType(ASTNode node) {
	switch(node.getNodeType()){
		case ASTNode.SINGLE_VARIABLE_DECLARATION:
			return ((SingleVariableDeclaration) node).getType();
		case ASTNode.FIELD_DECLARATION:
			return ((FieldDeclaration) node).getType();
		case ASTNode.VARIABLE_DECLARATION_STATEMENT:
			return ((VariableDeclarationStatement) node).getType();
		case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
			return ((VariableDeclarationExpression) node).getType();
		case ASTNode.METHOD_DECLARATION:
			return ((MethodDeclaration)node).getReturnType2();
		case ASTNode.PARAMETERIZED_TYPE:
			return ((ParameterizedType)node).getType();
		default:
			Assert.isTrue(false);
			return null;
	}
}
 
Example #8
Source Project: Eclipse-Postfix-Code-Completion   Author: trylimits   File: ChangeTypeRefactoring.java    License: Eclipse Public License 1.0 6 votes vote down vote up
/**
  * The selection corresponds to a ParameterizedType (return type of method)
 * @param pt the type
 * @return the message
  */
private String parameterizedTypeSelected(ParameterizedType pt) {
	ASTNode parent= pt.getParent();
	if (parent.getNodeType() == ASTNode.METHOD_DECLARATION){
		fMethodBinding= ((MethodDeclaration)parent).resolveBinding();
		fParamIndex= -1;
		fEffectiveSelectionStart= pt.getStartPosition();
		fEffectiveSelectionLength= pt.getLength();
		setOriginalType(pt.resolveBinding());
	} else if (parent.getNodeType() == ASTNode.SINGLE_VARIABLE_DECLARATION){
		return singleVariableDeclarationSelected((SingleVariableDeclaration)parent);
	} else if (parent.getNodeType() == ASTNode.VARIABLE_DECLARATION_STATEMENT){
		return variableDeclarationStatementSelected((VariableDeclarationStatement)parent);
	} else if (parent.getNodeType() == ASTNode.FIELD_DECLARATION){
		return fieldDeclarationSelected((FieldDeclaration)parent);
	} else {
		return nodeTypeNotSupported();
	}
	return null;
}
 
Example #9
Source Project: windup   Author: windup   File: ReferenceResolvingVisitor.java    License: Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Declaration of the variable within a block
 */
@Override
public boolean visit(VariableDeclarationStatement node)
{
    for (int i = 0; i < node.fragments().size(); ++i)
    {
        String nodeType = node.getType().toString();
        VariableDeclarationFragment frag = (VariableDeclarationFragment) node.fragments().get(i);
        state.getNames().add(frag.getName().getIdentifier());
        state.getNameInstance().put(frag.getName().toString(), nodeType.toString());
    }

    processType(node.getType(), TypeReferenceLocation.VARIABLE_DECLARATION,
                compilationUnit.getLineNumber(node.getStartPosition()),
                compilationUnit.getColumnNumber(node.getStartPosition()), node.getLength(), node.toString());
    return super.visit(node);
}
 
Example #10
Source Project: Eclipse-Postfix-Code-Completion   Author: trylimits   File: ExtractTempRefactoring.java    License: Eclipse Public License 1.0 6 votes vote down vote up
private void createAndInsertTempDeclaration() throws CoreException {
	Expression initializer= getSelectedExpression().createCopyTarget(fCURewrite.getASTRewrite(), true);
	VariableDeclarationStatement vds= createTempDeclaration(initializer);

	if ((!fReplaceAllOccurrences) || (retainOnlyReplacableMatches(getMatchingFragments()).length <= 1)) {
		insertAt(getSelectedExpression().getAssociatedNode(), vds);
		return;
	}

	ASTNode[] firstReplaceNodeParents= getParents(getFirstReplacedExpression().getAssociatedNode());
	ASTNode[] commonPath= findDeepestCommonSuperNodePathForReplacedNodes();
	Assert.isTrue(commonPath.length <= firstReplaceNodeParents.length);

	ASTNode deepestCommonParent= firstReplaceNodeParents[commonPath.length - 1];
	if (deepestCommonParent instanceof Block)
		insertAt(firstReplaceNodeParents[commonPath.length], vds);
	else
		insertAt(deepestCommonParent, vds);
}
 
Example #11
Source Project: Eclipse-Postfix-Code-Completion   Author: trylimits   File: CallInliner.java    License: Eclipse Public License 1.0 6 votes vote down vote up
public RefactoringStatus initialize(ASTNode invocation, int severity) {
	RefactoringStatus result= new RefactoringStatus();
	fInvocation= invocation;
	fLocals= new ArrayList<VariableDeclarationStatement>(3);

	checkMethodDeclaration(result, severity);
	if (result.getSeverity() >= severity)
		return result;

	initializeRewriteState();
	initializeTargetNode();
	flowAnalysis();

	fContext= new CallContext(fInvocation, fInvocationScope, fTargetNode.getNodeType(), fImportRewrite);

	try {
		computeRealArguments();
		computeReceiver();
	} catch (BadLocationException exception) {
		JavaPlugin.log(exception);
	}
	checkInvocationContext(result, severity);

	return result;
}
 
Example #12
Source Project: Eclipse-Postfix-Code-Completion   Author: trylimits   File: ASTNodes.java    License: Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the type node for the given declaration.
 * 
 * @param declaration the declaration
 * @return the type node or <code>null</code> if the given declaration represents a type
 *         inferred parameter in lambda expression
 */
public static Type getType(VariableDeclaration declaration) {
	if (declaration instanceof SingleVariableDeclaration) {
		return ((SingleVariableDeclaration)declaration).getType();
	} else if (declaration instanceof VariableDeclarationFragment) {
		ASTNode parent= ((VariableDeclarationFragment)declaration).getParent();
		if (parent instanceof VariableDeclarationExpression)
			return ((VariableDeclarationExpression)parent).getType();
		else if (parent instanceof VariableDeclarationStatement)
			return ((VariableDeclarationStatement)parent).getType();
		else if (parent instanceof FieldDeclaration)
			return ((FieldDeclaration)parent).getType();
		else if (parent instanceof LambdaExpression)
			return null;
	}
	Assert.isTrue(false, "Unknown VariableDeclaration"); //$NON-NLS-1$
	return null;
}
 
Example #13
Source Project: SimFix   Author: xgdsmileboy   File: CodeSearch.java    License: GNU General Public License v2.0 5 votes vote down vote up
public boolean visit(VariableDeclarationStatement node){
	int start = _unit.getLineNumber(node.getStartPosition());
	if(start == _extendedLine){
		_extendedStatement = node;
		return false;
	}
	return true;
}
 
Example #14
Source Project: SimFix   Author: xgdsmileboy   File: TypeParseVisitor.java    License: GNU General Public License v2.0 5 votes vote down vote up
public boolean visit(VariableDeclarationStatement node) {
	if(isAnonymousClass(node)){
		return true;
	}
	for (Object o : node.fragments()) {
		VariableDeclarationFragment vdf = (VariableDeclarationFragment) o;
		Type type = node.getType();
		if(vdf.getExtraDimensions() > 0){
			AST ast = AST.newAST(AST.JLS8);
			type = ast.newArrayType((Type) ASTNode.copySubtree(ast, type), vdf.getExtraDimensions());
		}
		map.put(vdf.getName().toString(), type);
	}
	return true;
}
 
Example #15
Source Project: DesigniteJava   Author: tushartushar   File: LocalVarVisitor.java    License: Apache License 2.0 5 votes vote down vote up
public boolean visit(VariableDeclarationStatement variable){
		for (Iterator iter = variable.fragments().iterator(); iter.hasNext();) {
			VariableDeclarationFragment fragment = (VariableDeclarationFragment) iter.next();
//			IVariableBinding binding = fragment.resolveBinding();
			
			SM_LocalVar newLocalVar = new SM_LocalVar(variable, fragment, parentMethod);
			//newLocalVar.setType(variable.getType());
			localVariables.add(newLocalVar);
		}
		return super.visit(variable);
	}
 
Example #16
Source Project: codemining-core   Author: mast-group   File: VariableScopeExtractor.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean visit(final VariableDeclarationStatement node) {
	final ASTNode parent = node.getParent();
	for (final Object fragment : node.fragments()) {
		final VariableDeclarationFragment frag = (VariableDeclarationFragment) fragment;
		variableScopes.put(parent, new Variable(frag.getName()
				.getIdentifier(), node.getType().toString(),
				ScopeType.SCOPE_LOCAL));
	}
	return false;
}
 
Example #17
Source Project: eclipse.jdt.ls   Author: eclipse   File: ExtractTempRefactoring.java    License: Eclipse Public License 2.0 5 votes vote down vote up
private VariableDeclarationStatement createTempDeclaration(Expression initializer) throws CoreException {
	AST ast = fCURewrite.getAST();

	VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment();
	vdf.setName(ast.newSimpleName(fTempName));
	vdf.setInitializer(initializer);

	VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);
	if (fDeclareFinal) {
		vds.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));
	}
	vds.setType(createTempType());

	if (fLinkedProposalModel != null) {
		ASTRewrite rewrite = fCURewrite.getASTRewrite();
		LinkedProposalPositionGroupCore nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true);
		nameGroup.addPosition(rewrite.track(vdf.getName()), true);

		String[] nameSuggestions = guessTempNames();
		if (nameSuggestions.length > 0 && !nameSuggestions[0].equals(fTempName)) {
			nameGroup.addProposal(fTempName, nameSuggestions.length + 1);
		}
		for (int i = 0; i < nameSuggestions.length; i++) {
			nameGroup.addProposal(nameSuggestions[i], nameSuggestions.length - i);
		}
	}
	return vds;
}
 
Example #18
Source Project: eclipse.jdt.ls   Author: eclipse   File: ExtractTempRefactoring.java    License: Eclipse Public License 2.0 5 votes vote down vote up
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));
}
 
Example #19
Source Project: eclipse.jdt.ls   Author: eclipse   File: ExtractMethodRefactoring.java    License: Eclipse Public License 2.0 5 votes vote down vote up
private VariableDeclarationStatement createDeclaration(IVariableBinding binding, Expression intilizer) {
	VariableDeclaration original = ASTNodes.findVariableDeclaration(binding, fAnalyzer.getEnclosingBodyDeclaration());
	VariableDeclarationFragment fragment = fAST.newVariableDeclarationFragment();
	fragment.setName((SimpleName) ASTNode.copySubtree(fAST, original.getName()));
	fragment.setInitializer(intilizer);
	VariableDeclarationStatement result = fAST.newVariableDeclarationStatement(fragment);
	result.modifiers().addAll(ASTNode.copySubtrees(fAST, ASTNodes.getModifiers(original)));
	result.setType(ASTNodeFactory.newType(fAST, original, fImportRewriter, new ContextSensitiveImportRewriteContext(original, fImportRewriter)));
	return result;
}
 
Example #20
Source Project: eclipse.jdt.ls   Author: eclipse   File: SurroundWithTryCatchRefactoring.java    License: Eclipse Public License 2.0 5 votes vote down vote up
private List<ASTNode> getSpecialVariableDeclarationStatements() {
	List<ASTNode> result = new ArrayList<>(3);
	VariableDeclaration[] locals = fAnalyzer.getAffectedLocals();
	for (int i = 0; i < locals.length; i++) {
		ASTNode parent = locals[i].getParent();
		if (parent instanceof VariableDeclarationStatement && !result.contains(parent)) {
			result.add(parent);
		}
	}
	return result;

}
 
Example #21
Source Project: JDeodorant   Author: tsantalis   File: ControlVariable.java    License: MIT License 5 votes vote down vote up
private static List<Expression> getAllFirstLevelUpdaters(Statement statement)
{
	List<Expression> updaters               = new ArrayList<Expression>();
	ExpressionExtractor expressionExtractor = new ExpressionExtractor();
	List<Statement> innerStatements         = new ArrayList<Statement>();
	innerStatements.add(statement);
	innerStatements = AbstractLoopUtilities.unBlock(innerStatements);
	// get all first level PrefixExpressions, PostfixExpressions, Assignments, and next() MethodInvocations from each inner statement
	for (Statement currentStatement : innerStatements)
	{
		// only updaters in an ExpressionStatment or VariableDeclaration are first level, unless a ConditionalExpression (handled in return statement)
		if (currentStatement instanceof ExpressionStatement || currentStatement instanceof VariableDeclarationStatement)
		{
			updaters.addAll(expressionExtractor.getPrefixExpressions(currentStatement));
			updaters.addAll(expressionExtractor.getPostfixExpressions(currentStatement));
			updaters.addAll(expressionExtractor.getAssignments(currentStatement));
			
			List<Expression> methodInvocations = expressionExtractor.getMethodInvocations(currentStatement);
			for (Expression currentExpression : methodInvocations)
			{
				if (currentExpression instanceof MethodInvocation)
				{
					MethodInvocation currentMethodInvocation      = (MethodInvocation) currentExpression;
					IMethodBinding currentMethodInvocationBinding = currentMethodInvocation.resolveMethodBinding();
					AbstractLoopBindingInformation bindingInformation = AbstractLoopBindingInformation.getInstance();
					if (bindingInformation.updateMethodValuesContains(currentMethodInvocationBinding.getMethodDeclaration().getKey()))
					{
						updaters.add(currentMethodInvocation);
					}
				}
			}
		}
	}
	return removeExpressionsInAConditionalExpression(updaters, statement);
}
 
Example #22
Source Project: xtext-xtend   Author: eclipse   File: JavaASTFlattener.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean visit(final Modifier it) {
  boolean append = true;
  int _flagValue = it.getKeyword().toFlagValue();
  switch (_flagValue) {
    case Modifier.PUBLIC:
      if (((it.getParent() instanceof TypeDeclaration) || (it.getParent() instanceof MethodDeclaration))) {
        append = false;
      }
      break;
    case Modifier.PRIVATE:
      ASTNode _parent = it.getParent();
      if ((_parent instanceof FieldDeclaration)) {
        append = false;
      }
      break;
    case Modifier.FINAL:
      if (((it.getParent() instanceof VariableDeclarationExpression) || (it.getParent() instanceof VariableDeclarationStatement))) {
        append = false;
      }
      break;
    default:
      append = true;
      break;
  }
  if (append) {
    String valueToAppend = it.getKeyword().toString();
    int _flagValue_1 = it.getKeyword().toFlagValue();
    boolean _equals = (_flagValue_1 == 0);
    if (_equals) {
      valueToAppend = "package";
    }
    this.appendToBuffer(valueToAppend);
    this.appendSpaceToBuffer();
  }
  return false;
}
 
Example #23
Source Project: xtext-xtend   Author: eclipse   File: JavaASTFlattener.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean visit(final VariableDeclarationStatement it) {
  boolean _isEmpty = IterableExtensions.isEmpty(Iterables.<Annotation>filter(it.modifiers(), Annotation.class));
  final boolean hasAnnotations = (!_isEmpty);
  final Consumer<VariableDeclarationFragment> _function = (VariableDeclarationFragment frag) -> {
    if (hasAnnotations) {
      this.appendToBuffer("/*FIXME Cannot add Annotation to Variable declaration. Java code: ");
    }
    final Function1<ASTNode, StringBuffer> _function_1 = (ASTNode it_1) -> {
      StringBuffer _xifexpression = null;
      if (hasAnnotations) {
        StringBuffer _xblockexpression = null;
        {
          this.appendToBuffer("*/");
          _xblockexpression = this.appendLineWrapToBuffer();
        }
        _xifexpression = _xblockexpression;
      }
      return _xifexpression;
    };
    this.appendModifiers(it, it.modifiers(), _function_1);
    this.appendToBuffer(this._aSTFlattenerUtils.handleVariableDeclaration(it.modifiers()));
    this.appendSpaceToBuffer();
    boolean _isMissingType = this.isMissingType(it.getType());
    boolean _not = (!_isMissingType);
    if (_not) {
      it.getType().accept(this);
    }
    this.appendExtraDimensions(frag.getExtraDimensions());
    this.appendSpaceToBuffer();
    frag.accept(this);
    this.appendSpaceToBuffer();
  };
  it.fragments().forEach(_function);
  return false;
}
 
Example #24
Source Project: Eclipse-Postfix-Code-Completion   Author: trylimits   File: UnusedCodeFix.java    License: Eclipse Public License 1.0 5 votes vote down vote up
private void splitUpDeclarations(ASTRewrite rewrite, TextEditGroup group, VariableDeclarationFragment frag, VariableDeclarationStatement originalStatement, List<Expression> sideEffects) {
	if (sideEffects.size() > 0) {
		ListRewrite statementRewrite= rewrite.getListRewrite(originalStatement.getParent(), (ChildListPropertyDescriptor) originalStatement.getLocationInParent());
		
		Statement previousStatement= originalStatement;
		for (int i= 0; i < sideEffects.size(); i++) {
			Expression sideEffect= sideEffects.get(i);
			Expression movedInit= (Expression) rewrite.createMoveTarget(sideEffect);
			ExpressionStatement wrapped= rewrite.getAST().newExpressionStatement(movedInit);
			statementRewrite.insertAfter(wrapped, previousStatement, group);
			previousStatement= wrapped;
		}

		VariableDeclarationStatement newDeclaration= null;
		List<VariableDeclarationFragment> fragments= originalStatement.fragments();
		int fragIndex= fragments.indexOf(frag);
		ListIterator<VariableDeclarationFragment> fragmentIterator= fragments.listIterator(fragIndex+1);
		while (fragmentIterator.hasNext()) {
			VariableDeclarationFragment currentFragment= fragmentIterator.next();
			VariableDeclarationFragment movedFragment= (VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment);
			if (newDeclaration == null) {
				newDeclaration= rewrite.getAST().newVariableDeclarationStatement(movedFragment);
				Type copiedType= (Type) rewrite.createCopyTarget(originalStatement.getType());
				newDeclaration.setType(copiedType);
			} else {
				newDeclaration.fragments().add(movedFragment);
			}
		}
		if (newDeclaration != null){
			statementRewrite.insertAfter(newDeclaration, previousStatement, group);
			if (originalStatement.fragments().size() == newDeclaration.fragments().size() + 1){
				rewrite.remove(originalStatement, group);
			}
		}
	}
}
 
Example #25
Source Project: KodeBeagle   Author: Imaginea   File: MethodInvocationResolver.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * If the passed node is a method invocation or class creation then return the
 * return type of the method based on what is the returned value assigned to.
 *
 * @param node
 * @return return type
 */
private String getReturnType(Expression node) {
    ASTNode parent = node.getParent();
    if (parent instanceof VariableDeclarationFragment) {
        ASTNode grandParent = parent.getParent();
        if (grandParent instanceof VariableDeclarationStatement) {
            Type typ = ((VariableDeclarationStatement) grandParent).getType();
            return removeSpecialSymbols(getFullyQualifiedNameFor(typ.toString()));
        }
    }
    return null;
}
 
Example #26
Source Project: api-mining   Author: mast-group   File: VariableScopeExtractor.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean visit(final VariableDeclarationStatement node) {
	final ASTNode parent = node.getParent();
	for (final Object fragment : node.fragments()) {
		final VariableDeclarationFragment frag = (VariableDeclarationFragment) fragment;
		variableScopes.put(parent, new Variable(frag.getName()
				.getIdentifier(), node.getType().toString(),
				ScopeType.SCOPE_LOCAL));
	}
	return false;
}
 
Example #27
Source Project: api-mining   Author: mast-group   File: TypenameScopeExtractor.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean visit(VariableDeclarationStatement node) {
	final String type = node.getType().toString();
	if (topMethod != null && methodsAsRoot) {
		types.put(topMethod, type);
	} else if (topClass != null) {
		types.put(topClass, type);
	}
	return super.visit(node);
}
 
Example #28
Source Project: api-mining   Author: mast-group   File: UsagePointExtractor.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean visit(final VariableDeclarationStatement node) {
	if (className.contains(node.getType().toString())) {
		interestingNodes.add(node.getParent());
	}
	return false;
}
 
Example #29
/**
 * @return a statement in form of <code>final int maxLen = 10;</code>
 */
protected VariableDeclarationStatement createMaxLenDeclaration() {
	VariableDeclarationFragment fragment= fAst.newVariableDeclarationFragment();
	fragment.setName(fAst.newSimpleName(fMaxLenVariableName));
	fragment.setInitializer(fAst.newNumberLiteral(String.valueOf(fContext.getLimitItemsValue())));
	VariableDeclarationStatement declExpression= fAst.newVariableDeclarationStatement(fragment);
	declExpression.setType(fAst.newPrimitiveType(PrimitiveType.INT));
	declExpression.modifiers().add(fAst.newModifier(ModifierKeyword.FINAL_KEYWORD));
	return declExpression;
}
 
Example #30
Source Project: api-mining   Author: mast-group   File: JavaApproximateTypeInferencer.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Looks for local variable declarations. For every declaration of a
 * variable, the parent {@link Block} denoting the variable's scope is
 * stored in {@link #variableScope} map.
 *
 * @param node
 *            the node to visit
 */
@Override
public boolean visit(final VariableDeclarationStatement node) {
	for (final Object fragment : node.fragments()) {
		final VariableDeclarationFragment frag = (VariableDeclarationFragment) fragment;
		addBinding(node, frag.getName().getIdentifier(), node.getType());
	}
	return true;
}