Java Code Examples for org.eclipse.jdt.core.dom.ITypeBinding#getDeclaredFields()

The following examples show how to use org.eclipse.jdt.core.dom.ITypeBinding#getDeclaredFields() . 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: ScopeAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean visit(SwitchCase node) {
	// switch on enum allows to use enum constants without qualification
	if (hasFlag(VARIABLES, fFlags) && !node.isDefault() && isInside(node.getExpression())) {
		SwitchStatement switchStatement= (SwitchStatement) node.getParent();
		ITypeBinding binding= switchStatement.getExpression().resolveTypeBinding();
		if (binding != null && binding.isEnum()) {
			IVariableBinding[] declaredFields= binding.getDeclaredFields();
			for (int i= 0; i < declaredFields.length; i++) {
				IVariableBinding curr= declaredFields[i];
				if (curr.isEnumConstant()) {
					fBreak= fRequestor.acceptBinding(curr);
					if (fBreak)
						return false;
				}
			}
		}
	}
	return false;
}
 
Example 2
Source File: LocalCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static boolean evaluateMissingSwitchCases(ITypeBinding enumBindings, List<Statement> switchStatements, ArrayList<String> enumConstNames) {
	IVariableBinding[] fields= enumBindings.getDeclaredFields();
	for (int i= 0; i < fields.length; i++) {
		if (fields[i].isEnumConstant()) {
			enumConstNames.add(fields[i].getName());
		}
	}

	boolean hasDefault=false;
	List<Statement> statements= switchStatements;
	for (int i= 0; i < statements.size(); i++) {
		Statement curr= statements.get(i);
		if (curr instanceof SwitchCase) {
			Expression expression= ((SwitchCase) curr).getExpression();
			if (expression instanceof SimpleName) {
				enumConstNames.remove(((SimpleName) expression).getFullyQualifiedName());
			} else if(expression== null){
				hasDefault=true;
			}
		}
	}
	return hasDefault;
}
 
Example 3
Source File: PreconditionExaminer.java    From JDeodorant with MIT License 6 votes vote down vote up
private Set<IVariableBinding> getDeclaredFields(ITypeBinding typeBinding) {
	Set<IVariableBinding> declaredFields = new LinkedHashSet<IVariableBinding>();
	//first add the directly declared methods
	for(IVariableBinding variableBinding : typeBinding.getDeclaredFields()) {
		declaredFields.add(variableBinding);
	}
	ITypeBinding superclassTypeBinding = typeBinding.getSuperclass();
	if(superclassTypeBinding != null) {
		declaredFields.addAll(getDeclaredFields(superclassTypeBinding));
	}
	ITypeBinding[] interfaces = typeBinding.getInterfaces();
	for(ITypeBinding interfaceTypeBinding : interfaces) {
		declaredFields.addAll(getDeclaredFields(interfaceTypeBinding));
	}
	return declaredFields;
}
 
Example 4
Source File: UnresolvedElementsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static boolean hasMethodWithName(ITypeBinding typeBinding, String name) {
	IVariableBinding[] fields= typeBinding.getDeclaredFields();
	for (int i= 0; i < fields.length; i++) {
		if (fields[i].getName().equals(name)) {
			return true;
		}
	}
	ITypeBinding superclass= typeBinding.getSuperclass();
	if (superclass != null) {
		return hasMethodWithName(superclass, name);
	}
	return false;
}
 
Example 5
Source File: MoveInstanceMethodProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates a new read only field finder.
 *
 * @param binding
 *            The declaring class of the method declaring to find fields
 *            for
 */
public ReadyOnlyFieldFinder(final ITypeBinding binding) {
	Assert.isNotNull(binding);
	final IVariableBinding[] bindings= binding.getDeclaredFields();
	IVariableBinding variable= null;
	for (int index= 0; index < bindings.length; index++) {
		variable= bindings[index];
		if (!variable.isSynthetic() && !fFound.contains(variable.getKey())) {
			fFound.add(variable.getKey());
			fBindings.add(variable);
		}
	}
}
 
Example 6
Source File: Bindings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Finds the field specified by <code>fieldName<code> in
 * the given <code>type</code>. Returns <code>null</code> if no such field exits.
 * @param type the type to search the field in
 * @param fieldName the field name
 * @return the binding representing the field or <code>null</code>
 */
