use of org.eclipse.jdt.core.dom.ClassInstanceCreation in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method setSuperType.
private void setSuperType(TypeDeclaration declaration) {
ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) fAnonymousInnerClassNode.getParent();
ITypeBinding binding = classInstanceCreation.resolveTypeBinding();
if (binding == null)
return;
Type newType = (Type) ASTNode.copySubtree(fAnonymousInnerClassNode.getAST(), classInstanceCreation.getType());
if (binding.getSuperclass().getQualifiedName().equals("java.lang.Object")) {
//$NON-NLS-1$
Assert.isTrue(binding.getInterfaces().length <= 1);
if (binding.getInterfaces().length == 0)
return;
declaration.superInterfaceTypes().add(0, newType);
} else {
declaration.setSuperclassType(newType);
}
}
use of org.eclipse.jdt.core.dom.ClassInstanceCreation in project che by eclipse.
the class AbstractSerialVersionOperation method rewriteAST.
/**
* {@inheritDoc}
*/
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel positionGroups) throws CoreException {
final ASTRewrite rewrite = cuRewrite.getASTRewrite();
VariableDeclarationFragment fragment = null;
for (int i = 0; i < fNodes.length; i++) {
final ASTNode node = fNodes[i];
final AST ast = node.getAST();
fragment = ast.newVariableDeclarationFragment();
fragment.setName(ast.newSimpleName(NAME_FIELD));
final FieldDeclaration declaration = ast.newFieldDeclaration(fragment);
declaration.setType(ast.newPrimitiveType(PrimitiveType.LONG));
declaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL));
if (!addInitializer(fragment, node))
continue;
if (fragment.getInitializer() != null) {
final TextEditGroup editGroup = createTextEditGroup(FixMessages.SerialVersion_group_description, cuRewrite);
if (node instanceof AbstractTypeDeclaration)
rewrite.getListRewrite(node, ((AbstractTypeDeclaration) node).getBodyDeclarationsProperty()).insertAt(declaration, 0, editGroup);
else if (node instanceof AnonymousClassDeclaration)
rewrite.getListRewrite(node, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY).insertAt(declaration, 0, editGroup);
else if (node instanceof ParameterizedType) {
final ParameterizedType type = (ParameterizedType) node;
final ASTNode parent = type.getParent();
if (parent instanceof ClassInstanceCreation) {
final ClassInstanceCreation creation = (ClassInstanceCreation) parent;
final AnonymousClassDeclaration anonymous = creation.getAnonymousClassDeclaration();
if (anonymous != null)
rewrite.getListRewrite(anonymous, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY).insertAt(declaration, 0, editGroup);
}
} else
Assert.isTrue(false);
addLinkedPositions(rewrite, fragment, positionGroups);
}
final String comment = CodeGeneration.getFieldComment(fUnit, declaration.getType().toString(), NAME_FIELD, StubUtility.getLineDelimiterUsed(fUnit));
if (comment != null && comment.length() > 0) {
final Javadoc doc = (Javadoc) rewrite.createStringPlaceholder(comment, ASTNode.JAVADOC);
declaration.setJavadoc(doc);
}
}
if (fragment == null)
return;
positionGroups.setEndPosition(rewrite.track(fragment));
}
use of org.eclipse.jdt.core.dom.ClassInstanceCreation in project che by eclipse.
the class Java50Fix method createRawTypeReferenceOperations.
private static SimpleType createRawTypeReferenceOperations(CompilationUnit compilationUnit, IProblemLocation[] locations, List<CompilationUnitRewriteOperation> operations) {
if (hasFatalError(compilationUnit))
return null;
List<SimpleType> result = new ArrayList<SimpleType>();
for (int i = 0; i < locations.length; i++) {
IProblemLocation problem = locations[i];
if (isRawTypeReferenceProblem(problem.getProblemId())) {
ASTNode node = problem.getCoveredNode(compilationUnit);
if (node instanceof ClassInstanceCreation) {
Type rawReference = (Type) node.getStructuralProperty(ClassInstanceCreation.TYPE_PROPERTY);
if (isRawTypeReference(rawReference)) {
result.add((SimpleType) rawReference);
}
} else if (node instanceof SimpleName) {
ASTNode rawReference = node.getParent();
if (isRawTypeReference(rawReference)) {
ASTNode parent = rawReference.getParent();
if (!(parent instanceof ArrayType || parent instanceof ParameterizedType))
result.add((SimpleType) rawReference);
}
} else if (node instanceof MethodInvocation) {
MethodInvocation invocation = (MethodInvocation) node;
SimpleType rawReference = getRawReference(invocation, compilationUnit);
if (rawReference != null) {
result.add(rawReference);
}
}
}
}
if (result.size() == 0)
return null;
SimpleType[] types = result.toArray(new SimpleType[result.size()]);
operations.add(new AddTypeParametersOperation(types));
return types[0];
}
use of org.eclipse.jdt.core.dom.ClassInstanceCreation in project che by eclipse.
the class IntroduceFactoryRefactoring method replaceConstructorCalls.
/**
* Use the given <code>ASTRewrite</code> to replace direct calls to the constructor
* with calls to the newly-created factory method.
* @param rg the <code>SearchResultGroup</code> indicating all of the constructor references
* @param unit the <code>CompilationUnit</code> to be rewritten
* @param unitRewriter the rewriter
* @param unitChange the compilation unit change
* @throws CoreException
* @return true iff at least one constructor call site was rewritten.
*/
private boolean replaceConstructorCalls(SearchResultGroup rg, CompilationUnit unit, ASTRewrite unitRewriter, CompilationUnitChange unitChange) throws CoreException {
Assert.isTrue(ASTCreator.getCu(unit).equals(rg.getCompilationUnit()));
SearchMatch[] hits = rg.getSearchResults();
Arrays.sort(hits, new Comparator<SearchMatch>() {
/**
* Sort by descending offset, such that nested constructor calls are processed first.
* This is necessary, since they can only be moved into the factory method invocation
* after they have been rewritten.
*/
public int compare(SearchMatch m1, SearchMatch m2) {
return m2.getOffset() - m1.getOffset();
}
});
boolean someCallPatched = false;
for (int i = 0; i < hits.length; i++) {
ASTNode ctrCall = getCtorCallAt(hits[i].getOffset(), hits[i].getLength(), unit);
if (ctrCall instanceof ClassInstanceCreation) {
TextEditGroup gd = new TextEditGroup(RefactoringCoreMessages.IntroduceFactory_replaceCalls);
rewriteFactoryMethodCall((ClassInstanceCreation) ctrCall, unitRewriter, gd);
unitChange.addTextEditGroup(gd);
someCallPatched = true;
} else if (ctrCall instanceof MethodRef) {
TextEditGroup gd = new TextEditGroup(RefactoringCoreMessages.IntroduceFactoryRefactoring_replaceJavadocReference);
rewriteJavadocReference((MethodRef) ctrCall, unitRewriter, gd);
unitChange.addTextEditGroup(gd);
someCallPatched = true;
}
}
return someCallPatched;
}
use of org.eclipse.jdt.core.dom.ClassInstanceCreation in project che by eclipse.
the class IntroduceFactoryRefactoring method createFactoryMethod.
/**
* Creates and returns a new MethodDeclaration that represents the factory method to be used in
* place of direct calls to the constructor in question.
*
* @param ast An AST used as a factory for various AST nodes
* @param ctorBinding binding for the constructor being wrapped
* @param unitRewriter the ASTRewrite to be used
* @return the new method declaration
* @throws CoreException if an exception occurs while accessing its corresponding resource
*/
private MethodDeclaration createFactoryMethod(AST ast, IMethodBinding ctorBinding, ASTRewrite unitRewriter) throws CoreException {
MethodDeclaration newMethod = ast.newMethodDeclaration();
SimpleName newMethodName = ast.newSimpleName(fNewMethodName);
ClassInstanceCreation newCtorCall = ast.newClassInstanceCreation();
ReturnStatement ret = ast.newReturnStatement();
Block body = ast.newBlock();
List<Statement> stmts = body.statements();
String retTypeName = ctorBinding.getName();
createFactoryMethodSignature(ast, newMethod);
newMethod.setName(newMethodName);
newMethod.setBody(body);
ITypeBinding declaringClass = fCtorBinding.getDeclaringClass();
ITypeBinding[] ctorOwnerTypeParameters = declaringClass.getTypeParameters();
setMethodReturnType(newMethod, retTypeName, ctorOwnerTypeParameters, ast);
newMethod.modifiers().addAll(ASTNodeFactory.newModifiers(ast, Modifier.STATIC | Modifier.PUBLIC));
setCtorTypeArguments(newCtorCall, retTypeName, ctorOwnerTypeParameters, ast);
createFactoryMethodConstructorArgs(ast, newCtorCall);
if (Modifier.isAbstract(declaringClass.getModifiers())) {
AnonymousClassDeclaration decl = ast.newAnonymousClassDeclaration();
IMethodBinding[] unimplementedMethods = getUnimplementedMethods(declaringClass);
CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(fCUHandle.getJavaProject());
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(fFactoryCU, decl.getStartPosition(), fImportRewriter);
for (int i = 0; i < unimplementedMethods.length; i++) {
IMethodBinding unImplementedMethod = unimplementedMethods[i];
MethodDeclaration newMethodDecl = StubUtility2.createImplementationStub(fCUHandle, unitRewriter, fImportRewriter, context, unImplementedMethod, unImplementedMethod.getDeclaringClass().getName(), settings, false);
decl.bodyDeclarations().add(newMethodDecl);
}
newCtorCall.setAnonymousClassDeclaration(decl);
}
ret.setExpression(newCtorCall);
stmts.add(ret);
return newMethod;
}
Aggregations