use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project flux by eclipse.
the class Bindings method getBindingOfParentTypeContext.
/**
* Returns the type binding of the node's type context or null if the node is inside
* an annotation, type parameter, super type declaration, or Javadoc of a top level type.
* The result of this method is equal to the result of {@link #getBindingOfParentType(ASTNode)} for nodes in the type's body.
*
* @param node an AST node
* @return the type binding of the node's parent type context, or <code>null</code>
*/
public static ITypeBinding getBindingOfParentTypeContext(ASTNode node) {
StructuralPropertyDescriptor lastLocation = null;
while (node != null) {
if (node instanceof AbstractTypeDeclaration) {
AbstractTypeDeclaration decl = (AbstractTypeDeclaration) node;
if (lastLocation == decl.getBodyDeclarationsProperty() || lastLocation == decl.getJavadocProperty()) {
return decl.resolveBinding();
} else if (decl instanceof EnumDeclaration && lastLocation == EnumDeclaration.ENUM_CONSTANTS_PROPERTY) {
return decl.resolveBinding();
}
} else if (node instanceof AnonymousClassDeclaration) {
return ((AnonymousClassDeclaration) node).resolveBinding();
}
lastLocation = node.getLocationInParent();
node = node.getParent();
}
return null;
}
use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.
the class PromoteTempToFieldRefactoring method addInitializersToConstructors.
private void addInitializersToConstructors(ASTRewrite rewrite) throws CoreException {
Assert.isTrue(!isDeclaredInAnonymousClass());
final AbstractTypeDeclaration declaration = (AbstractTypeDeclaration) getMethodDeclaration().getParent();
final MethodDeclaration[] constructors = getAllConstructors(declaration);
if (constructors.length == 0) {
AST ast = rewrite.getAST();
MethodDeclaration newConstructor = ast.newMethodDeclaration();
newConstructor.setConstructor(true);
newConstructor.modifiers().addAll(ast.newModifiers(declaration.getModifiers() & ModifierRewrite.VISIBILITY_MODIFIERS));
newConstructor.setName(ast.newSimpleName(declaration.getName().getIdentifier()));
newConstructor.setJavadoc(getNewConstructorComment(rewrite));
newConstructor.setBody(ast.newBlock());
addFieldInitializationToConstructor(rewrite, newConstructor);
int insertionIndex = computeInsertIndexForNewConstructor(declaration);
rewrite.getListRewrite(declaration, declaration.getBodyDeclarationsProperty()).insertAt(newConstructor, insertionIndex, null);
} else {
for (int index = 0; index < constructors.length; index++) {
if (shouldInsertTempInitialization(constructors[index]))
addFieldInitializationToConstructor(rewrite, constructors[index]);
}
}
}
use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.
the class IntroduceIndirectionRefactoring method typeToBodyDeclarationProperty.
private ChildListPropertyDescriptor typeToBodyDeclarationProperty(IType type, CompilationUnit root) throws JavaModelException {
ASTNode typeDeclaration = typeToDeclaration(type, root);
if (typeDeclaration instanceof AbstractTypeDeclaration)
return ((AbstractTypeDeclaration) typeDeclaration).getBodyDeclarationsProperty();
else if (typeDeclaration instanceof AnonymousClassDeclaration)
return AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY;
Assert.isTrue(false);
return null;
}
use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.
the class PromoteTempToFieldRefactoring method getAllConstructors.
private static MethodDeclaration[] getAllConstructors(AbstractTypeDeclaration typeDeclaration) {
if (typeDeclaration instanceof TypeDeclaration) {
MethodDeclaration[] allMethods = ((TypeDeclaration) typeDeclaration).getMethods();
List<MethodDeclaration> result = new ArrayList<MethodDeclaration>(Math.min(allMethods.length, 1));
for (int i = 0; i < allMethods.length; i++) {
MethodDeclaration declaration = allMethods[i];
if (declaration.isConstructor())
result.add(declaration);
}
return result.toArray(new MethodDeclaration[result.size()]);
}
return new MethodDeclaration[] {};
}
use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.
the class PromoteTempToFieldRefactoring method checkClashesInConstructors.
private RefactoringStatus checkClashesInConstructors() {
Assert.isTrue(fInitializeIn == INITIALIZE_IN_CONSTRUCTOR);
Assert.isTrue(!isDeclaredInAnonymousClass());
final AbstractTypeDeclaration declaration = (AbstractTypeDeclaration) getMethodDeclaration().getParent();
if (declaration instanceof TypeDeclaration) {
MethodDeclaration[] methods = ((TypeDeclaration) declaration).getMethods();
for (int i = 0; i < methods.length; i++) {
MethodDeclaration method = methods[i];
if (!method.isConstructor())
continue;
NameCollector nameCollector = new NameCollector(method) {
@Override
protected boolean visitNode(ASTNode node) {
return true;
}
};
method.accept(nameCollector);
List<String> names = nameCollector.getNames();
if (names.contains(fFieldName)) {
String[] keys = { BasicElementLabels.getJavaElementName(fFieldName), BindingLabelProvider.getBindingLabel(method.resolveBinding(), JavaElementLabels.ALL_FULLY_QUALIFIED) };
String msg = Messages.format(RefactoringCoreMessages.PromoteTempToFieldRefactoring_Name_conflict, keys);
return RefactoringStatus.createFatalErrorStatus(msg);
}
}
}
return null;
}
Aggregations