Java Code Examples for org.eclipse.jdt.core.dom.SingleVariableDeclaration#resolveBinding()

The following examples show how to use org.eclipse.jdt.core.dom.SingleVariableDeclaration#resolveBinding() . 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: FlowContext.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
boolean isExceptionCaught(ITypeBinding excpetionType) {
	for (Iterator<List<CatchClause>> exceptions= fExceptionStack.iterator(); exceptions.hasNext(); ) {
		for (Iterator<CatchClause> catchClauses= exceptions.next().iterator(); catchClauses.hasNext(); ) {
			SingleVariableDeclaration caughtException= catchClauses.next().getException();
			IVariableBinding binding= caughtException.resolveBinding();
			if (binding == null) {
				continue;
			}
			ITypeBinding caughtype= binding.getType();
			while (caughtype != null) {
				if (caughtype == excpetionType) {
					return true;
				}
				caughtype= caughtype.getSuperclass();
			}
		}
	}
	return false;
}
 
Example 2
Source File: InJavaImporter.java    From jdt2famix with Eclipse Public License 1.0 6 votes vote down vote up
public Parameter ensureParameterFromSingleVariableDeclaration(SingleVariableDeclaration variableDeclaration,
		Method method) {
	String name = variableDeclaration.getName().toString();
	String qualifiedName = Famix.qualifiedNameOf(method) + NAME_SEPARATOR + name;
	if (parameters.has(qualifiedName))
		return parameters.named(qualifiedName);
	Parameter parameter = new Parameter();
	parameters.add(qualifiedName, parameter);
	parameter.setName(name);
	parameter.setParentBehaviouralEntity(method);
	parameter.setDeclaredType(ensureTypeFromDomType(variableDeclaration.getType()));
	IVariableBinding binding = variableDeclaration.resolveBinding();
	if (binding != null) {
		// We only recover the final modifier
		if (Modifier.isFinal(binding.getModifiers()))
			parameter.addModifiers("final");
	}
	return parameter;
}
 
Example 3
Source File: SourceAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public void initialize() {
	Block body= fDeclaration.getBody();
	// first collect the static imports. This is necessary to not mark
	// static imported fields and methods as implicit visible.
	fTypesToImport= new ArrayList<SimpleName>();
	fStaticsToImport= new ArrayList<SimpleName>();
	ImportReferencesCollector.collect(body, fTypeRoot.getJavaProject(), null, fTypesToImport, fStaticsToImport);

	// Now collect implicit references and name references
	body.accept(new UpdateCollector());

	int numberOfLocals= LocalVariableIndex.perform(fDeclaration);
	FlowContext context= new FlowContext(0, numberOfLocals + 1);
	context.setConsiderAccessMode(true);
	context.setComputeMode(FlowContext.MERGE);
	InOutFlowAnalyzer flowAnalyzer= new InOutFlowAnalyzer(context);
	FlowInfo info= flowAnalyzer.perform(getStatements());

	for (Iterator<SingleVariableDeclaration> iter= fDeclaration.parameters().iterator(); iter.hasNext();) {
		SingleVariableDeclaration element= iter.next();
		IVariableBinding binding= element.resolveBinding();
		ParameterData data= (ParameterData)element.getProperty(ParameterData.PROPERTY);
		data.setAccessMode(info.getAccessMode(context, binding));
	}
}
 
Example 4
Source File: FlowContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
boolean isExceptionCaught(ITypeBinding excpetionType) {
	for (Iterator<List<CatchClause>> exceptions= fExceptionStack.iterator(); exceptions.hasNext(); ) {
		for (Iterator<CatchClause> catchClauses= exceptions.next().iterator(); catchClauses.hasNext(); ) {
			SingleVariableDeclaration caughtException= catchClauses.next().getException();
			IVariableBinding binding= caughtException.resolveBinding();
			if (binding == null)
				continue;
			ITypeBinding caughtype= binding.getType();
			while (caughtype != null) {
				if (caughtype == excpetionType)
					return true;
				caughtype= caughtype.getSuperclass();
			}
		}
	}
	return false;
}
 
Example 5
Source File: JavaSourceFileParser.java    From BUILD_file_generator with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true iff 'methodDeclaration' represents a void static method named 'main' that takes a
 * single String[] parameter.
 */
