use of org.eclipse.jdt.core.dom.FieldAccess in project processing by processing.
the class CompletionGenerator method findDeclaration2.
/**
* A variation of findDeclaration() but accepts an alternate parent ASTNode
* @param findMe
* @param alternateParent
* @return
*/
protected static ASTNode findDeclaration2(Name findMe, ASTNode alternateParent) {
ASTNode declaringClass;
ASTNode parent = findMe.getParent();
ASTNode ret;
ArrayList<Integer> constrains = new ArrayList<>();
if (parent.getNodeType() == ASTNode.METHOD_INVOCATION) {
Expression exp = (Expression) parent.getStructuralProperty(MethodInvocation.EXPRESSION_PROPERTY);
// Possibly a bug here. Investigate later.
if (((MethodInvocation) parent).getName().toString().equals(findMe.toString())) {
constrains.add(ASTNode.METHOD_DECLARATION);
if (exp != null) {
constrains.add(ASTNode.TYPE_DECLARATION);
// + exp.getClass().getName() + " parent: " + exp.getParent());
if (exp instanceof MethodInvocation) {
SimpleType stp = extracTypeInfo(findDeclaration2(((MethodInvocation) exp).getName(), alternateParent));
if (stp == null)
return null;
declaringClass = findDeclaration2(stp.getName(), alternateParent);
return definedIn(declaringClass, ((MethodInvocation) parent).getName().toString(), constrains);
} else if (exp instanceof FieldAccess) {
SimpleType stp = extracTypeInfo(findDeclaration2(((FieldAccess) exp).getName(), alternateParent));
if (stp == null)
return null;
declaringClass = findDeclaration2((stp.getName()), alternateParent);
return definedIn(declaringClass, ((MethodInvocation) parent).getName().toString(), constrains);
}
if (exp instanceof SimpleName) {
SimpleType stp = extracTypeInfo(findDeclaration2(((SimpleName) exp), alternateParent));
if (stp == null)
return null;
declaringClass = findDeclaration2(stp.getName(), alternateParent);
// log("MI.SN " + getNodeAsString(declaringClass));
constrains.add(ASTNode.METHOD_DECLARATION);
return definedIn(declaringClass, ((MethodInvocation) parent).getName().toString(), constrains);
}
}
} else {
// Move one up the ast. V V IMP!!
parent = parent.getParent();
alternateParent = alternateParent.getParent();
}
} else if (parent.getNodeType() == ASTNode.FIELD_ACCESS) {
FieldAccess fa = (FieldAccess) parent;
Expression exp = fa.getExpression();
if (fa.getName().toString().equals(findMe.toString())) {
constrains.add(ASTNode.FIELD_DECLARATION);
if (exp != null) {
constrains.add(ASTNode.TYPE_DECLARATION);
// + exp.getClass().getName() + " parent: " + exp.getParent());
if (exp instanceof MethodInvocation) {
SimpleType stp = extracTypeInfo(findDeclaration2(((MethodInvocation) exp).getName(), alternateParent));
if (stp == null)
return null;
declaringClass = findDeclaration2(stp.getName(), alternateParent);
return definedIn(declaringClass, fa.getName().toString(), constrains);
} else if (exp instanceof FieldAccess) {
SimpleType stp = extracTypeInfo(findDeclaration2(((FieldAccess) exp).getName(), alternateParent));
if (stp == null)
return null;
declaringClass = findDeclaration2((stp.getName()), alternateParent);
constrains.add(ASTNode.TYPE_DECLARATION);
return definedIn(declaringClass, fa.getName().toString(), constrains);
}
if (exp instanceof SimpleName) {
SimpleType stp = extracTypeInfo(findDeclaration2(((SimpleName) exp), alternateParent));
if (stp == null)
return null;
declaringClass = findDeclaration2(stp.getName(), alternateParent);
// log("FA.SN " + getNodeAsString(declaringClass));
constrains.add(ASTNode.METHOD_DECLARATION);
return definedIn(declaringClass, fa.getName().toString(), constrains);
}
}
} else {
// Move one up the ast. V V IMP!!
parent = parent.getParent();
alternateParent = alternateParent.getParent();
}
} else if (parent.getNodeType() == ASTNode.QUALIFIED_NAME) {
QualifiedName qn = (QualifiedName) parent;
if (!findMe.toString().equals(qn.getQualifier().toString())) {
SimpleType stp = extracTypeInfo(findDeclaration2((qn.getQualifier()), alternateParent));
if (stp == null)
return null;
declaringClass = findDeclaration2(stp.getName(), alternateParent);
// log(qn.getQualifier() + "->" + qn.getName());
// log("QN decl class: " + getNodeAsString(declaringClass));
constrains.clear();
constrains.add(ASTNode.TYPE_DECLARATION);
constrains.add(ASTNode.FIELD_DECLARATION);
return definedIn(declaringClass, qn.getName().toString(), constrains);
} else {
if (findMe instanceof QualifiedName) {
QualifiedName qnn = (QualifiedName) findMe;
// log("findMe is a QN, "
// + (qnn.getQualifier().toString() + " other " + qnn.getName()
// .toString()));
SimpleType stp = extracTypeInfo(findDeclaration2((qnn.getQualifier()), alternateParent));
if (stp == null) {
return null;
}
// log(qnn.getQualifier() + "->" + qnn.getName());
declaringClass = findDeclaration2(stp.getName(), alternateParent);
// log("QN decl class: "
// + getNodeAsString(declaringClass));
constrains.clear();
constrains.add(ASTNode.TYPE_DECLARATION);
constrains.add(ASTNode.FIELD_DECLARATION);
return definedIn(declaringClass, qnn.getName().toString(), constrains);
}
}
} else if (parent.getNodeType() == ASTNode.SIMPLE_TYPE) {
constrains.add(ASTNode.TYPE_DECLARATION);
if (parent.getParent().getNodeType() == ASTNode.CLASS_INSTANCE_CREATION)
constrains.add(ASTNode.CLASS_INSTANCE_CREATION);
} else if (parent instanceof Expression) {
// constrains.add(ASTNode.TYPE_DECLARATION);
// constrains.add(ASTNode.METHOD_DECLARATION);
// constrains.add(ASTNode.FIELD_DECLARATION);
}
// log("Alternate parent: " + getNodeAsString(alternateParent));
while (alternateParent != null) {
// + getNodeAsString(alternateParent));
for (Object oprop : alternateParent.structuralPropertiesForType()) {
StructuralPropertyDescriptor prop = (StructuralPropertyDescriptor) oprop;
if (prop.isChildProperty() || prop.isSimpleProperty()) {
if (alternateParent.getStructuralProperty(prop) instanceof ASTNode) {
// log(prop + " C/S Prop of -> "
// + getNodeAsString(alternateParent));
ret = definedIn((ASTNode) alternateParent.getStructuralProperty(prop), findMe.toString(), constrains);
if (ret != null)
return ret;
}
} else if (prop.isChildListProperty()) {
// log((prop) + " ChildList props of "
// + getNodeAsString(alternateParent));
List<ASTNode> nodelist = (List<ASTNode>) alternateParent.getStructuralProperty(prop);
for (ASTNode retNode : nodelist) {
ret = definedIn(retNode, findMe.toString(), constrains);
if (ret != null)
return ret;
}
}
}
alternateParent = alternateParent.getParent();
}
return null;
}
use of org.eclipse.jdt.core.dom.FieldAccess in project AutoRefactor by JnRouvignac.
the class BooleanConstantRatherThanValueOfRefactoring method toFieldAccess.
private FieldAccess toFieldAccess(final MethodInvocation node, final boolean booleanLiteral) {
final ASTBuilder b = ctx.getASTBuilder();
final FieldAccess fa = b.getAST().newFieldAccess();
if (node.getExpression() instanceof Name) {
fa.setExpression(b.copy(node.getExpression()));
}
fa.setName(b.simpleName(booleanLiteral ? "TRUE" : "FALSE"));
return fa;
}
use of org.eclipse.jdt.core.dom.FieldAccess in project che by eclipse.
the class DelegateFieldCreator method createDelegateFieldInitializer.
// ******************* INTERNAL HELPERS ***************************
private Expression createDelegateFieldInitializer(final FieldDeclaration declaration) throws JavaModelException {
Assert.isNotNull(declaration);
Expression qualification = getAccess();
if (qualification != null) {
FieldAccess access = getAst().newFieldAccess();
access.setExpression(qualification);
access.setName(getAst().newSimpleName(getNewElementName()));
return access;
} else {
SimpleName access = getAst().newSimpleName(getNewElementName());
return access;
}
}
use of org.eclipse.jdt.core.dom.FieldAccess in project che by eclipse.
the class StubUtility2 method createDelegationStub.
public static MethodDeclaration createDelegationStub(ICompilationUnit unit, ASTRewrite rewrite, ImportRewrite imports, ImportRewriteContext context, IMethodBinding delegate, IVariableBinding delegatingField, CodeGenerationSettings settings) throws CoreException {
Assert.isNotNull(delegate);
Assert.isNotNull(delegatingField);
Assert.isNotNull(settings);
AST ast = rewrite.getAST();
MethodDeclaration decl = ast.newMethodDeclaration();
decl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, delegate.getModifiers() & ~Modifier.SYNCHRONIZED & ~Modifier.ABSTRACT & ~Modifier.NATIVE));
decl.setName(ast.newSimpleName(delegate.getName()));
decl.setConstructor(false);
createTypeParameters(imports, context, ast, delegate, decl);
decl.setReturnType2(imports.addImport(delegate.getReturnType(), ast, context));
List<SingleVariableDeclaration> params = createParameters(unit.getJavaProject(), imports, context, ast, delegate, null, decl);
createThrownExceptions(decl, delegate, imports, context, ast);
Block body = ast.newBlock();
decl.setBody(body);
String delimiter = StubUtility.getLineDelimiterUsed(unit);
Statement statement = null;
MethodInvocation invocation = ast.newMethodInvocation();
invocation.setName(ast.newSimpleName(delegate.getName()));
List<Expression> arguments = invocation.arguments();
for (int i = 0; i < params.size(); i++) arguments.add(ast.newSimpleName(params.get(i).getName().getIdentifier()));
if (settings.useKeywordThis) {
FieldAccess access = ast.newFieldAccess();
access.setExpression(ast.newThisExpression());
access.setName(ast.newSimpleName(delegatingField.getName()));
invocation.setExpression(access);
} else
invocation.setExpression(ast.newSimpleName(delegatingField.getName()));
if (delegate.getReturnType().isPrimitive() && delegate.getReturnType().getName().equals("void")) {
//$NON-NLS-1$
statement = ast.newExpressionStatement(invocation);
} else {
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(invocation);
statement = returnStatement;
}
body.statements().add(statement);
ITypeBinding declaringType = delegatingField.getDeclaringClass();
if (declaringType == null) {
// can be null for
return decl;
}
String qualifiedName = declaringType.getQualifiedName();
IPackageBinding packageBinding = declaringType.getPackage();
if (packageBinding != null) {
if (packageBinding.getName().length() > 0 && qualifiedName.startsWith(packageBinding.getName()))
qualifiedName = qualifiedName.substring(packageBinding.getName().length());
}
if (settings.createComments) {
/*
* TODO: have API for delegate method comments This is an inlined
* version of
* {@link CodeGeneration#getMethodComment(ICompilationUnit, String, MethodDeclaration, IMethodBinding, String)}
*/
delegate = delegate.getMethodDeclaration();
String declaringClassQualifiedName = delegate.getDeclaringClass().getQualifiedName();
String linkToMethodName = delegate.getName();
String[] parameterTypesQualifiedNames = StubUtility.getParameterTypeNamesForSeeTag(delegate);
String string = StubUtility.getMethodComment(unit, qualifiedName, decl, delegate.isDeprecated(), linkToMethodName, declaringClassQualifiedName, parameterTypesQualifiedNames, true, delimiter);
if (string != null) {
Javadoc javadoc = (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
decl.setJavadoc(javadoc);
}
}
return decl;
}
use of org.eclipse.jdt.core.dom.FieldAccess 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;
}
Aggregations