org.eclipse.jdt.internal.compiler.ast.LocalDeclaration Java Examples

The following examples show how to use org.eclipse.jdt.internal.compiler.ast.LocalDeclaration. 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: UnresolvedReferenceNameFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void removeLocals(Statement[] statements, int start, int end) {
	if (statements != null) {
		for (int i = 0; i < statements.length; i++) {
			if (statements[i] instanceof LocalDeclaration) {
				LocalDeclaration localDeclaration = (LocalDeclaration) statements[i];
				int j = indexOfFisrtNameAfter(start);
				done : while (j != -1) {
					int nameStart = this.potentialVariableNameStarts[j];
					if (start <= nameStart && nameStart <= end) {
						if (CharOperation.equals(this.potentialVariableNames[j], localDeclaration.name, false)) {
							removeNameAt(j);
						}
					}

					if (end < nameStart) break done;
					j = indexOfNextName(j);
				}
			}
		}

	}
}
 
Example #2
Source File: RecoveredInitializer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public RecoveredElement add(LocalDeclaration localDeclaration, int bracketBalanceValue) {

	/* do not consider a type starting passed the type end (if set)
		it must be belonging to an enclosing type */
	if (this.fieldDeclaration.declarationSourceEnd != 0
			&& localDeclaration.declarationSourceStart > this.fieldDeclaration.declarationSourceEnd){
		resetPendingModifiers();
		if (this.parent == null) return this; // ignore
		return this.parent.add(localDeclaration, bracketBalanceValue);
	}
	/* method body should have been created */
	Block block = new Block(0);
	block.sourceStart = ((Initializer)this.fieldDeclaration).sourceStart;
	RecoveredElement element = this.add(block, 1);
	if (this.initializerBody != null) {
		this.initializerBody.attachPendingModifiers(
				this.pendingAnnotations,
				this.pendingAnnotationCount,
				this.pendingModifiers,
				this.pendingModifersSourceStart);
	}
	resetPendingModifiers();
	return element.add(localDeclaration, bracketBalanceValue);
}
 
Example #3
Source File: DefaultBindingResolver.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
synchronized IVariableBinding resolveVariable(VariableDeclaration variable) {
	final Object node = this.newAstToOldAst.get(variable);
	if (node instanceof AbstractVariableDeclaration) {
		AbstractVariableDeclaration abstractVariableDeclaration = (AbstractVariableDeclaration) node;
		IVariableBinding variableBinding = null;
		if (abstractVariableDeclaration instanceof org.eclipse.jdt.internal.compiler.ast.FieldDeclaration) {
			org.eclipse.jdt.internal.compiler.ast.FieldDeclaration fieldDeclaration = (org.eclipse.jdt.internal.compiler.ast.FieldDeclaration) abstractVariableDeclaration;
			variableBinding = this.getVariableBinding(fieldDeclaration.binding, variable);
		} else {
			variableBinding = this.getVariableBinding(((LocalDeclaration) abstractVariableDeclaration).binding, variable);
		}
		if (variableBinding == null) {
			return null;
		}
		this.bindingsToAstNodes.put(variableBinding, variable);
		String key = variableBinding.getKey();
		if (key != null) {
			this.bindingTables.bindingKeysToBindings.put(key, variableBinding);
		}
		return variableBinding;
	}
	return null;
}
 
Example #4
Source File: EclipseAST.java    From EasyMPermission with MIT License 6 votes vote down vote up
/** {@inheritDoc} */
@Override protected EclipseNode buildTree(ASTNode node, Kind kind) {
	switch (kind) {
	case COMPILATION_UNIT:
		return buildCompilationUnit((CompilationUnitDeclaration) node);
	case TYPE:
		return buildType((TypeDeclaration) node);
	case FIELD:
		return buildField((FieldDeclaration) node);
	case INITIALIZER:
		return buildInitializer((Initializer) node);
	case METHOD:
		return buildMethod((AbstractMethodDeclaration) node);
	case ARGUMENT:
		return buildLocal((Argument) node, kind);
	case LOCAL:
		return buildLocal((LocalDeclaration) node, kind);
	case STATEMENT:
		return buildStatement((Statement) node);
	case ANNOTATION:
		return buildAnnotation((Annotation) node, false);
	default:
		throw new AssertionError("Did not expect to arrive here: " + kind);
	}
}
 