public static IVariableBinding findFieldInType(ITypeBinding type, String fieldName) {
	if (type.isPrimitive())
		return null;
	IVariableBinding[] fields= type.getDeclaredFields();
	for (int i= 0; i < fields.length; i++) {
		IVariableBinding field= fields[i];
		if (field.getName().equals(fieldName))
			return field;
	}
	return null;
}
 
Example 7
Source File: ScopeAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IVariableBinding[] getEnumContants(ITypeBinding binding) {
	IVariableBinding[] declaredFields= binding.getDeclaredFields();
	ArrayList<IVariableBinding> res= new ArrayList<IVariableBinding>(declaredFields.length);
	for (int i= 0; i < declaredFields.length; i++) {
		IVariableBinding curr= declaredFields[i];
		if (curr.isEnumConstant()) {
			res.add(curr);
		}
	}
	return res.toArray(new IVariableBinding[res.size()]);
}
 
Example 8
Source File: ScopeAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean hasEnumContants(IBinding declaration, ITypeBinding binding) {
	IVariableBinding[] declaredFields= binding.getDeclaredFields();
	for (int i= 0; i < declaredFields.length; i++) {
		IVariableBinding curr= declaredFields[i];
		if (curr == declaration)
			return true;
	}
	return false;
}
 
Example 9
Source File: AbstractToStringGenerator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected String createNameSuggestion(String baseName, int variableKind) {
	if (excluded == null) {
		excluded= new HashSet<String>();
		IVariableBinding[] fields= fContext.getTypeBinding().getDeclaredFields();
		for (int i= 0; i < fields.length; i++) {
			excluded.add(fields[i].getName());
		}
		ITypeBinding superType= fContext.getTypeBinding().getSuperclass();
		while (superType != null) {
			fields= superType.getDeclaredFields();
			for (int i= 0; i < fields.length; i++) {
				if (!Modifier.isPrivate(fields[i].getModifiers())) {
					excluded.add(fields[i].getName());
				}
			}
			superType= superType.getSuperclass();
		}
		ITypeBinding[] types= fContext.getTypeBinding().getDeclaredTypes();
		for (int i= 0; i < types.length; i++) {
			excluded.add(types[i].getName());
		}
		superType= fContext.getTypeBinding().getSuperclass();
		while (superType != null) {
			types= superType.getDeclaredTypes();
			for (int i= 0; i < types.length; i++) {
				if (!Modifier.isPrivate(types[i].getModifiers())) {
					excluded.add(types[i].getName());
				}
			}
			superType= superType.getSuperclass();
		}
	}
	return StubUtility.getVariableNameSuggestions(variableKind, fContext.getCompilationUnit().getJavaProject(), baseName, 0, excluded, true)[0];
}
 
Example 10
Source File: StubUtility2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static DelegateEntry[] getDelegatableMethods(ITypeBinding binding) {
	final List<DelegateEntry> tuples= new ArrayList<DelegateEntry>();
	final List<IMethodBinding> declared= new ArrayList<IMethodBinding>();
	IMethodBinding[] typeMethods= binding.getDeclaredMethods();
	for (int index= 0; index < typeMethods.length; index++)
		declared.add(typeMethods[index]);
	IVariableBinding[] typeFields= binding.getDeclaredFields();
	for (int index= 0; index < typeFields.length; index++) {
		IVariableBinding fieldBinding= typeFields[index];
		if (fieldBinding.isField() && !fieldBinding.isEnumConstant() && !fieldBinding.isSynthetic())
			getDelegatableMethods(new ArrayList<IMethodBinding>(declared), fieldBinding, fieldBinding.getType(), binding, tuples);
	}
	// list of tuple<IVariableBinding, IMethodBinding>
	return tuples.toArray(new DelegateEntry[tuples.size()]);
}
 
Example 11
Source File: UnresolvedElementsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean hasMethodWithName(ITypeBinding typeBinding, String name) {
	IVariableBinding[] fields= typeBinding.getDeclaredFields();
	for (int i= 0; i < fields.length; i++) {
		if (fields[i].getName().equals(name)) {
			return true;
		}
	}
	ITypeBinding superclass= typeBinding.getSuperclass();
	if (superclass != null) {
		return hasMethodWithName(superclass, name);
	}
	return false;
}
 
Example 12
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 13
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;
}