use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.
the class ImportReferencesCollector method possibleStaticImportFound.
private void possibleStaticImportFound(Name name) {
if (fStaticImports == null || fASTRoot == null) {
return;
}
while (name.isQualifiedName()) {
name = ((QualifiedName) name).getQualifier();
}
if (!isAffected(name)) {
return;
}
IBinding binding = name.resolveBinding();
SimpleName simpleName = (SimpleName) name;
if (binding == null || binding instanceof ITypeBinding || !Modifier.isStatic(binding.getModifiers()) || simpleName.isDeclaration()) {
return;
}
if (binding instanceof IVariableBinding) {
IVariableBinding varBinding = (IVariableBinding) binding;
if (varBinding.isField()) {
varBinding = varBinding.getVariableDeclaration();
ITypeBinding declaringClass = varBinding.getDeclaringClass();
if (declaringClass != null && !declaringClass.isLocal()) {
if (new ScopeAnalyzer(fASTRoot).isDeclaredInScope(varBinding, simpleName, ScopeAnalyzer.VARIABLES | ScopeAnalyzer.CHECK_VISIBILITY))
return;
fStaticImports.add(simpleName);
}
}
} else if (binding instanceof IMethodBinding) {
IMethodBinding methodBinding = ((IMethodBinding) binding).getMethodDeclaration();
ITypeBinding declaringClass = methodBinding.getDeclaringClass();
if (declaringClass != null && !declaringClass.isLocal()) {
if (new ScopeAnalyzer(fASTRoot).isDeclaredInScope(methodBinding, simpleName, ScopeAnalyzer.METHODS | ScopeAnalyzer.CHECK_VISIBILITY))
return;
fStaticImports.add(simpleName);
}
}
}
use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.
the class StubUtility method getBaseNameFromExpression.
private static String getBaseNameFromExpression(IJavaProject project, Expression assignedExpression, int variableKind) {
String name = null;
if (assignedExpression instanceof CastExpression) {
assignedExpression = ((CastExpression) assignedExpression).getExpression();
}
if (assignedExpression instanceof Name) {
Name simpleNode = (Name) assignedExpression;
IBinding binding = simpleNode.resolveBinding();
if (binding instanceof IVariableBinding)
return getBaseName((IVariableBinding) binding, project);
return ASTNodes.getSimpleNameIdentifier(simpleNode);
} else if (assignedExpression instanceof MethodInvocation) {
name = ((MethodInvocation) assignedExpression).getName().getIdentifier();
} else if (assignedExpression instanceof SuperMethodInvocation) {
name = ((SuperMethodInvocation) assignedExpression).getName().getIdentifier();
} else if (assignedExpression instanceof FieldAccess) {
return ((FieldAccess) assignedExpression).getName().getIdentifier();
} else if (variableKind == NamingConventions.VK_STATIC_FINAL_FIELD && (assignedExpression instanceof StringLiteral || assignedExpression instanceof NumberLiteral)) {
String string = assignedExpression instanceof StringLiteral ? ((StringLiteral) assignedExpression).getLiteralValue() : ((NumberLiteral) assignedExpression).getToken();
StringBuffer res = new StringBuffer();
boolean needsUnderscore = false;
for (int i = 0; i < string.length(); i++) {
char ch = string.charAt(i);
if (Character.isJavaIdentifierPart(ch)) {
if (res.length() == 0 && !Character.isJavaIdentifierStart(ch) || needsUnderscore) {
res.append('_');
}
res.append(ch);
needsUnderscore = false;
} else {
needsUnderscore = res.length() > 0;
}
}
if (res.length() > 0) {
return res.toString();
}
}
if (name != null) {
for (int i = 0; i < KNOWN_METHOD_NAME_PREFIXES.length; i++) {
String curr = KNOWN_METHOD_NAME_PREFIXES[i];
if (name.startsWith(curr)) {
if (name.equals(curr)) {
// don't suggest 'get' as variable name
return null;
} else if (Character.isUpperCase(name.charAt(curr.length()))) {
return name.substring(curr.length());
}
}
}
}
return name;
}
use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.
the class ModifierCorrectionSubProcessor method addNeedToEmulateProposal.
public static void addNeedToEmulateProposal(IInvocationContext context, IProblemLocation problem, Collection<ModifierChangeCorrectionProposal> proposals) {
ICompilationUnit cu = context.getCompilationUnit();
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (!(selectedNode instanceof SimpleName)) {
return;
}
IBinding binding = ((SimpleName) selectedNode).resolveBinding();
if (binding instanceof IVariableBinding) {
binding = ((IVariableBinding) binding).getVariableDeclaration();
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
String label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertofinal_description, BasicElementLabels.getJavaElementName(binding.getName()));
proposals.add(new ModifierChangeCorrectionProposal(label, cu, binding, selectedNode, Modifier.FINAL, 0, IProposalRelevance.CHANGE_MODIFIER_OF_VARIABLE_TO_FINAL, image));
}
}
use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.
the class AssignToVariableAssistProposal method doAddField.
private ASTRewrite doAddField() {
boolean isParamToField = fNodeToAssign.getNodeType() == ASTNode.SINGLE_VARIABLE_DECLARATION;
ASTNode newTypeDecl = ASTResolving.findParentType(fNodeToAssign);
if (newTypeDecl == null) {
return null;
}
Expression expression = isParamToField ? ((SingleVariableDeclaration) fNodeToAssign).getName() : ((ExpressionStatement) fNodeToAssign).getExpression();
AST ast = newTypeDecl.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
createImportRewrite((CompilationUnit) fNodeToAssign.getRoot());
BodyDeclaration bodyDecl = ASTResolving.findParentBodyDeclaration(fNodeToAssign);
Block body;
if (bodyDecl instanceof MethodDeclaration) {
body = ((MethodDeclaration) bodyDecl).getBody();
} else if (bodyDecl instanceof Initializer) {
body = ((Initializer) bodyDecl).getBody();
} else {
return null;
}
IJavaProject project = getCompilationUnit().getJavaProject();
boolean isAnonymous = newTypeDecl.getNodeType() == ASTNode.ANONYMOUS_CLASS_DECLARATION;
boolean isStatic = Modifier.isStatic(bodyDecl.getModifiers()) && !isAnonymous;
boolean isConstructorParam = isParamToField && fNodeToAssign.getParent() instanceof MethodDeclaration && ((MethodDeclaration) fNodeToAssign.getParent()).isConstructor();
int modifiers = Modifier.PRIVATE;
if (isStatic) {
modifiers |= Modifier.STATIC;
} else if (isConstructorParam) {
// String saveActionsKey= AbstractSaveParticipantPreferenceConfiguration.EDITOR_SAVE_PARTICIPANT_PREFIX + CleanUpPostSaveListener.POSTSAVELISTENER_ID;
// IScopeContext[] scopes= { InstanceScope.INSTANCE, new ProjectScope(project.getProject()) };
// boolean safeActionsEnabled= Platform.getPreferencesService().getBoolean(JavaPlugin.getPluginId(), saveActionsKey, false, scopes);
}
if (/*CleanUpOptions.TRUE.equals(PreferenceConstants.getPreference(
CleanUpPreferenceUtil.SAVE_PARTICIPANT_KEY_PREFIX + CleanUpConstants.CLEANUP_ON_SAVE_ADDITIONAL_OPTIONS, project))
&&*/
CleanUpOptions.TRUE.equals(PreferenceConstants.getPreference(CleanUpPreferenceUtil.SAVE_PARTICIPANT_KEY_PREFIX + CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL, project)) && CleanUpOptions.TRUE.equals(PreferenceConstants.getPreference(CleanUpPreferenceUtil.SAVE_PARTICIPANT_KEY_PREFIX + CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PRIVATE_FIELDS, project))) {
int constructors = 0;
if (newTypeDecl instanceof AbstractTypeDeclaration) {
List<BodyDeclaration> bodyDeclarations = ((AbstractTypeDeclaration) newTypeDecl).bodyDeclarations();
for (BodyDeclaration decl : bodyDeclarations) {
if (decl instanceof MethodDeclaration && ((MethodDeclaration) decl).isConstructor()) {
constructors++;
}
}
}
if (constructors == 1) {
modifiers |= Modifier.FINAL;
}
}
VariableDeclarationFragment newDeclFrag = addFieldDeclaration(rewrite, newTypeDecl, modifiers, expression);
String varName = newDeclFrag.getName().getIdentifier();
Assignment assignment = ast.newAssignment();
assignment.setRightHandSide((Expression) rewrite.createCopyTarget(expression));
boolean needsThis = StubUtility.useThisForFieldAccess(project);
if (isParamToField) {
needsThis |= varName.equals(((SimpleName) expression).getIdentifier());
}
SimpleName accessName = ast.newSimpleName(varName);
if (needsThis) {
FieldAccess fieldAccess = ast.newFieldAccess();
fieldAccess.setName(accessName);
if (isStatic) {
String typeName = ((AbstractTypeDeclaration) newTypeDecl).getName().getIdentifier();
fieldAccess.setExpression(ast.newSimpleName(typeName));
} else {
fieldAccess.setExpression(ast.newThisExpression());
}
assignment.setLeftHandSide(fieldAccess);
} else {
assignment.setLeftHandSide(accessName);
}
ASTNode selectionNode;
if (isParamToField) {
// assign parameter to field
ExpressionStatement statement = ast.newExpressionStatement(assignment);
int insertIdx = findAssignmentInsertIndex(body.statements());
rewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY).insertAt(statement, insertIdx, null);
selectionNode = statement;
} else {
if (needsSemicolon(expression)) {
rewrite.replace(expression, ast.newExpressionStatement(assignment), null);
} else {
rewrite.replace(expression, assignment, null);
}
selectionNode = fNodeToAssign;
}
addLinkedPosition(rewrite.track(newDeclFrag.getName()), false, KEY_NAME);
if (!isParamToField) {
FieldDeclaration fieldDeclaration = (FieldDeclaration) newDeclFrag.getParent();
addLinkedPosition(rewrite.track(fieldDeclaration.getType()), false, KEY_TYPE);
}
addLinkedPosition(rewrite.track(accessName), true, KEY_NAME);
IVariableBinding variableBinding = newDeclFrag.resolveBinding();
if (variableBinding != null) {
SimpleName[] linkedNodes = LinkedNodeFinder.findByBinding(fNodeToAssign.getRoot(), variableBinding);
for (int i = 0; i < linkedNodes.length; i++) {
addLinkedPosition(rewrite.track(linkedNodes[i]), false, KEY_NAME);
}
}
setEndPosition(rewrite.track(selectionNode));
return rewrite;
}
use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.
the class AssignToVariableAssistProposal method findAssignmentInsertIndex.
private int findAssignmentInsertIndex(List<Statement> statements) {
HashSet<String> paramsBefore = new HashSet<String>();
List<SingleVariableDeclaration> params = ((MethodDeclaration) fNodeToAssign.getParent()).parameters();
for (int i = 0; i < params.size() && (params.get(i) != fNodeToAssign); i++) {
SingleVariableDeclaration decl = params.get(i);
paramsBefore.add(decl.getName().getIdentifier());
}
int i = 0;
for (i = 0; i < statements.size(); i++) {
Statement curr = statements.get(i);
switch(curr.getNodeType()) {
case ASTNode.CONSTRUCTOR_INVOCATION:
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
break;
case ASTNode.EXPRESSION_STATEMENT:
Expression expr = ((ExpressionStatement) curr).getExpression();
if (expr instanceof Assignment) {
Assignment assignment = (Assignment) expr;
Expression rightHand = assignment.getRightHandSide();
if (rightHand instanceof SimpleName && paramsBefore.contains(((SimpleName) rightHand).getIdentifier())) {
IVariableBinding binding = Bindings.getAssignedVariable(assignment);
if (binding == null || binding.isField()) {
break;
}
}
}
return i;
default:
return i;
}
}
return i;
}
Aggregations