Example #5
Source File: EclipseAST.java    From EasyMPermission with MIT License 5 votes vote down vote up
private Collection<EclipseNode> buildArguments(Argument[] children) {
	List<EclipseNode> childNodes = new ArrayList<EclipseNode>();
	if (children != null) for (LocalDeclaration local : children) {
		addIfNotNull(childNodes, buildLocal(local, Kind.ARGUMENT));
	}
	return childNodes;
}
 
Example #6
Source File: InternalExtendedCompletionContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private JavaElement getJavaElement(LocalVariableBinding binding) {
	LocalDeclaration local = binding.declaration;

	JavaElement parent = null;
	ReferenceContext referenceContext = binding.declaringScope.referenceContext();
	if (referenceContext instanceof AbstractMethodDeclaration) {
		AbstractMethodDeclaration methodDeclaration = (AbstractMethodDeclaration) referenceContext;
		parent = this.getJavaElementOfCompilationUnit(methodDeclaration, methodDeclaration.binding);
	} else if (referenceContext instanceof TypeDeclaration){
		// Local variable is declared inside an initializer
		TypeDeclaration typeDeclaration = (TypeDeclaration) referenceContext;

		JavaElement type = this.getJavaElementOfCompilationUnit(typeDeclaration, typeDeclaration.binding);
		parent = Util.getUnresolvedJavaElement(local.sourceStart, local.sourceEnd, type);
	}
	if (parent == null) return null;

	return new LocalVariable(
			parent,
			new String(local.name),
			local.declarationSourceStart,
			local.declarationSourceEnd,
			local.sourceStart,
			local.sourceEnd,
			Util.typeSignature(local.type),
			binding.declaration.annotations,
			local.modifiers,
			local.getKind() == AbstractVariableDeclaration.PARAMETER);
}
 
Example #7
Source File: EclipseNode.java    From EasyMPermission with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
@Override protected boolean calculateIsStructurallySignificant(ASTNode parent) {
	if (node instanceof TypeDeclaration) return true;
	if (node instanceof AbstractMethodDeclaration) return true;
	if (node instanceof FieldDeclaration) return true;
	if (node instanceof LocalDeclaration) return true;
	if (node instanceof CompilationUnitDeclaration) return true;
	return false;
}
 
Example #8
Source File: EclipseNode.java    From EasyMPermission with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
@Override public String getName() {
	final char[] n;
	if (node instanceof TypeDeclaration) n = ((TypeDeclaration)node).name;
	else if (node instanceof FieldDeclaration) n = ((FieldDeclaration)node).name;
	else if (node instanceof AbstractMethodDeclaration) n = ((AbstractMethodDeclaration)node).selector;
	else if (node instanceof LocalDeclaration) n = ((LocalDeclaration)node).name;
	else n = null;
	
	return n == null ? null : new String(n);
}
 
Example #9
Source File: SelectionParser.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected void consumeLocalVariableDeclarationStatement() {
	super.consumeLocalVariableDeclarationStatement();

	// force to restart in recovery mode if the declaration contains the selection
	if (!this.diet) {
		LocalDeclaration localDeclaration = (LocalDeclaration) this.astStack[this.astPtr];
		if ((this.selectionStart >= localDeclaration.sourceStart)
				&&  (this.selectionEnd <= localDeclaration.sourceEnd)) {
			this.restartRecovery	= true;
			this.lastIgnoredToken = -1;
		}
	}
}
 
Example #10
Source File: SelectionParser.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected LocalDeclaration createLocalDeclaration(char[] assistName,int sourceStart,int sourceEnd) {
	if (this.indexOfAssistIdentifier() < 0) {
		return super.createLocalDeclaration(assistName, sourceStart, sourceEnd);
	} else {
		SelectionOnLocalName local = new SelectionOnLocalName(assistName, sourceStart, sourceEnd);
		this.assistNode = local;
		this.lastCheckPoint = sourceEnd + 1;
		return local;
	}
}
 