private static boolean isMainMethod(MethodDeclaration methodDeclaration) {
  // Is it static?
  if ((methodDeclaration.getModifiers() & Modifier.STATIC) == 0) {
    return false;
  }
  // Does it return void?
  Type returnType = methodDeclaration.getReturnType2();
  if (!returnType.isPrimitiveType()) {
    return false;
  }
  if (((PrimitiveType) returnType).getPrimitiveTypeCode() != PrimitiveType.VOID) {
    return false;
  }
  // Is it called 'main'?
  if (!"main".equals(methodDeclaration.getName().getIdentifier())) {
    return false;
  }
  // Does it have a single parameter?
  if (methodDeclaration.parameters().size() != 1) {
    return false;
  }

  // Is the parameter's type String[]?
  SingleVariableDeclaration pt =
      getOnlyElement((List<SingleVariableDeclaration>) methodDeclaration.parameters());
  IVariableBinding vb = pt.resolveBinding();
  if (vb == null) {
    return false;
  }
  ITypeBinding tb = vb.getType();
  return tb != null && "java.lang.String[]".equals(tb.getQualifiedName());
}
 
Example 6
Source File: QuickAssistProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static boolean getAssignAllParamsToFieldsProposals(IInvocationContext context, ASTNode node, Collection<ChangeCorrectionProposal> resultingCollections) {
	node = ASTNodes.getNormalizedNode(node);
	ASTNode parent = node.getParent();
	if (!(parent instanceof SingleVariableDeclaration) || !(parent.getParent() instanceof MethodDeclaration)) {
		return false;
	}
	MethodDeclaration methodDecl = (MethodDeclaration) parent.getParent();
	if (methodDecl.getBody() == null) {
		return false;
	}
	List<SingleVariableDeclaration> parameters = methodDecl.parameters();
	if (parameters.size() <= 1) {
		return false;
	}
	ITypeBinding parentType = Bindings.getBindingOfParentType(node);
	if (parentType == null || parentType.isInterface()) {
		return false;
	}
	for (SingleVariableDeclaration param : parameters) {
		IVariableBinding binding = param.resolveBinding();
		if (binding == null || binding.getType() == null) {
			return false;
		}
	}
	if (resultingCollections == null) {
		return true;
	}

	AssignToVariableAssistProposal fieldProposal = new AssignToVariableAssistProposal(context.getCompilationUnit(), parameters, IProposalRelevance.ASSIGN_ALL_PARAMS_TO_NEW_FIELDS);
	resultingCollections.add(fieldProposal);
	return true;
}
 
Example 7
Source File: FlowAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void endVisit(SingleVariableDeclaration node) {
	if (skipNode(node)) {
		return;
	}

	IVariableBinding binding = node.resolveBinding();
	LocalFlowInfo nameInfo = null;
	Expression initializer = node.getInitializer();
	if (binding != null && !binding.isField() && initializer != null) {
		nameInfo = new LocalFlowInfo(binding, FlowInfo.WRITE, fFlowContext);
	}
	GenericSequentialFlowInfo info = processSequential(node, node.getType(), initializer);
	info.merge(nameInfo, fFlowContext);
}
 
Example 8
Source File: InferTypeArgumentsConstraintCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(CatchClause node) {
	SingleVariableDeclaration exception= node.getException();
	IVariableBinding variableBinding= exception.resolveBinding();
	VariableVariable2 cv= fTCModel.makeDeclaredVariableVariable(variableBinding, fCU);
	setConstraintVariable(exception, cv);
	return true;
}
 
Example 9
Source File: FlowAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void endVisit(SingleVariableDeclaration node) {
	if (skipNode(node))
		return;

	IVariableBinding binding= node.resolveBinding();
	LocalFlowInfo nameInfo= null;
	Expression initializer= node.getInitializer();
	if (binding != null && !binding.isField() && initializer != null) {
		nameInfo= new LocalFlowInfo(binding, FlowInfo.WRITE, fFlowContext);
	}
	GenericSequentialFlowInfo info= processSequential(node, node.getType(), initializer);
	info.merge(nameInfo, fFlowContext);
}
 
