use of org.eclipse.jdt.core.dom.FieldAccess in project che by eclipse.
the class ExtractMethodAnalyzer method endVisit.
@Override
public void endVisit(CompilationUnit node) {
RefactoringStatus status = getStatus();
superCall: {
if (status.hasFatalError())
break superCall;
if (!hasSelectedNodes()) {
ASTNode coveringNode = getLastCoveringNode();
if (coveringNode instanceof Block && coveringNode.getParent() instanceof MethodDeclaration) {
MethodDeclaration methodDecl = (MethodDeclaration) coveringNode.getParent();
Message[] messages = ASTNodes.getMessages(methodDecl, ASTNodes.NODE_ONLY);
if (messages.length > 0) {
status.addFatalError(Messages.format(RefactoringCoreMessages.ExtractMethodAnalyzer_compile_errors, BasicElementLabels.getJavaElementName(methodDecl.getName().getIdentifier())), JavaStatusContext.create(fCUnit, methodDecl));
break superCall;
}
}
status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_invalid_selection);
break superCall;
}
fEnclosingBodyDeclaration = (BodyDeclaration) ASTNodes.getParent(getFirstSelectedNode(), BodyDeclaration.class);
if (fEnclosingBodyDeclaration == null || (fEnclosingBodyDeclaration.getNodeType() != ASTNode.METHOD_DECLARATION && fEnclosingBodyDeclaration.getNodeType() != ASTNode.FIELD_DECLARATION && fEnclosingBodyDeclaration.getNodeType() != ASTNode.INITIALIZER)) {
status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_invalid_selection);
break superCall;
} else if (ASTNodes.getEnclosingType(fEnclosingBodyDeclaration) == null) {
status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_compile_errors_no_parent_binding);
break superCall;
} else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
fEnclosingMethodBinding = ((MethodDeclaration) fEnclosingBodyDeclaration).resolveBinding();
}
if (!isSingleExpressionOrStatementSet()) {
status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_single_expression_or_set);
break superCall;
}
if (isExpressionSelected()) {
ASTNode expression = getFirstSelectedNode();
if (expression instanceof Name) {
Name name = (Name) expression;
if (name.resolveBinding() instanceof ITypeBinding) {
status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_type_reference);
break superCall;
}
if (name.resolveBinding() instanceof IMethodBinding) {
status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_method_name_reference);
break superCall;
}
if (name.resolveBinding() instanceof IVariableBinding) {
StructuralPropertyDescriptor locationInParent = name.getLocationInParent();
if (locationInParent == QualifiedName.NAME_PROPERTY || (locationInParent == FieldAccess.NAME_PROPERTY && !(((FieldAccess) name.getParent()).getExpression() instanceof ThisExpression))) {
status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_part_of_qualified_name);
break superCall;
}
}
if (name.isSimpleName() && ((SimpleName) name).isDeclaration()) {
status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_name_in_declaration);
break superCall;
}
}
fForceStatic = ASTNodes.getParent(expression, ASTNode.SUPER_CONSTRUCTOR_INVOCATION) != null || ASTNodes.getParent(expression, ASTNode.CONSTRUCTOR_INVOCATION) != null;
}
status.merge(LocalTypeAnalyzer.perform(fEnclosingBodyDeclaration, getSelection()));
computeLastStatementSelected();
}
super.endVisit(node);
}
use of org.eclipse.jdt.core.dom.FieldAccess in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method newFieldAssignment.
private Statement newFieldAssignment(AST ast, SimpleName fieldNameNode, Expression initializer, boolean useThisAccess) {
Assignment assignment = ast.newAssignment();
if (useThisAccess) {
FieldAccess access = ast.newFieldAccess();
access.setExpression(ast.newThisExpression());
access.setName(fieldNameNode);
assignment.setLeftHandSide(access);
} else {
assignment.setLeftHandSide(fieldNameNode);
}
assignment.setOperator(Assignment.Operator.ASSIGN);
assignment.setRightHandSide(initializer);
return ast.newExpressionStatement(assignment);
}
use of org.eclipse.jdt.core.dom.FieldAccess in project flux by eclipse.
the class ModifierCorrectionSubProcessor method addNonAccessibleReferenceProposal.
public static void addNonAccessibleReferenceProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals, int kind, int relevance) throws CoreException {
ICompilationUnit cu = context.getCompilationUnit();
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (selectedNode == null) {
return;
}
IBinding binding = null;
switch(selectedNode.getNodeType()) {
case ASTNode.SIMPLE_NAME:
binding = ((SimpleName) selectedNode).resolveBinding();
break;
case ASTNode.QUALIFIED_NAME:
binding = ((QualifiedName) selectedNode).resolveBinding();
break;
case ASTNode.SIMPLE_TYPE:
binding = ((SimpleType) selectedNode).resolveBinding();
break;
case ASTNode.NAME_QUALIFIED_TYPE:
binding = ((NameQualifiedType) selectedNode).resolveBinding();
break;
case ASTNode.METHOD_INVOCATION:
binding = ((MethodInvocation) selectedNode).getName().resolveBinding();
break;
case ASTNode.SUPER_METHOD_INVOCATION:
binding = ((SuperMethodInvocation) selectedNode).getName().resolveBinding();
break;
case ASTNode.FIELD_ACCESS:
binding = ((FieldAccess) selectedNode).getName().resolveBinding();
break;
case ASTNode.SUPER_FIELD_ACCESS:
binding = ((SuperFieldAccess) selectedNode).getName().resolveBinding();
break;
case ASTNode.CLASS_INSTANCE_CREATION:
binding = ((ClassInstanceCreation) selectedNode).resolveConstructorBinding();
break;
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
binding = ((SuperConstructorInvocation) selectedNode).resolveConstructorBinding();
break;
default:
return;
}
ITypeBinding typeBinding = null;
String name;
IBinding bindingDecl;
boolean isLocalVar = false;
if (binding instanceof IVariableBinding && problem.getProblemId() == IProblem.NotVisibleType) {
binding = ((IVariableBinding) binding).getType();
}
if (binding instanceof IMethodBinding && problem.getProblemId() == IProblem.NotVisibleType) {
binding = ((IMethodBinding) binding).getReturnType();
}
if (binding instanceof IMethodBinding) {
IMethodBinding methodDecl = (IMethodBinding) binding;
if (methodDecl.isDefaultConstructor()) {
//UnresolvedElementsSubProcessor.getConstructorProposals(context, problem, proposals);
return;
}
bindingDecl = methodDecl.getMethodDeclaration();
typeBinding = methodDecl.getDeclaringClass();
//$NON-NLS-1$
name = BasicElementLabels.getJavaElementName(methodDecl.getName() + "()");
} else if (binding instanceof IVariableBinding) {
IVariableBinding varDecl = (IVariableBinding) binding;
typeBinding = varDecl.getDeclaringClass();
name = BasicElementLabels.getJavaElementName(binding.getName());
isLocalVar = !varDecl.isField();
bindingDecl = varDecl.getVariableDeclaration();
} else if (binding instanceof ITypeBinding) {
typeBinding = (ITypeBinding) binding;
bindingDecl = typeBinding.getTypeDeclaration();
name = BasicElementLabels.getJavaElementName(binding.getName());
} else {
return;
}
if (typeBinding != null && typeBinding.isFromSource() || isLocalVar) {
int includedModifiers = 0;
int excludedModifiers = 0;
String label;
switch(kind) {
case TO_VISIBLE:
excludedModifiers = Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC;
includedModifiers = getNeededVisibility(selectedNode, typeBinding, binding);
label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changevisibility_description, new String[] { name, getVisibilityString(includedModifiers) });
break;
case TO_STATIC:
label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertostatic_description, name);
includedModifiers = Modifier.STATIC;
if (bindingDecl.getKind() == IBinding.METHOD) {
excludedModifiers = Modifier.DEFAULT | Modifier.ABSTRACT;
}
break;
case TO_NON_STATIC:
if (typeBinding != null && typeBinding.isInterface())
return;
label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertononstatic_description, name);
excludedModifiers = Modifier.STATIC;
break;
case TO_NON_PRIVATE:
int visibility;
if (cu.getParent().getElementName().equals(typeBinding.getPackage().getName())) {
visibility = Modifier.NONE;
excludedModifiers = Modifier.PRIVATE;
} else {
visibility = Modifier.PUBLIC;
includedModifiers = Modifier.PUBLIC;
excludedModifiers = Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC;
}
label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changevisibility_description, new String[] { name, getVisibilityString(visibility) });
break;
case TO_NON_FINAL:
if (typeBinding != null && typeBinding.isInterface())
return;
label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertononfinal_description, name);
excludedModifiers = Modifier.FINAL;
break;
default:
//$NON-NLS-1$
throw new IllegalArgumentException("not supported");
}
ICompilationUnit targetCU = isLocalVar ? cu : ASTResolving.findCompilationUnitForBinding(cu, context.getASTRoot(), typeBinding.getTypeDeclaration());
if (targetCU != null) {
//Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
proposals.add(new ModifierChangeCorrectionProposal(label, targetCU, bindingDecl, selectedNode, includedModifiers, excludedModifiers, relevance));
}
}
if (kind == TO_VISIBLE && bindingDecl.getKind() == IBinding.VARIABLE) {
// UnresolvedElementsSubProcessor.getVariableProposals(context, problem, (IVariableBinding) bindingDecl, proposals);
}
}
use of org.eclipse.jdt.core.dom.FieldAccess in project flux 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.FieldAccess in project processing by processing.
the class CompletionGenerator method resolveExpression3rdParty.
/**
* Finds the type of the expression in foo.bar().a().b, this would give me the
* type of b if it exists in return type of a(). If noCompare is true,
* it'll return type of a()
* @param nearestNode
* @param astNode
* @return
*/
public static ClassMember resolveExpression3rdParty(PreprocessedSketch ps, ASTNode nearestNode, ASTNode astNode, boolean noCompare) {
log("Resolve 3rdParty expr-- " + getNodeAsString(astNode) + " nearest node " + getNodeAsString(nearestNode));
if (astNode == null)
return null;
ClassMember scopeParent;
SimpleType stp;
if (astNode instanceof SimpleName) {
ASTNode decl = findDeclaration2(((SimpleName) astNode), nearestNode);
if (decl != null) {
// see if locally defined
log(getNodeAsString(astNode) + " found decl -> " + getNodeAsString(decl));
{
if (decl.getNodeType() == ASTNode.TYPE_DECLARATION) {
TypeDeclaration td = (TypeDeclaration) decl;
return new ClassMember(ps, td);
}
}
{
// Handle "array." x "array[1]."
Type type = extracTypeInfo2(decl);
if (type != null && type.isArrayType() && astNode.getParent().getNodeType() != ASTNode.ARRAY_ACCESS) {
// No array access, we want members of the array itself
Type elementType = ((ArrayType) type).getElementType();
// Get name of the element class
String name = "";
if (elementType.isSimpleType()) {
Class<?> c = findClassIfExists(ps, elementType.toString());
if (c != null)
name = c.getName();
} else if (elementType.isPrimitiveType()) {
name = ((PrimitiveType) elementType).getPrimitiveTypeCode().toString();
}
// Convert element class to array class
Class<?> arrayClass = getArrayClass(name, ps.classLoader);
return arrayClass == null ? null : new ClassMember(arrayClass);
}
}
return new ClassMember(ps, extracTypeInfo(decl));
} else {
// or in a predefined class?
Class<?> tehClass = findClassIfExists(ps, astNode.toString());
if (tehClass != null) {
return new ClassMember(tehClass);
}
}
astNode = astNode.getParent();
}
switch(astNode.getNodeType()) {
//TODO: Notice the redundancy in the 3 cases, you can simplify things even more.
case ASTNode.FIELD_ACCESS:
FieldAccess fa = (FieldAccess) astNode;
if (fa.getExpression() == null) {
// TODO: Check for existence of 'new' keyword. Could be a ClassInstanceCreation
// Local code or belongs to super class
log("FA,Not implemented.");
return null;
} else {
if (fa.getExpression() instanceof SimpleName) {
stp = extracTypeInfo(findDeclaration2((SimpleName) fa.getExpression(), nearestNode));
if (stp == null) {
/*The type wasn't found in local code, so it might be something like
* log(), or maybe belonging to super class, etc.
*/
Class<?> tehClass = findClassIfExists(ps, fa.getExpression().toString());
if (tehClass != null) {
// so look for method in this class.
return definedIn3rdPartyClass(ps, new ClassMember(tehClass), fa.getName().toString());
}
log("FA resolve 3rd par, Can't resolve " + fa.getExpression());
return null;
}
log("FA, SN Type " + getNodeAsString(stp));
scopeParent = definedIn3rdPartyClass(ps, stp.getName().toString(), "THIS");
} else {
scopeParent = resolveExpression3rdParty(ps, nearestNode, fa.getExpression(), noCompare);
}
log("FA, ScopeParent " + scopeParent);
return definedIn3rdPartyClass(ps, scopeParent, fa.getName().toString());
}
case ASTNode.METHOD_INVOCATION:
MethodInvocation mi = (MethodInvocation) astNode;
ASTNode temp = findDeclaration2(mi.getName(), nearestNode);
if (temp instanceof MethodDeclaration) {
// method is locally defined
log(mi.getName() + " was found locally," + getNodeAsString(extracTypeInfo(temp)));
{
// Handle "array." x "array[1]."
Type type = extracTypeInfo2(temp);
if (type != null && type.isArrayType() && astNode.getParent().getNodeType() != ASTNode.ARRAY_ACCESS) {
// No array access, we want members of the array itself
Type elementType = ((ArrayType) type).getElementType();
// Get name of the element class
String name = "";
if (elementType.isSimpleType()) {
Class<?> c = findClassIfExists(ps, elementType.toString());
if (c != null)
name = c.getName();
} else if (elementType.isPrimitiveType()) {
name = ((PrimitiveType) elementType).getPrimitiveTypeCode().toString();
}
// Convert element class to array class
Class<?> arrayClass = getArrayClass(name, ps.classLoader);
return arrayClass == null ? null : new ClassMember(arrayClass);
}
}
return new ClassMember(ps, extracTypeInfo(temp));
}
if (mi.getExpression() == null) {
// if()
//Local code or belongs to super class
log("MI,Not implemented.");
return null;
} else {
if (mi.getExpression() instanceof SimpleName) {
ASTNode decl = findDeclaration2((SimpleName) mi.getExpression(), nearestNode);
if (decl != null) {
if (decl.getNodeType() == ASTNode.TYPE_DECLARATION) {
TypeDeclaration td = (TypeDeclaration) decl;
return new ClassMember(ps, td);
}
stp = extracTypeInfo(decl);
if (stp == null) {
/*The type wasn't found in local code, so it might be something like
* System.console()., or maybe belonging to super class, etc.
*/
Class<?> tehClass = findClassIfExists(ps, mi.getExpression().toString());
if (tehClass != null) {
// so look for method in this class.
return definedIn3rdPartyClass(ps, new ClassMember(tehClass), mi.getName().toString());
}
log("MI resolve 3rd par, Can't resolve " + mi.getExpression());
return null;
}
log("MI, SN Type " + getNodeAsString(stp));
ASTNode typeDec = findDeclaration2(stp.getName(), nearestNode);
if (typeDec == null) {
log(stp.getName() + " couldn't be found locally..");
Class<?> tehClass = findClassIfExists(ps, stp.getName().toString());
if (tehClass != null) {
// so look for method in this class.
return definedIn3rdPartyClass(ps, new ClassMember(tehClass), mi.getName().toString());
}
//return new ClassMember(findClassIfExists(stp.getName().toString()));
}
//scopeParent = definedIn3rdPartyClass(stp.getName().toString(), "THIS");
return definedIn3rdPartyClass(ps, new ClassMember(ps, typeDec), mi.getName().toString());
}
} else {
log("MI EXP.." + getNodeAsString(mi.getExpression()));
// return null;
scopeParent = resolveExpression3rdParty(ps, nearestNode, mi.getExpression(), noCompare);
log("MI, ScopeParent " + scopeParent);
return definedIn3rdPartyClass(ps, scopeParent, mi.getName().toString());
}
}
break;
case ASTNode.QUALIFIED_NAME:
QualifiedName qn = (QualifiedName) astNode;
ASTNode temp2 = findDeclaration2(qn.getName(), nearestNode);
if (temp2 instanceof FieldDeclaration) {
// field is locally defined
log(qn.getName() + " was found locally," + getNodeAsString(extracTypeInfo(temp2)));
return new ClassMember(ps, extracTypeInfo(temp2));
}
if (qn.getQualifier() == null) {
log("QN,Not implemented.");
return null;
} else {
if (qn.getQualifier() instanceof SimpleName) {
stp = extracTypeInfo(findDeclaration2(qn.getQualifier(), nearestNode));
if (stp == null) {
/*The type wasn't found in local code, so it might be something like
* log(), or maybe belonging to super class, etc.
*/
Class<?> tehClass = findClassIfExists(ps, qn.getQualifier().toString());
if (tehClass != null) {
// note how similar thing is called on line 690. Check check.
return definedIn3rdPartyClass(ps, new ClassMember(tehClass), qn.getName().toString());
}
log("QN resolve 3rd par, Can't resolve " + qn.getQualifier());
return null;
}
log("QN, SN Local Type " + getNodeAsString(stp));
//scopeParent = definedIn3rdPartyClass(stp.getName().toString(), "THIS");
ASTNode typeDec = findDeclaration2(stp.getName(), nearestNode);
if (typeDec == null) {
log(stp.getName() + " couldn't be found locally..");
Class<?> tehClass = findClassIfExists(ps, stp.getName().toString());
if (tehClass != null) {
// note how similar thing is called on line 690. Check check.
return definedIn3rdPartyClass(ps, new ClassMember(tehClass), qn.getName().toString());
}
log("QN resolve 3rd par, Can't resolve " + qn.getQualifier());
return null;
}
return definedIn3rdPartyClass(ps, new ClassMember(ps, typeDec), qn.getName().toString());
} else {
scopeParent = resolveExpression3rdParty(ps, nearestNode, qn.getQualifier(), noCompare);
log("QN, ScopeParent " + scopeParent);
return definedIn3rdPartyClass(ps, scopeParent, qn.getName().toString());
}
}
case ASTNode.ARRAY_ACCESS:
ArrayAccess arac = (ArrayAccess) astNode;
return resolveExpression3rdParty(ps, nearestNode, arac.getArray(), noCompare);
default:
log("Unaccounted type " + getNodeAsString(astNode));
break;
}
return null;
}
Aggregations