Example #11
Source File: HandleCleanup.java    From EasyMPermission with MIT License 5 votes vote down vote up
private void doAssignmentCheck0(EclipseNode node, Statement statement, char[] varName) {
	if (statement instanceof Assignment)
		doAssignmentCheck0(node, ((Assignment)statement).expression, varName);
	else if (statement instanceof LocalDeclaration)
		doAssignmentCheck0(node, ((LocalDeclaration)statement).initialization, varName);
	else if (statement instanceof CastExpression)
		doAssignmentCheck0(node, ((CastExpression)statement).expression, varName);
	else if (statement instanceof SingleNameReference) {
		if (Arrays.equals(((SingleNameReference)statement).token, varName)) {
			EclipseNode problemNode = node.getNodeFor(statement);
			if (problemNode != null) problemNode.addWarning(
					"You're assigning an auto-cleanup variable to something else. This is a bad idea.");
		}
	}
}
 
Example #12
Source File: HandleEqualsAndHashCode.java    From EasyMPermission with MIT License 5 votes vote down vote up
public LocalDeclaration createLocalDeclaration(ASTNode source, char[] dollarFieldName, TypeReference type, Expression initializer) {
	int pS = source.sourceStart, pE = source.sourceEnd;
	LocalDeclaration tempVar = new LocalDeclaration(dollarFieldName, pS, pE);
	setGeneratedBy(tempVar, source);
	tempVar.initialization = initializer;
	tempVar.type = type;
	tempVar.type.sourceStart = pS; tempVar.type.sourceEnd = pE;
	setGeneratedBy(tempVar.type, source);
	tempVar.modifiers = Modifier.FINAL;
	return tempVar;
}
 
Example #13
Source File: HandleVal.java    From EasyMPermission with MIT License 5 votes vote down vote up
@Override public void visitLocal(EclipseNode localNode, LocalDeclaration local) {
	if (!EclipseHandlerUtil.typeMatches(val.class, localNode, local.type)) return;
	handleFlagUsage(localNode, ConfigurationKeys.VAL_FLAG_USAGE, "val");
	
	boolean variableOfForEach = false;
	
	if (localNode.directUp().get() instanceof ForeachStatement) {
		ForeachStatement fs = (ForeachStatement) localNode.directUp().get();
		variableOfForEach = fs.elementVariable == local;
	}
	
	if (local.initialization == null && !variableOfForEach) {
		localNode.addError("'val' on a local variable requires an initializer expression");
		return;
	}
	
	if (local.initialization instanceof ArrayInitializer) {
		localNode.addError("'val' is not compatible with array initializer expressions. Use the full form (new int[] { ... } instead of just { ... })");
		return;
	}
	
	if (localNode.directUp().get() instanceof ForStatement) {
		localNode.addError("'val' is not allowed in old-style for loops");
		return;
	}
	
	if (local.initialization != null && local.initialization.getClass().getName().equals("org.eclipse.jdt.internal.compiler.ast.LambdaExpression")) {
		localNode.addError("'val' is not allowed with lambda expressions.");
	}
}
 
Example #14
Source File: AssistParser.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected void consumeEnhancedForStatementHeaderInit(boolean hasModifiers) {
	super.consumeEnhancedForStatementHeaderInit(hasModifiers);

	if (this.currentElement != null) {
		LocalDeclaration localDecl = ((ForeachStatement)this.astStack[this.astPtr]).elementVariable;
		this.lastCheckPoint = localDecl.sourceEnd + 1;
		this.currentElement = this.currentElement.add(localDecl, 0);
	}
}
 
Example #15
Source File: OrLocator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public int match(LocalDeclaration node, MatchingNodeSet nodeSet) {
	int level = IMPOSSIBLE_MATCH;
	for (int i = 0, length = this.patternLocators.length; i < length; i++) {
		int newLevel = this.patternLocators[i].match(node, nodeSet);
		if (newLevel > level) {
			if (newLevel == ACCURATE_MATCH) return ACCURATE_MATCH;
			level = newLevel;
		}
	}
	return level;
}
 
