use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method addNestedClass.
private void addNestedClass(CompilationUnitRewrite rewrite, ITypeBinding[] typeParameters) throws CoreException {
final AbstractTypeDeclaration declarations = (AbstractTypeDeclaration) ASTNodes.getParent(fAnonymousInnerClassNode, AbstractTypeDeclaration.class);
int index = findIndexOfFistNestedClass(declarations.bodyDeclarations());
if (index == -1)
index = 0;
rewrite.getASTRewrite().getListRewrite(declarations, declarations.getBodyDeclarationsProperty()).insertAt(createNewNestedClass(rewrite, typeParameters), index, null);
}
use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration 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.AbstractTypeDeclaration in project che by eclipse.
the class ExtractConstantRefactoring method createConstantDeclaration.
private void createConstantDeclaration() throws CoreException {
Type type = getConstantType();
IExpressionFragment fragment = getSelectedExpression();
Expression initializer = getSelectedExpression().createCopyTarget(fCuRewrite.getASTRewrite(), true);
AST ast = fCuRewrite.getAST();
VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();
variableDeclarationFragment.setName(ast.newSimpleName(fConstantName));
variableDeclarationFragment.setInitializer(initializer);
FieldDeclaration fieldDeclaration = ast.newFieldDeclaration(variableDeclarationFragment);
fieldDeclaration.setType(type);
Modifier.ModifierKeyword accessModifier = Modifier.ModifierKeyword.toKeyword(fVisibility);
if (accessModifier != null)
fieldDeclaration.modifiers().add(ast.newModifier(accessModifier));
fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));
fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD));
boolean createComments = JavaPreferencesSettings.getCodeGenerationSettings(fCu.getJavaProject()).createComments;
if (createComments) {
String comment = CodeGeneration.getFieldComment(fCu, getConstantTypeName(), fConstantName, StubUtility.getLineDelimiterUsed(fCu));
if (comment != null && comment.length() > 0) {
Javadoc doc = (Javadoc) fCuRewrite.getASTRewrite().createStringPlaceholder(comment, ASTNode.JAVADOC);
fieldDeclaration.setJavadoc(doc);
}
}
AbstractTypeDeclaration parent = getContainingTypeDeclarationNode();
ListRewrite listRewrite = fCuRewrite.getASTRewrite().getListRewrite(parent, parent.getBodyDeclarationsProperty());
TextEditGroup msg = fCuRewrite.createGroupDescription(RefactoringCoreMessages.ExtractConstantRefactoring_declare_constant);
if (insertFirst()) {
listRewrite.insertFirst(fieldDeclaration, msg);
} else {
listRewrite.insertAfter(fieldDeclaration, getNodeToInsertConstantDeclarationAfter(), msg);
}
if (fLinkedProposalModel != null) {
ASTRewrite rewrite = fCuRewrite.getASTRewrite();
LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true);
nameGroup.addPosition(rewrite.track(variableDeclarationFragment.getName()), true);
String[] nameSuggestions = guessConstantNames();
if (nameSuggestions.length > 0 && !nameSuggestions[0].equals(fConstantName)) {
nameGroup.addProposal(fConstantName, null, nameSuggestions.length + 1);
}
for (int i = 0; i < nameSuggestions.length; i++) {
nameGroup.addProposal(nameSuggestions[i], null, nameSuggestions.length - i);
}
LinkedProposalPositionGroup typeGroup = fLinkedProposalModel.getPositionGroup(KEY_TYPE, true);
typeGroup.addPosition(rewrite.track(type), true);
ITypeBinding typeBinding = guessBindingForReference(fragment.getAssociatedExpression());
if (typeBinding != null) {
ITypeBinding[] relaxingTypes = ASTResolving.getNarrowingTypes(ast, typeBinding);
for (int i = 0; i < relaxingTypes.length; i++) {
typeGroup.addProposal(relaxingTypes[i], fCuRewrite.getCu(), relaxingTypes.length - i);
}
}
boolean isInterface = parent.resolveBinding() != null && parent.resolveBinding().isInterface();
ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(fLinkedProposalModel, rewrite, fieldDeclaration.modifiers(), isInterface);
}
}
use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project flux by eclipse.
the class ASTNodeFactory method newTypeParameter.
public static TypeParameter newTypeParameter(AST ast, String content) {
StringBuffer buffer = new StringBuffer(TYPEPARAM_HEADER);
buffer.append(content);
buffer.append(TYPEPARAM_FOOTER);
ASTParser p = ASTParser.newParser(ast.apiLevel());
p.setSource(buffer.toString().toCharArray());
CompilationUnit root = (CompilationUnit) p.createAST(null);
List<AbstractTypeDeclaration> list = root.types();
TypeDeclaration typeDecl = (TypeDeclaration) list.get(0);
MethodDeclaration methodDecl = typeDecl.getMethods()[0];
TypeParameter tp = (TypeParameter) methodDecl.typeParameters().get(0);
ASTNode result = ASTNode.copySubtree(ast, tp);
result.accept(new PositionClearer());
return (TypeParameter) result;
}
use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project flux by eclipse.
the class ASTNodes method getReceiverTypeBinding.
/**
* Returns the receiver's type binding of the given method invocation.
*
* @param invocation method invocation to resolve type of
* @return the type binding of the receiver
*/
public static ITypeBinding getReceiverTypeBinding(MethodInvocation invocation) {
ITypeBinding result = null;
Expression exp = invocation.getExpression();
if (exp != null) {
return exp.resolveTypeBinding();
} else {
AbstractTypeDeclaration type = (AbstractTypeDeclaration) getParent(invocation, AbstractTypeDeclaration.class);
if (type != null)
return type.resolveBinding();
}
return result;
}
Aggregations