Example 10
Source File: QuickAssistProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private static boolean getAssignParamToFieldProposals(IInvocationContext context, ASTNode node, Collection<ChangeCorrectionProposal> resultingCollections) {
	node = ASTNodes.getNormalizedNode(node);
	ASTNode parent = node.getParent();
	if (!(parent instanceof SingleVariableDeclaration) || !(parent.getParent() instanceof MethodDeclaration)) {
		return false;
	}
	SingleVariableDeclaration paramDecl = (SingleVariableDeclaration) parent;
	IVariableBinding binding = paramDecl.resolveBinding();

	MethodDeclaration methodDecl = (MethodDeclaration) parent.getParent();
	if (binding == null || methodDecl.getBody() == null) {
		return false;
	}
	ITypeBinding typeBinding = binding.getType();
	if (typeBinding == null) {
		return false;
	}

	if (resultingCollections == null) {
		return true;
	}

	ITypeBinding parentType = Bindings.getBindingOfParentType(node);
	if (parentType != null) {
		if (parentType.isInterface()) {
			return false;
		}
		// assign to existing fields
		CompilationUnit root = context.getASTRoot();
		IVariableBinding[] declaredFields = parentType.getDeclaredFields();
		boolean isStaticContext = ASTResolving.isInStaticContext(node);
		for (int i = 0; i < declaredFields.length; i++) {
			IVariableBinding curr = declaredFields[i];
			if (isStaticContext == Modifier.isStatic(curr.getModifiers()) && typeBinding.isAssignmentCompatible(curr.getType())) {
				ASTNode fieldDeclFrag = root.findDeclaringNode(curr);
				if (fieldDeclFrag instanceof VariableDeclarationFragment) {
					VariableDeclarationFragment fragment = (VariableDeclarationFragment) fieldDeclFrag;
					if (fragment.getInitializer() == null) {
						resultingCollections.add(new AssignToVariableAssistProposal(context.getCompilationUnit(), paramDecl, fragment, typeBinding, IProposalRelevance.ASSIGN_PARAM_TO_EXISTING_FIELD));
					}
				}
			}
		}
	}

	AssignToVariableAssistProposal fieldProposal = new AssignToVariableAssistProposal(context.getCompilationUnit(), paramDecl, null, typeBinding, IProposalRelevance.ASSIGN_PARAM_TO_NEW_FIELD);
	resultingCollections.add(fieldProposal);
	return true;
}
 
Example 11
Source File: InOutFlowAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private void clearAccessMode(FlowInfo info, SingleVariableDeclaration decl) {
	IVariableBinding binding = decl.resolveBinding();
	if (binding != null && !binding.isField()) {
		info.clearAccessMode(binding, fFlowContext);
	}
}
 
Example 12
Source File: SuperTypeConstraintsCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public final void endVisit(final MethodDeclaration node) {
	fCurrentMethods.pop();
	final IMethodBinding binding= node.resolveBinding();
	if (binding != null) {
		if (!binding.isConstructor()) {
			final Type type= node.getReturnType2();
			if (type != null) {
				final ConstraintVariable2 first= fModel.createReturnTypeVariable(binding);
				final ConstraintVariable2 second= (ConstraintVariable2) type.getProperty(PROPERTY_CONSTRAINT_VARIABLE);
				if (first != null) {
					if (second != null)
						fModel.createEqualityConstraint(first, second);
					endVisit(binding);
				}
			}
		}
		ConstraintVariable2 ancestor= null;
		ConstraintVariable2 descendant= null;
		IVariableBinding variable= null;
		final List<SingleVariableDeclaration> parameters= node.parameters();
		if (!parameters.isEmpty()) {
			final Collection<IMethodBinding> originals= getOriginalMethods(binding);
			SingleVariableDeclaration declaration= null;
			for (int index= 0; index < parameters.size(); index++) {
				declaration= parameters.get(index);
				ancestor= fModel.createMethodParameterVariable(binding, index);
				if (ancestor != null) {
					descendant= (ConstraintVariable2) declaration.getType().getProperty(PROPERTY_CONSTRAINT_VARIABLE);
					if (descendant != null)
						fModel.createEqualityConstraint(descendant, ancestor);
					variable= declaration.resolveBinding();
					if (variable != null) {
						descendant= fModel.createVariableVariable(variable);
						if (descendant != null)
							fModel.createEqualityConstraint(ancestor, descendant);
					}
					IMethodBinding method= null;
					for (final Iterator<IMethodBinding> iterator= originals.iterator(); iterator.hasNext();) {
						method= iterator.next();
						if (!method.getKey().equals(binding.getKey())) {
							descendant= fModel.createMethodParameterVariable(method, index);
							if (descendant != null)
								fModel.createEqualityConstraint(ancestor, descendant);
						}
					}
				}
			}
		}
		final List<Type> exceptions= node.thrownExceptionTypes();
		if (!exceptions.isEmpty()) {
			final ITypeBinding throwable= node.getAST().resolveWellKnownType("java.lang.Throwable"); //$NON-NLS-1$
			if (throwable != null) {
				ancestor= fModel.createImmutableTypeVariable(throwable);
				if (ancestor != null) {
					Type exception= null;
					for (int index= 0; index < exceptions.size(); index++) {
						exception= exceptions.get(index);
						descendant= (ConstraintVariable2) exception.getProperty(PROPERTY_CONSTRAINT_VARIABLE);
						if (descendant != null)
							fModel.createSubtypeConstraint(descendant, ancestor);
					}
				}
			}
		}
	}
}
 
