Java Code Examples for org.eclipse.jdt.core.dom.IVariableBinding#getName()

The following examples show how to use org.eclipse.jdt.core.dom.IVariableBinding#getName() . 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: ExtractMethodRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private void initializeParameterInfos() {
	IVariableBinding[] arguments = fAnalyzer.getArguments();
	fParameterInfos = new ArrayList<>(arguments.length);
	ASTNode root = fAnalyzer.getEnclosingBodyDeclaration();
	ParameterInfo vararg = null;
	for (int i = 0; i < arguments.length; i++) {
		IVariableBinding argument = arguments[i];
		if (argument == null) {
			continue;
		}
		VariableDeclaration declaration = ASTNodes.findVariableDeclaration(argument, root);
		boolean isVarargs = declaration instanceof SingleVariableDeclaration ? ((SingleVariableDeclaration) declaration).isVarargs() : false;
		ParameterInfo info = new ParameterInfo(argument, getType(declaration, isVarargs), argument.getName(), i);
		if (isVarargs) {
			vararg = info;
		} else {
			fParameterInfos.add(info);
		}
	}
	if (vararg != null) {
		fParameterInfos.add(vararg);
	}
}
 
Example 2
Source File: InJavaImporter.java    From jdt2famix with Eclipse Public License 1.0 6 votes vote down vote up
Attribute ensureAttributeForVariableBinding(IVariableBinding binding) {
	String name = binding.getName();
	ITypeBinding parentTypeBinding = binding.getDeclaringClass();
	Type parentType;
	if (parentTypeBinding == null)
		/*
		 * for example String[] args; args.length appears like an attribute, but the
		 * declaring class is not present
		 */
		parentType = unknownType();
	else
		parentType = ensureTypeFromTypeBinding(parentTypeBinding);
	String qualifiedName = Famix.qualifiedNameOf(parentType) + NAME_SEPARATOR + name;
	if (attributes.has(qualifiedName))
		return attributes.named(qualifiedName);
	Attribute attribute = ensureBasicAttribute(parentType, name, qualifiedName,
			ensureTypeFromTypeBinding(binding.getType()));
	return attribute;
}
 
Example 3
Source File: ExtractMethodRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void initializeParameterInfos() {
	IVariableBinding[] arguments= fAnalyzer.getArguments();
	fParameterInfos= new ArrayList<ParameterInfo>(arguments.length);
	ASTNode root= fAnalyzer.getEnclosingBodyDeclaration();
	ParameterInfo vararg= null;
	for (int i= 0; i < arguments.length; i++) {
		IVariableBinding argument= arguments[i];
		if (argument == null)
			continue;
		VariableDeclaration declaration= ASTNodes.findVariableDeclaration(argument, root);
		boolean isVarargs= declaration instanceof SingleVariableDeclaration
			? ((SingleVariableDeclaration)declaration).isVarargs()
			: false;
		ParameterInfo info= new ParameterInfo(argument, getType(declaration, isVarargs), argument.getName(), i);
		if (isVarargs) {
			vararg= info;
		} else {
			fParameterInfos.add(info);
		}
	}
	if (vararg != null) {
		fParameterInfos.add(vararg);
	}
}
 
Example 4
Source File: SrcTreeGenerator.java    From sahagin-java with Apache License 2.0 6 votes vote down vote up
private Code generateMethodArgCode(SimpleName simpleName,
        IVariableBinding paramVarBinding, TestMethod parentMethod) {
    int argIndex;
    if (parentMethod == null) {
        argIndex = -1;
    } else {
        String varName = paramVarBinding.getName();
        argIndex = parentMethod.getArgVariables().indexOf(varName);
    }

    if (argIndex == -1) {
        // when fails to resolve parameter variable
        return generateUnknownCode(simpleName);
    }

    MethodArgument methodArg = new MethodArgument();
    methodArg.setOriginal(simpleName.toString().trim());
    methodArg.setArgIndex(argIndex);
    return methodArg;
}
 
Example 5
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.4
 */