Example #16
Source File: AndLocator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public int match(LocalDeclaration node, MatchingNodeSet nodeSet) {
	int level = IMPOSSIBLE_MATCH;
	for (int i = 0, length = this.patternLocators.length; i < length; i++) {
		int newLevel = this.patternLocators[i].match(node, nodeSet);
		if (newLevel > level) {
			if (newLevel == ACCURATE_MATCH) return ACCURATE_MATCH;
			level = newLevel;
		}
	}
	return level;
}
 
Example #17
Source File: EclipseAST.java    From EasyMPermission with MIT License 5 votes vote down vote up
private EclipseNode buildLocal(LocalDeclaration local, Kind kind) {
	if (setAndGetAsHandled(local)) return null;
	List<EclipseNode> childNodes = new ArrayList<EclipseNode>();
	addIfNotNull(childNodes, buildStatement(local.initialization));
	childNodes.addAll(buildAnnotations(local.annotations, true));
	return putInMap(new EclipseNode(this, local, childNodes, kind));
}
 
Example #18
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 #19
Source File: RecoveredElement.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public RecoveredElement add(LocalDeclaration localDeclaration, int bracketBalanceValue) {

	/* default behavior is to delegate recording to parent if any */
	resetPendingModifiers();
	if (this.parent == null) return this; // ignore
	this.updateSourceEndIfNecessary(previousAvailableLineEnd(localDeclaration.declarationSourceStart - 1));
	return this.parent.add(localDeclaration, bracketBalanceValue);
}
 
Example #20
Source File: PatchVal.java    From EasyMPermission with MIT License 5 votes vote down vote up
public static TypeBinding skipResolveInitializerIfAlreadyCalled2(Expression expr, BlockScope scope, LocalDeclaration decl) {
	if (decl != null && LocalDeclaration.class.equals(decl.getClass()) && expr.resolvedType != null) return expr.resolvedType;
	try {
		return expr.resolveType(scope);
	} catch (NullPointerException e) {
		return null;
	}
}
 
Example #21
Source File: SetGeneratedByVisitor.java    From EasyMPermission with MIT License 4 votes vote down vote up
@Override public boolean visit(LocalDeclaration node, BlockScope scope) {
	fixPositions(setGeneratedBy(node, source));
	return super.visit(node, scope);
}
 
Example #22
Source File: RecoveredMethod.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public RecoveredElement add(LocalDeclaration localDeclaration, int bracketBalanceValue) {
	resetPendingModifiers();

	/* local variables inside method can only be final and non void */
/*
	char[][] localTypeName;
	if ((localDeclaration.modifiers & ~AccFinal) != 0 // local var can only be final
		|| (localDeclaration.type == null) // initializer
		|| ((localTypeName = localDeclaration.type.getTypeName()).length == 1 // non void
			&& CharOperation.equals(localTypeName[0], VoidBinding.sourceName()))){

		if (this.parent == null){
			return this; // ignore
		} else {
			this.updateSourceEndIfNecessary(this.previousAvailableLineEnd(localDeclaration.declarationSourceStart - 1));
			return this.parent.add(localDeclaration, bracketBalance);
		}
	}
*/
	/* do not consider a type starting passed the type end (if set)
		it must be belonging to an enclosing type */
	if (this.methodDeclaration.declarationSourceEnd != 0
		&& localDeclaration.declarationSourceStart > this.methodDeclaration.declarationSourceEnd){

		if (this.parent == null) {
			return this; // ignore
		} else {
			return this.parent.add(localDeclaration, bracketBalanceValue);
		}
	}
	if (this.methodBody == null){
		Block block = new Block(0);
		block.sourceStart = this.methodDeclaration.bodyStart;
		RecoveredElement currentBlock = this.add(block, 1);
		if (this.bracketBalance > 0){
			for (int i = 0; i < this.bracketBalance - 1; i++){
				currentBlock = currentBlock.add(new Block(0), 1);
			}
			this.bracketBalance = 1;
		}
		return currentBlock.add(localDeclaration, bracketBalanceValue);
	}
	return this.methodBody.add(localDeclaration, bracketBalanceValue, true);
}
 