Example 13
Source File: InOutFlowAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void clearAccessMode(FlowInfo info, SingleVariableDeclaration decl) {
	IVariableBinding binding= decl.resolveBinding();
	if (binding != null && !binding.isField())
		info.clearAccessMode(binding, fFlowContext);
}
 
Example 14
Source File: QuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static boolean getAssignParamToFieldProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
	node= ASTNodes.getNormalizedNode(node);
	ASTNode parent= node.getParent();
	if (!(parent instanceof SingleVariableDeclaration) || !(parent.getParent() instanceof MethodDeclaration)) {
		return false;
	}
	SingleVariableDeclaration paramDecl= (SingleVariableDeclaration) parent;
	IVariableBinding binding= paramDecl.resolveBinding();

	MethodDeclaration methodDecl= (MethodDeclaration) parent.getParent();
	if (binding == null || methodDecl.getBody() == null) {
		return false;
	}
	ITypeBinding typeBinding= binding.getType();
	if (typeBinding == null) {
		return false;
	}

	if (resultingCollections == null) {
		return true;
	}

	ITypeBinding parentType= Bindings.getBindingOfParentType(node);
	if (parentType != null) {
		if (parentType.isInterface()) {
			return false;
		}
		// assign to existing fields
		CompilationUnit root= context.getASTRoot();
		IVariableBinding[] declaredFields= parentType.getDeclaredFields();
		boolean isStaticContext= ASTResolving.isInStaticContext(node);
		for (int i= 0; i < declaredFields.length; i++) {
			IVariableBinding curr= declaredFields[i];
			if (isStaticContext == Modifier.isStatic(curr.getModifiers()) && typeBinding.isAssignmentCompatible(curr.getType())) {
				ASTNode fieldDeclFrag= root.findDeclaringNode(curr);
				if (fieldDeclFrag instanceof VariableDeclarationFragment) {
					VariableDeclarationFragment fragment= (VariableDeclarationFragment) fieldDeclFrag;
					if (fragment.getInitializer() == null) {
						resultingCollections.add(new AssignToVariableAssistProposal(context.getCompilationUnit(), paramDecl, fragment, typeBinding, IProposalRelevance.ASSIGN_PARAM_TO_EXISTING_FIELD));
					}
				}
			}
		}
	}

	AssignToVariableAssistProposal fieldProposal= new AssignToVariableAssistProposal(context.getCompilationUnit(), paramDecl, null, typeBinding, IProposalRelevance.ASSIGN_PARAM_TO_NEW_FIELD);
	fieldProposal.setCommandId(ASSIGN_PARAM_TO_FIELD_ID);
	resultingCollections.add(fieldProposal);
	return true;
}
 
Example 15
Source File: TypeVisitor.java    From repositoryminer with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public boolean visit(MethodDeclaration node) {
	AbstractMethod method = new AbstractMethod();
	method.setConstructor(node.isConstructor());
	method.setVarargs(node.isVarargs());

	StringBuilder builder = null;
	builder = new StringBuilder();
	builder.append(node.getName().getIdentifier()).append("(");

	List<AbstractParameter> params = new ArrayList<>();
	for (SingleVariableDeclaration var : (List<SingleVariableDeclaration>) node.parameters()) {
		IVariableBinding varBind = var.resolveBinding();
		AbstractParameter param = new AbstractParameter(varBind.getType().getQualifiedName(), varBind.getName());
		params.add(param);
		builder.append(param.getType() + ",");
	}

	if (builder.substring(builder.length() - 1).equals(",")) {
		builder.replace(builder.length() - 1, builder.length(), ")");
	} else {
		builder.append(")");
	}

	method.setName(builder.toString());
	method.setParameters(params);
	verifyAccessorMethod(method);

	List<String> throwsList = new ArrayList<String>();
	List<Type> types = node.thrownExceptionTypes();
	for (Type type : types) {
		throwsList.add(type.toString());
	}
	method.setThrownsExceptions(throwsList);

	List<String> modifiers = new ArrayList<String>();
	for (Object modifier : node.modifiers()) {
		modifiers.add(modifier.toString());
	}
	method.setModifiers(modifiers);

	if (node.getBody() != null) {
		MethodVisitor visitor = new MethodVisitor();
		node.getBody().accept(visitor);
		method.setMaxDepth(visitor.getMaxDepth());
		method.setStatements(visitor.getStatements());
	} else {
		method.setStatements(new ArrayList<AbstractStatement>());
	}

	if (node.getReturnType2() != null) {
		ITypeBinding bind = node.getReturnType2().resolveBinding();
		if (bind != null)
		method.setReturnType(bind.getQualifiedName());
	}

	method.setStartPosition(node.getStartPosition());
	method.setEndPosition(node.getStartPosition() + node.getLength() - 1);
	
	methods.add(method);
	return true;
}