protected JvmField createField(StringBuilder typeName, IVariableBinding field) {
	JvmField result;
	if (!field.isEnumConstant()) {
		result = TypesFactory.eINSTANCE.createJvmField();
		Object constantValue = field.getConstantValue();
		if (constantValue != null) {
			result.setConstant(true);
			result.setConstantValue(constantValue);
		} else {
			result.setConstant(false);
		}
	} else
		result = TypesFactory.eINSTANCE.createJvmEnumerationLiteral();
	String name = field.getName();
	result.internalSetIdentifier(typeName.append(name).toString());
	result.setSimpleName(name);
	int modifiers = field.getModifiers();
	result.setFinal(Modifier.isFinal(modifiers));
	result.setStatic(Modifier.isStatic(modifiers));
	result.setTransient(Modifier.isTransient(modifiers));
	result.setVolatile(Modifier.isVolatile(modifiers));
	result.setDeprecated(field.isDeprecated());
	setVisibility(result, modifiers);
	result.setType(createTypeReference(field.getType()));
	createAnnotationValues(field, result);
	return result;
}
 
Example 6
Source File: ExtractMethodRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private String getName(IVariableBinding binding) {
	for (Iterator<ParameterInfo> iter = fParameterInfos.iterator(); iter.hasNext();) {
		ParameterInfo info = iter.next();
		if (Bindings.equals(binding, info.getOldBinding())) {
			return info.getNewName();
		}
	}
	return binding.getName();
}
 
Example 7
Source File: ExtractMethodRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private String getName(IVariableBinding binding) {
	for (Iterator<ParameterInfo> iter= fParameterInfos.iterator(); iter.hasNext();) {
		ParameterInfo info= iter.next();
		if (Bindings.equals(binding, info.getOldBinding())) {
			return info.getNewName();
		}
	}
	return binding.getName();
}
 
Example 8
Source File: SrcTreeGenerator.java    From sahagin-java with Apache License 2.0 5 votes vote down vote up
private Code generateFieldCode(SimpleName simpleName,
        IVariableBinding localVarBinding) {
    String key = localVarBinding.getDeclaringClass().getBinaryName()
            + "." + localVarBinding.getName();
    TestField testField = fieldTable.getByKey(key);
    if (testField == null) {
        return generateUnknownCode(simpleName.toString().trim());
    }
    Field field = new Field();
    field.setFieldKey(testField.getKey());
    field.setField(testField);
    field.setOriginal(simpleName.toString().trim());
    field.setThisInstance(null); // TODO really null??
    return field;
}
 
Example 9
Source File: AbstractVariable.java    From JDeodorant with MIT License 5 votes vote down vote up
public AbstractVariable(VariableDeclaration name) {
	IVariableBinding variableBinding = name.resolveBinding();
	this.variableBindingKey = variableBinding.getKey();
	this.variableName = variableBinding.getName();
	this.variableType = variableBinding.getType().getQualifiedName();
	this.isField = variableBinding.isField();
	this.isParameter = variableBinding.isParameter();
	this.isStatic = (variableBinding.getModifiers() & Modifier.STATIC) != 0;
}
 
Example 10
Source File: JdtUtils.java    From j2cl with Apache License 2.0 4 votes vote down vote up
public static FieldDescriptor createFieldDescriptor(IVariableBinding variableBinding) {
  checkArgument(!isArrayLengthBinding(variableBinding));

  boolean isStatic = isStatic(variableBinding);
  Visibility visibility = getVisibility(variableBinding);
  DeclaredTypeDescriptor enclosingTypeDescriptor =
      createDeclaredTypeDescriptor(variableBinding.getDeclaringClass());
  String fieldName = variableBinding.getName();

  TypeDescriptor thisTypeDescriptor =
      createTypeDescriptorWithNullability(
          variableBinding.getType(), variableBinding.getAnnotations());

  if (variableBinding.isEnumConstant()) {
    // Enum fields are always non-nullable.
    thisTypeDescriptor = thisTypeDescriptor.toNonNullable();
  }

  FieldDescriptor declarationFieldDescriptor = null;
  if (variableBinding.getVariableDeclaration() != variableBinding) {
    declarationFieldDescriptor = createFieldDescriptor(variableBinding.getVariableDeclaration());
  }

  JsInfo jsInfo = JsInteropUtils.getJsInfo(variableBinding);
  boolean isCompileTimeConstant = variableBinding.getConstantValue() != null;
  boolean isFinal = JdtUtils.isFinal(variableBinding);
  return FieldDescriptor.newBuilder()
      .setEnclosingTypeDescriptor(enclosingTypeDescriptor)
      .setName(fieldName)
      .setTypeDescriptor(thisTypeDescriptor)
      .setStatic(isStatic)
      .setVisibility(visibility)
      .setJsInfo(jsInfo)
      .setFinal(isFinal)
      .setCompileTimeConstant(isCompileTimeConstant)
      .setDeclarationDescriptor(declarationFieldDescriptor)
      .setEnumConstant(variableBinding.isEnumConstant())
      .setUnusableByJsSuppressed(
          JsInteropAnnotationUtils.isUnusableByJsSuppressed(variableBinding))
      .setDeprecated(isDeprecated(variableBinding))
      .build();
}
 
