use of org.eclipse.jdt.core.dom.Expression 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.Expression in project che by eclipse.
the class ExtractConstantRefactoring method checkExpression.
private RefactoringStatus checkExpression() throws JavaModelException {
RefactoringStatus result = new RefactoringStatus();
result.merge(checkExpressionBinding());
if (result.hasFatalError())
return result;
checkAllStaticFinal();
IExpressionFragment selectedExpression = getSelectedExpression();
Expression associatedExpression = selectedExpression.getAssociatedExpression();
if (associatedExpression instanceof NullLiteral)
result.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_null_literals));
else if (!ConstantChecks.isLoadTimeConstant(selectedExpression))
result.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_not_load_time_constant));
else if (associatedExpression instanceof SimpleName) {
if (associatedExpression.getParent() instanceof QualifiedName && associatedExpression.getLocationInParent() == QualifiedName.NAME_PROPERTY || associatedExpression.getParent() instanceof FieldAccess && associatedExpression.getLocationInParent() == FieldAccess.NAME_PROPERTY)
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_select_expression);
}
return result;
}
use of org.eclipse.jdt.core.dom.Expression in project che by eclipse.
the class FullConstraintCreator method create.
/* (non-Javadoc)
* @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.FieldAccess)
*/
@Override
public ITypeConstraint[] create(FieldAccess access) {
Expression expression = access.getExpression();
SimpleName name = access.getName();
IBinding binding = name.resolveBinding();
if (!(binding instanceof IVariableBinding))
return new ITypeConstraint[0];
IVariableBinding vb = (IVariableBinding) binding;
return createConstraintsForAccessToField(vb, expression, access);
}
use of org.eclipse.jdt.core.dom.Expression in project che by eclipse.
the class FullConstraintCreator method create.
/* (non-Javadoc)
* @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.InstanceofExpression)
*/
@Override
public ITypeConstraint[] create(InstanceofExpression instanceofExpression) {
Expression expression = instanceofExpression.getLeftOperand();
Type type = instanceofExpression.getRightOperand();
if (isClassBinding(expression.resolveTypeBinding()) && isClassBinding(type.resolveBinding())) {
ConstraintVariable expressionVar = fConstraintVariableFactory.makeExpressionOrTypeVariable(expression, getContext());
ConstraintVariable typeVariable = fConstraintVariableFactory.makeTypeVariable(type);
return createOrOrSubtypeConstraint(expressionVar, typeVariable);
} else
return new ITypeConstraint[0];
}
use of org.eclipse.jdt.core.dom.Expression in project che by eclipse.
the class FullConstraintCreator method create.
/* (non-Javadoc)
* @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.MethodInvocation)
*/
@Override
public ITypeConstraint[] create(MethodInvocation invocation) {
List<Expression> arguments = invocation.arguments();
List<ITypeConstraint> result = new ArrayList<ITypeConstraint>(arguments.size());
IMethodBinding methodBinding = invocation.resolveMethodBinding();
if (methodBinding == null)
return new ITypeConstraint[0];
ITypeConstraint[] returnTypeConstraint = getReturnTypeConstraint(invocation, methodBinding);
result.addAll(Arrays.asList(returnTypeConstraint));
result.addAll(Arrays.asList(getArgumentConstraints(arguments, methodBinding)));
if (invocation.getExpression() != null) {
if (MethodChecks.isVirtual(methodBinding)) {
IMethodBinding[] rootDefs = getRootDefs(methodBinding);
Assert.isTrue(rootDefs.length > 0);
ConstraintVariable expressionVar = fConstraintVariableFactory.makeExpressionOrTypeVariable(invocation.getExpression(), getContext());
if (rootDefs.length == 1) {
result.addAll(Arrays.asList(fTypeConstraintFactory.createSubtypeConstraint(expressionVar, fConstraintVariableFactory.makeDeclaringTypeVariable(rootDefs[0]))));
} else {
Collection<ITypeConstraint> constraints = new ArrayList<ITypeConstraint>();
for (int i = 0; i < rootDefs.length; i++) {
ConstraintVariable rootDefTypeVar = fConstraintVariableFactory.makeDeclaringTypeVariable(rootDefs[i]);
ITypeConstraint[] tc = fTypeConstraintFactory.createSubtypeConstraint(expressionVar, rootDefTypeVar);
constraints.addAll(Arrays.asList(tc));
}
ITypeConstraint[] constraintsArray = constraints.toArray(new ITypeConstraint[constraints.size()]);
if (constraintsArray.length > 0) {
result.add(fTypeConstraintFactory.createCompositeOrTypeConstraint(constraintsArray));
}
}
} else {
ConstraintVariable typeVar = fConstraintVariableFactory.makeDeclaringTypeVariable(methodBinding);
ConstraintVariable expressionVar = fConstraintVariableFactory.makeExpressionOrTypeVariable(invocation.getExpression(), getContext());
result.addAll(Arrays.asList(fTypeConstraintFactory.createSubtypeConstraint(expressionVar, typeVar)));
}
}
return result.toArray(new ITypeConstraint[result.size()]);
}
Aggregations