Example #23
Source File: RecoveredLocalVariable.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public RecoveredLocalVariable(LocalDeclaration localDeclaration, RecoveredElement parent, int bracketBalance){
	super(localDeclaration, parent, bracketBalance);
	this.localDeclaration = localDeclaration;
	this.alreadyCompletedLocalInitialization = localDeclaration.initialization != null;
}
 
Example #24
Source File: RecoveredBlock.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public RecoveredElement add(LocalDeclaration localDeclaration, int bracketBalanceValue) {
	return this.add(localDeclaration, bracketBalanceValue, false);
}
 
Example #25
Source File: UnresolvedReferenceNameFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public void endVisit(LocalDeclaration localDeclaration, BlockScope blockScope) {
	endVisitRemoved(localDeclaration.declarationSourceStart, localDeclaration.sourceEnd);
}
 
Example #26
Source File: RecoveredBlock.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public RecoveredElement add(LocalDeclaration localDeclaration, int bracketBalanceValue, boolean delegatedByParent) {

	/* local variables inside method can only be final and non void */
/*
	char[][] localTypeName;
	if ((localDeclaration.modifiers & ~AccFinal) != 0 // local var can only be final
		|| (localDeclaration.type == null) // initializer
		|| ((localTypeName = localDeclaration.type.getTypeName()).length == 1 // non void
			&& CharOperation.equals(localTypeName[0], VoidBinding.sourceName()))){

		if (delegatedByParent){
			return this; //ignore
		} else {
			this.updateSourceEndIfNecessary(this.previousAvailableLineEnd(localDeclaration.declarationSourceStart - 1));
			return this.parent.add(localDeclaration, bracketBalance);
		}
	}
*/
		/* do not consider a local variable starting passed the block end (if set)
		it must be belonging to an enclosing block */
	if (this.blockDeclaration.sourceEnd != 0
			&& localDeclaration.declarationSourceStart > this.blockDeclaration.sourceEnd){
		resetPendingModifiers();
		if (delegatedByParent) return this; //ignore
		return this.parent.add(localDeclaration, bracketBalanceValue);
	}

	RecoveredLocalVariable element = new RecoveredLocalVariable(localDeclaration, this, bracketBalanceValue);

	if(this.pendingAnnotationCount > 0) {
		element.attach(
				this.pendingAnnotations,
				this.pendingAnnotationCount,
				this.pendingModifiers,
				this.pendingModifersSourceStart);
	}
	resetPendingModifiers();

	if (localDeclaration instanceof Argument){
		this.pendingArgument = element;
		return this;
	}

	attach(element);
	if (localDeclaration.declarationSourceEnd == 0) return element;
	return this;
}
 
Example #27
Source File: CatchParameterBinding.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public CatchParameterBinding(LocalDeclaration declaration, TypeBinding type, int modifiers, boolean isArgument) {
	super(declaration, type, modifiers, isArgument);
}
 
Example #28
Source File: LocalVariableBinding.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public LocalVariableBinding(LocalDeclaration declaration, TypeBinding type, int modifiers, boolean isArgument) {

		this(declaration.name, type, modifiers, isArgument);
		this.declaration = declaration;
	}
 
Example #29
Source File: LocalVariableBinding.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public LocalVariableBinding(LocalDeclaration declaration, TypeBinding type, int modifiers, MethodScope declaringScope) {

		this(declaration, type, modifiers, true);
		this.declaringScope = declaringScope;
	}
 
Example #30
Source File: EclipseASTVisitor.java    From EasyMPermission with MIT License 4 votes vote down vote up
public void visitAnnotationOnLocal(LocalDeclaration local, EclipseNode node, Annotation annotation) {
	print("<ANNOTATION%s: %s />", isGenerated(annotation) ? " (GENERATED)" : "", annotation);
}