use of org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext in project che by eclipse.
the class InlineTempRefactoring method getModifiedInitializerSource.
private Expression getModifiedInitializerSource(CompilationUnitRewrite rewrite, SimpleName reference) throws JavaModelException {
VariableDeclaration varDecl = getVariableDeclaration();
Expression initializer = varDecl.getInitializer();
ASTNode referenceContext = reference.getParent();
if (Invocations.isResolvedTypeInferredFromExpectedType(initializer)) {
if (!(referenceContext instanceof VariableDeclarationFragment || referenceContext instanceof SingleVariableDeclaration || referenceContext instanceof Assignment)) {
ITypeBinding[] typeArguments = Invocations.getInferredTypeArguments(initializer);
if (typeArguments != null) {
String newSource = createParameterizedInvocation(initializer, typeArguments, rewrite);
return (Expression) rewrite.getASTRewrite().createStringPlaceholder(newSource, initializer.getNodeType());
}
}
}
Expression copy = (Expression) rewrite.getASTRewrite().createCopyTarget(initializer);
AST ast = rewrite.getAST();
if (NecessaryParenthesesChecker.needsParentheses(initializer, reference.getParent(), reference.getLocationInParent())) {
ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
parenthesized.setExpression(copy);
copy = parenthesized;
}
ITypeBinding explicitCast = ASTNodes.getExplicitCast(initializer, reference);
if (explicitCast != null) {
CastExpression cast = ast.newCastExpression();
if (NecessaryParenthesesChecker.needsParentheses(copy, cast, CastExpression.EXPRESSION_PROPERTY)) {
ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
parenthesized.setExpression(copy);
copy = parenthesized;
}
cast.setExpression(copy);
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(reference, rewrite.getImportRewrite());
cast.setType(rewrite.getImportRewrite().addImport(explicitCast, ast, context));
copy = cast;
} else if (initializer instanceof ArrayInitializer && ASTNodes.getDimensions(varDecl) > 0) {
ArrayType newType = (ArrayType) ASTNodeFactory.newType(ast, varDecl);
ArrayCreation newArrayCreation = ast.newArrayCreation();
newArrayCreation.setType(newType);
newArrayCreation.setInitializer((ArrayInitializer) copy);
return newArrayCreation;
}
return copy;
}
use of org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext in project che by eclipse.
the class IntroduceIndirectionRefactoring method createIntermediaryMethod.
private void createIntermediaryMethod() throws CoreException {
CompilationUnitRewrite imRewrite = getCachedCURewrite(fIntermediaryType.getCompilationUnit());
AST ast = imRewrite.getAST();
MethodDeclaration intermediary = ast.newMethodDeclaration();
// Intermediary class is non-anonymous
AbstractTypeDeclaration type = (AbstractTypeDeclaration) typeToDeclaration(fIntermediaryType, imRewrite.getRoot());
// Name
intermediary.setName(ast.newSimpleName(fIntermediaryMethodName));
// Flags
List<IExtendedModifier> modifiers = intermediary.modifiers();
if (!fIntermediaryType.isInterface()) {
modifiers.add(imRewrite.getAST().newModifier(ModifierKeyword.PUBLIC_KEYWORD));
}
modifiers.add(imRewrite.getAST().newModifier(ModifierKeyword.STATIC_KEYWORD));
// Parameters
String targetParameterName = StubUtility.suggestArgumentName(getProject(), fIntermediaryFirstParameterType.getName(), fTargetMethod.getParameterNames());
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(type, imRewrite.getImportRewrite());
if (!isStaticTarget()) {
// Add first param
SingleVariableDeclaration parameter = imRewrite.getAST().newSingleVariableDeclaration();
Type t = imRewrite.getImportRewrite().addImport(fIntermediaryFirstParameterType, imRewrite.getAST(), context);
if (fIntermediaryFirstParameterType.isGenericType()) {
ParameterizedType parameterized = imRewrite.getAST().newParameterizedType(t);
ITypeBinding[] typeParameters = fIntermediaryFirstParameterType.getTypeParameters();
for (int i = 0; i < typeParameters.length; i++) parameterized.typeArguments().add(imRewrite.getImportRewrite().addImport(typeParameters[i], imRewrite.getAST()));
t = parameterized;
}
parameter.setType(t);
parameter.setName(imRewrite.getAST().newSimpleName(targetParameterName));
intermediary.parameters().add(parameter);
}
// Add other params
copyArguments(intermediary, imRewrite);
// Add type parameters of declaring type (and enclosing types)
if (!isStaticTarget() && fIntermediaryFirstParameterType.isGenericType())
addTypeParameters(imRewrite, intermediary.typeParameters(), fIntermediaryFirstParameterType);
// Add type params of method
copyTypeParameters(intermediary, imRewrite);
// Return type
intermediary.setReturnType2(imRewrite.getImportRewrite().addImport(fTargetMethodBinding.getReturnType(), ast, context));
// Exceptions
copyExceptions(intermediary, imRewrite);
// Body
MethodInvocation invocation = imRewrite.getAST().newMethodInvocation();
invocation.setName(imRewrite.getAST().newSimpleName(fTargetMethod.getElementName()));
if (isStaticTarget()) {
Type importedType = imRewrite.getImportRewrite().addImport(fTargetMethodBinding.getDeclaringClass(), ast, context);
invocation.setExpression(ASTNodeFactory.newName(ast, ASTNodes.asString(importedType)));
} else {
invocation.setExpression(imRewrite.getAST().newSimpleName(targetParameterName));
}
copyInvocationParameters(invocation, ast);
Statement call = encapsulateInvocation(intermediary, invocation);
final Block body = imRewrite.getAST().newBlock();
body.statements().add(call);
intermediary.setBody(body);
// method comment
ICompilationUnit targetCU = imRewrite.getCu();
if (StubUtility.doAddComments(targetCU.getJavaProject())) {
String comment = CodeGeneration.getMethodComment(targetCU, getIntermediaryTypeName(), intermediary, null, StubUtility.getLineDelimiterUsed(targetCU));
if (comment != null) {
Javadoc javadoc = (Javadoc) imRewrite.getASTRewrite().createStringPlaceholder(comment, ASTNode.JAVADOC);
intermediary.setJavadoc(javadoc);
}
}
// Add the completed method to the intermediary type:
ChildListPropertyDescriptor typeBodyDeclarationsProperty = typeToBodyDeclarationProperty(fIntermediaryType, imRewrite.getRoot());
ListRewrite bodyDeclarationsListRewrite = imRewrite.getASTRewrite().getListRewrite(type, typeBodyDeclarationsProperty);
bodyDeclarationsListRewrite.insertAt(intermediary, ASTNodes.getInsertionIndex(intermediary, type.bodyDeclarations()), imRewrite.createGroupDescription(RefactoringCoreMessages.IntroduceIndirectionRefactoring_group_description_create_new_method));
}
use of org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext in project che by eclipse.
the class ExtractTempRefactoring method createTempType.
private Type createTempType() throws CoreException {
Expression expression = getSelectedExpression().getAssociatedExpression();
Type resultingType = null;
ITypeBinding typeBinding = expression.resolveTypeBinding();
ASTRewrite rewrite = fCURewrite.getASTRewrite();
AST ast = rewrite.getAST();
if (expression instanceof ClassInstanceCreation && (typeBinding == null || typeBinding.getTypeArguments().length == 0)) {
resultingType = (Type) rewrite.createCopyTarget(((ClassInstanceCreation) expression).getType());
} else if (expression instanceof CastExpression) {
resultingType = (Type) rewrite.createCopyTarget(((CastExpression) expression).getType());
} else {
if (typeBinding == null) {
typeBinding = ASTResolving.guessBindingForReference(expression);
}
if (typeBinding != null) {
typeBinding = Bindings.normalizeForDeclarationUse(typeBinding, ast);
ImportRewrite importRewrite = fCURewrite.getImportRewrite();
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(expression, importRewrite);
resultingType = importRewrite.addImport(typeBinding, ast, context);
} else {
//$NON-NLS-1$
resultingType = ast.newSimpleType(ast.newSimpleName("Object"));
}
}
if (fLinkedProposalModel != null) {
LinkedProposalPositionGroup typeGroup = fLinkedProposalModel.getPositionGroup(KEY_TYPE, true);
typeGroup.addPosition(rewrite.track(resultingType), false);
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);
}
}
}
return resultingType;
}
use of org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext in project che by eclipse.
the class ExtractMethodAnalyzer method initReturnType.
private void initReturnType(ImportRewrite rewriter) {
AST ast = fEnclosingBodyDeclaration.getAST();
fReturnType = null;
fReturnTypeBinding = null;
switch(fReturnKind) {
case ACCESS_TO_LOCAL:
VariableDeclaration declaration = ASTNodes.findVariableDeclaration(fReturnValue, fEnclosingBodyDeclaration);
fReturnType = ASTNodeFactory.newType(ast, declaration, rewriter, new ContextSensitiveImportRewriteContext(declaration, rewriter));
if (declaration.resolveBinding() != null) {
fReturnTypeBinding = declaration.resolveBinding().getType();
}
break;
case EXPRESSION:
Expression expression = (Expression) getFirstSelectedNode();
if (expression.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) {
fExpressionBinding = ((ClassInstanceCreation) expression).getType().resolveBinding();
} else {
fExpressionBinding = expression.resolveTypeBinding();
}
if (fExpressionBinding != null) {
if (fExpressionBinding.isNullType()) {
getStatus().addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_null_type, JavaStatusContext.create(fCUnit, expression));
} else {
ITypeBinding normalizedBinding = Bindings.normalizeForDeclarationUse(fExpressionBinding, ast);
if (normalizedBinding != null) {
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(fEnclosingBodyDeclaration, rewriter);
fReturnType = rewriter.addImport(normalizedBinding, ast, context);
fReturnTypeBinding = normalizedBinding;
}
}
} else {
fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
//$NON-NLS-1$
fReturnTypeBinding = ast.resolveWellKnownType("void");
getStatus().addError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_determine_return_type, JavaStatusContext.create(fCUnit, expression));
}
break;
case RETURN_STATEMENT_VALUE:
LambdaExpression enclosingLambdaExpr = ASTResolving.findEnclosingLambdaExpression(getFirstSelectedNode());
if (enclosingLambdaExpr != null) {
fReturnType = ASTNodeFactory.newReturnType(enclosingLambdaExpr, ast, rewriter, null);
IMethodBinding methodBinding = enclosingLambdaExpr.resolveMethodBinding();
fReturnTypeBinding = methodBinding != null ? methodBinding.getReturnType() : null;
} else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
fReturnType = ((MethodDeclaration) fEnclosingBodyDeclaration).getReturnType2();
fReturnTypeBinding = fReturnType != null ? fReturnType.resolveBinding() : null;
}
break;
default:
fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
//$NON-NLS-1$
fReturnTypeBinding = ast.resolveWellKnownType("void");
}
if (fReturnType == null) {
fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
//$NON-NLS-1$
fReturnTypeBinding = ast.resolveWellKnownType("void");
}
}
use of org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext in project che by eclipse.
the class JavaContext method addStaticImport.
/**
* Adds a static import for the member with name <code>qualifiedMemberName</code>. The member is
* either a static field or a static method or a '*' to import all static members of a type.
*
* @param qualifiedMemberName the fully qualified name of the member to import or a qualified type
* name plus a '.*' suffix.
* @return returns either the simple member name if the import was successful or else the qualified name.
* @since 3.4
*/
public String addStaticImport(String qualifiedMemberName) {
if (isReadOnly())
return qualifiedMemberName;
ICompilationUnit cu = getCompilationUnit();
if (cu == null)
return qualifiedMemberName;
int memberOffset = qualifiedMemberName.lastIndexOf('.');
if (memberOffset == -1)
return qualifiedMemberName;
String typeName = qualifiedMemberName.substring(0, memberOffset);
String memberName = qualifiedMemberName.substring(memberOffset + 1, qualifiedMemberName.length());
try {
boolean isField;
if ("*".equals(memberName)) {
//$NON-NLS-1$
isField = true;
} else {
IJavaProject javaProject = cu.getJavaProject();
IType type = javaProject.findType(typeName);
if (type == null)
return qualifiedMemberName;
IField field = type.getField(memberName);
if (field.exists()) {
isField = true;
} else if (hasMethod(type, memberName)) {
isField = false;
} else {
return qualifiedMemberName;
}
}
CompilationUnit root = getASTRoot(cu);
if (fImportRewrite == null) {
if (root == null) {
fImportRewrite = StubUtility.createImportRewrite(cu, true);
} else {
fImportRewrite = StubUtility.createImportRewrite(root, true);
}
}
ImportRewriteContext context;
if (root == null)
context = null;
else
context = new ContextSensitiveImportRewriteContext(root, getCompletionOffset(), fImportRewrite);
return fImportRewrite.addStaticImport(typeName, memberName, isField, context);
} catch (JavaModelException e) {
handleException(null, e);
return typeName;
}
}
Aggregations