use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method accessesAnonymousFields.
private boolean accessesAnonymousFields() {
List<IVariableBinding> anonymousInnerFieldTypes = getAllEnclosingAnonymousTypesField();
List<IBinding> accessedField = getAllAccessedFields();
final Iterator<IVariableBinding> it = anonymousInnerFieldTypes.iterator();
while (it.hasNext()) {
final IVariableBinding variableBinding = it.next();
final Iterator<IBinding> it2 = accessedField.iterator();
while (it2.hasNext()) {
IVariableBinding variableBinding2 = (IVariableBinding) it2.next();
if (Bindings.equals(variableBinding, variableBinding2)) {
return true;
}
}
}
return false;
}
use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method createNewNestedClass.
private AbstractTypeDeclaration createNewNestedClass(CompilationUnitRewrite rewrite, ITypeBinding[] typeParameters) throws CoreException {
final AST ast = fAnonymousInnerClassNode.getAST();
final TypeDeclaration newDeclaration = ast.newTypeDeclaration();
newDeclaration.setInterface(false);
newDeclaration.setJavadoc(null);
newDeclaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiersForNestedClass()));
newDeclaration.setName(ast.newSimpleName(fClassName));
TypeParameter parameter = null;
for (int index = 0; index < typeParameters.length; index++) {
parameter = ast.newTypeParameter();
parameter.setName(ast.newSimpleName(typeParameters[index].getName()));
newDeclaration.typeParameters().add(parameter);
}
setSuperType(newDeclaration);
IJavaProject project = fCu.getJavaProject();
IVariableBinding[] bindings = getUsedLocalVariables();
ArrayList<String> fieldNames = new ArrayList<String>();
for (int i = 0; i < bindings.length; i++) {
String name = StubUtility.getBaseName(bindings[i], project);
String[] fieldNameProposals = StubUtility.getVariableNameSuggestions(NamingConventions.VK_INSTANCE_FIELD, project, name, 0, fieldNames, true);
fieldNames.add(fieldNameProposals[0]);
if (fLinkedProposalModel != null) {
LinkedProposalPositionGroup positionGroup = fLinkedProposalModel.getPositionGroup(KEY_FIELD_NAME_EXT + i, true);
for (int k = 0; k < fieldNameProposals.length; k++) {
positionGroup.addProposal(fieldNameProposals[k], null, fieldNameProposals.length - k);
}
}
}
String[] allFieldNames = fieldNames.toArray(new String[fieldNames.size()]);
List<BodyDeclaration> newBodyDeclarations = newDeclaration.bodyDeclarations();
createFieldsForAccessedLocals(rewrite, bindings, allFieldNames, newBodyDeclarations);
MethodDeclaration newConstructorDecl = createNewConstructor(rewrite, bindings, allFieldNames);
if (newConstructorDecl != null) {
newBodyDeclarations.add(newConstructorDecl);
}
updateAndMoveBodyDeclarations(rewrite, bindings, allFieldNames, newBodyDeclarations, newConstructorDecl);
if (doAddComments()) {
String[] parameterNames = new String[typeParameters.length];
for (int index = 0; index < parameterNames.length; index++) {
parameterNames[index] = typeParameters[index].getName();
}
String string = CodeGeneration.getTypeComment(rewrite.getCu(), fClassName, parameterNames, StubUtility.getLineDelimiterUsed(fCu));
if (string != null) {
Javadoc javadoc = (Javadoc) rewrite.getASTRewrite().createStringPlaceholder(string, ASTNode.JAVADOC);
newDeclaration.setJavadoc(javadoc);
}
}
if (fLinkedProposalModel != null) {
addLinkedPosition(KEY_TYPE_NAME, newDeclaration.getName(), rewrite.getASTRewrite(), false);
ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(fLinkedProposalModel, rewrite.getASTRewrite(), newDeclaration.modifiers(), false);
}
return newDeclaration;
}
use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.
the class CallInliner method crossCheckArguments.
/**
* Checks whether arguments are passed to the method which do some assignments
* inside the expression. If so these arguments can't be inlined into the
* calling method since the assignments might be reorder. An example is:
* <code>
* add((field=args).length,field.hashCode());
* </code>
* Field might not be initialized when the arguments are reorder in the called
* method.
* @param arguments the arguments
* @return all arguments that cannot be inlined
*/
private Set<Expression> crossCheckArguments(List<Expression> arguments) {
final Set<IBinding> assigned = new HashSet<IBinding>();
final Set<Expression> result = new HashSet<Expression>();
for (Iterator<Expression> iter = arguments.iterator(); iter.hasNext(); ) {
final Expression expression = iter.next();
expression.accept(new ASTVisitor() {
@Override
public boolean visit(Assignment node) {
Expression lhs = node.getLeftHandSide();
if (lhs instanceof Name) {
IBinding binding = ((Name) lhs).resolveBinding();
if (binding instanceof IVariableBinding) {
assigned.add(binding);
result.add(expression);
}
}
return true;
}
});
}
for (Iterator<Expression> iter = arguments.iterator(); iter.hasNext(); ) {
final Expression expression = iter.next();
if (!result.contains(expression)) {
expression.accept(new HierarchicalASTVisitor() {
@Override
public boolean visit(Name node) {
IBinding binding = node.resolveBinding();
if (binding != null && assigned.contains(binding))
result.add(expression);
return false;
}
});
}
}
return result;
}
use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method getAllAccessedFields.
private List<IBinding> getAllAccessedFields() {
final List<IBinding> accessedFields = new ArrayList<IBinding>();
ASTVisitor visitor = new ASTVisitor() {
@Override
public boolean visit(FieldAccess node) {
final IVariableBinding binding = node.resolveFieldBinding();
if (binding != null && !binding.isEnumConstant())
accessedFields.add(binding);
return super.visit(node);
}
@Override
public boolean visit(QualifiedName node) {
final IBinding binding = node.resolveBinding();
if (binding != null && binding instanceof IVariableBinding) {
IVariableBinding variable = (IVariableBinding) binding;
if (!variable.isEnumConstant() && variable.isField())
accessedFields.add(binding);
}
return super.visit(node);
}
@Override
public boolean visit(SimpleName node) {
final IBinding binding = node.resolveBinding();
if (binding != null && binding instanceof IVariableBinding) {
IVariableBinding variable = (IVariableBinding) binding;
if (!variable.isEnumConstant() && variable.isField())
accessedFields.add(binding);
}
return super.visit(node);
}
@Override
public boolean visit(SuperFieldAccess node) {
final IVariableBinding binding = node.resolveFieldBinding();
if (binding != null && !binding.isEnumConstant())
accessedFields.add(binding);
return super.visit(node);
}
};
fAnonymousInnerClassNode.accept(visitor);
return accessedFields;
}
use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method areLocalsUsedIn.
private boolean areLocalsUsedIn(Expression fieldInitializer, List<VariableDeclarationFragment> fieldsToInitialize) {
Set<IBinding> localsUsed = new HashSet<IBinding>(0);
collectRefrencedVariables(fieldInitializer, localsUsed);
ITypeBinding anonType = fAnonymousInnerClassNode.resolveBinding();
for (Iterator<IBinding> iterator = localsUsed.iterator(); iterator.hasNext(); ) {
IVariableBinding curr = (IVariableBinding) iterator.next();
if (isBindingToTemp(curr)) {
// reference a local from outside
return true;
} else if (curr.isField() && (curr.getDeclaringClass() == anonType) && fieldsToInitialize.contains(fCompilationUnitNode.findDeclaringNode(curr))) {
// references a field that references a local from outside
return true;
}
}
return false;
}
Aggregations