Example 11
Source File: JdtDomModels.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public LspVariableBinding(IVariableBinding binding) {
	this.bindingKey = binding.getKey();
	this.name = binding.getName();
	this.type = binding.getType().getName();
	this.isField = binding.isField();
}
 
Example 12
Source File: LocalCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static void addDeprecatedFieldsToMethodsProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());
	if (selectedNode instanceof Name) {
		IBinding binding= ((Name) selectedNode).resolveBinding();
		if (binding instanceof IVariableBinding) {
			IVariableBinding variableBinding= (IVariableBinding) binding;
			if (variableBinding.isField()) {
				String qualifiedName= variableBinding.getDeclaringClass().getTypeDeclaration().getQualifiedName();
				String fieldName= variableBinding.getName();
				String[] methodName= getMethod(JavaModelUtil.concatenateName(qualifiedName, fieldName));
				if (methodName != null) {
					AST ast= selectedNode.getAST();
					ASTRewrite astRewrite= ASTRewrite.create(ast);
					ImportRewrite importRewrite= StubUtility.createImportRewrite(context.getASTRoot(), true);

					MethodInvocation method= ast.newMethodInvocation();
					String qfn= importRewrite.addImport(methodName[0]);
					method.setExpression(ast.newName(qfn));
					method.setName(ast.newSimpleName(methodName[1]));
					ASTNode parent= selectedNode.getParent();
					ICompilationUnit cu= context.getCompilationUnit();
					// add explicit type arguments if necessary (for 1.8 and later, we're optimistic that inference just works):
					if (Invocations.isInvocationWithArguments(parent) && !JavaModelUtil.is18OrHigher(cu.getJavaProject())) {
						IMethodBinding methodBinding= Invocations.resolveBinding(parent);
						if (methodBinding != null) {
							ITypeBinding[] parameterTypes= methodBinding.getParameterTypes();
							int i= Invocations.getArguments(parent).indexOf(selectedNode);
							if (parameterTypes.length >= i && parameterTypes[i].isParameterizedType()) {
								ITypeBinding[] typeArguments= parameterTypes[i].getTypeArguments();
								for (int j= 0; j < typeArguments.length; j++) {
									ITypeBinding typeArgument= typeArguments[j];
									typeArgument= Bindings.normalizeForDeclarationUse(typeArgument, ast);
									if (! TypeRules.isJavaLangObject(typeArgument)) {
										// add all type arguments if at least one is found to be necessary:
										List<Type> typeArgumentsList= method.typeArguments();
										for (int k= 0; k < typeArguments.length; k++) {
											typeArgument= typeArguments[k];
											typeArgument= Bindings.normalizeForDeclarationUse(typeArgument, ast);
											typeArgumentsList.add(importRewrite.addImport(typeArgument, ast));
										}
										break;
									}
								}
							}
						}
					}
					
					astRewrite.replace(selectedNode, method, null);

					String label= Messages.format(CorrectionMessages.LocalCorrectionsSubProcessor_replacefieldaccesswithmethod_description, BasicElementLabels.getJavaElementName(ASTNodes.asString(method)));
					Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
					ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, cu, astRewrite, IProposalRelevance.REPLACE_FIELD_ACCESS_WITH_METHOD, image);
					proposal.setImportRewrite(importRewrite);
					proposals.add(proposal);
				}
			}
		}
	}
}
 
Example 13
Source File: AbstractVariable.java    From JDeodorant with MIT License 4 votes vote down vote up
public AbstractVariable(IVariableBinding variableBinding) {
	this(variableBinding.getKey(), variableBinding.getName(), variableBinding.getType().getQualifiedName(),
			variableBinding.isField(), variableBinding.isParameter(), (variableBinding.getModifiers() & Modifier.STATIC) != 0);
}
 
Example 14
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;
}