use of org.eclipse.jdt.core.dom.VariableDeclaration in project che by eclipse.
the class ExtractMethodRefactoring method createNewMethodDeclaration.
private MethodDeclaration createNewMethodDeclaration() {
MethodDeclaration result = fAST.newMethodDeclaration();
int modifiers = fVisibility;
BodyDeclaration enclosingBodyDeclaration = fAnalyzer.getEnclosingBodyDeclaration();
boolean isDestinationInterface = isDestinationInterface();
if (isDestinationInterface && !(enclosingBodyDeclaration instanceof MethodDeclaration && enclosingBodyDeclaration.getParent() == fDestination && Modifier.isPublic(enclosingBodyDeclaration.getModifiers()))) {
modifiers = Modifier.NONE;
}
boolean shouldBeStatic = false;
ASTNode currentParent = enclosingBodyDeclaration;
do {
if (currentParent instanceof BodyDeclaration) {
shouldBeStatic = shouldBeStatic || JdtFlags.isStatic((BodyDeclaration) currentParent);
}
currentParent = currentParent.getParent();
} while (!shouldBeStatic && currentParent != null && currentParent != fDestination);
if (shouldBeStatic || fAnalyzer.getForceStatic() || forceStatic()) {
modifiers |= Modifier.STATIC;
} else if (isDestinationInterface) {
modifiers |= Modifier.DEFAULT;
}
ITypeBinding[] typeVariables = computeLocalTypeVariables(modifiers);
List<TypeParameter> typeParameters = result.typeParameters();
for (int i = 0; i < typeVariables.length; i++) {
TypeParameter parameter = fAST.newTypeParameter();
parameter.setName(fAST.newSimpleName(typeVariables[i].getName()));
ITypeBinding[] bounds = typeVariables[i].getTypeBounds();
for (int j = 0; j < bounds.length; j++) if (//$NON-NLS-1$
!"java.lang.Object".equals(bounds[j].getQualifiedName()))
parameter.typeBounds().add(fImportRewriter.addImport(bounds[j], fAST));
typeParameters.add(parameter);
}
result.modifiers().addAll(ASTNodeFactory.newModifiers(fAST, modifiers));
result.setReturnType2((Type) ASTNode.copySubtree(fAST, fAnalyzer.getReturnType()));
result.setName(fAST.newSimpleName(fMethodName));
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(enclosingBodyDeclaration, fImportRewriter);
List<SingleVariableDeclaration> parameters = result.parameters();
for (int i = 0; i < fParameterInfos.size(); i++) {
ParameterInfo info = fParameterInfos.get(i);
VariableDeclaration infoDecl = getVariableDeclaration(info);
SingleVariableDeclaration parameter = fAST.newSingleVariableDeclaration();
parameter.modifiers().addAll(ASTNodeFactory.newModifiers(fAST, ASTNodes.getModifiers(infoDecl)));
parameter.setType(ASTNodeFactory.newType(fAST, infoDecl, fImportRewriter, context));
parameter.setName(fAST.newSimpleName(info.getNewName()));
parameter.setVarargs(info.isNewVarargs());
parameters.add(parameter);
}
List<Type> exceptions = result.thrownExceptionTypes();
ITypeBinding[] exceptionTypes = fAnalyzer.getExceptions(fThrowRuntimeExceptions);
for (int i = 0; i < exceptionTypes.length; i++) {
ITypeBinding exceptionType = exceptionTypes[i];
exceptions.add(fImportRewriter.addImport(exceptionType, fAST, context));
}
return result;
}
use of org.eclipse.jdt.core.dom.VariableDeclaration in project che by eclipse.
the class ExtractMethodRefactoring method initializeParameterInfos.
//---- Helper methods ------------------------------------------------------------------------
private void initializeParameterInfos() {
IVariableBinding[] arguments = fAnalyzer.getArguments();
fParameterInfos = new ArrayList<ParameterInfo>(arguments.length);
ASTNode root = fAnalyzer.getEnclosingBodyDeclaration();
ParameterInfo vararg = null;
for (int i = 0; i < arguments.length; i++) {
IVariableBinding argument = arguments[i];
if (argument == null)
continue;
VariableDeclaration declaration = ASTNodes.findVariableDeclaration(argument, root);
boolean isVarargs = declaration instanceof SingleVariableDeclaration ? ((SingleVariableDeclaration) declaration).isVarargs() : false;
ParameterInfo info = new ParameterInfo(argument, getType(declaration, isVarargs), argument.getName(), i);
if (isVarargs) {
vararg = info;
} else {
fParameterInfos.add(info);
}
}
if (vararg != null) {
fParameterInfos.add(vararg);
}
}
use of org.eclipse.jdt.core.dom.VariableDeclaration in project che by eclipse.
the class TempDeclarationFinder method findTempDeclaration.
/**
* @return <code>null</code> if the selection is invalid or does not cover a temp
* declaration or reference.
*/
public static VariableDeclaration findTempDeclaration(CompilationUnit cu, int selectionOffset, int selectionLength) {
TempSelectionAnalyzer analyzer = new TempSelectionAnalyzer(selectionOffset, selectionLength);
cu.accept(analyzer);
ASTNode[] selected = analyzer.getSelectedNodes();
if (selected == null || selected.length != 1)
return null;
ASTNode selectedNode = selected[0];
if (selectedNode instanceof VariableDeclaration)
return (VariableDeclaration) selectedNode;
if (selectedNode instanceof Name) {
Name reference = (Name) selectedNode;
IBinding binding = reference.resolveBinding();
if (binding == null)
return null;
ASTNode declaringNode = cu.findDeclaringNode(binding);
if (declaringNode instanceof VariableDeclaration)
return (VariableDeclaration) declaringNode;
else
return null;
} else if (selectedNode instanceof VariableDeclarationStatement) {
VariableDeclarationStatement vds = (VariableDeclarationStatement) selectedNode;
if (vds.fragments().size() != 1)
return null;
return (VariableDeclaration) vds.fragments().get(0);
}
return null;
}
use of org.eclipse.jdt.core.dom.VariableDeclaration in project che by eclipse.
the class JavadocTagsSubProcessor method getMissingJavadocCommentProposals.
public static void getMissingJavadocCommentProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
ASTNode node = problem.getCoveringNode(context.getASTRoot());
if (node == null) {
return;
}
BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(node);
if (declaration == null) {
return;
}
ICompilationUnit cu = context.getCompilationUnit();
ITypeBinding binding = Bindings.getBindingOfParentType(declaration);
if (binding == null) {
return;
}
if (declaration instanceof MethodDeclaration) {
MethodDeclaration methodDecl = (MethodDeclaration) declaration;
IMethodBinding methodBinding = methodDecl.resolveBinding();
IMethodBinding overridden = null;
if (methodBinding != null) {
overridden = Bindings.findOverriddenMethod(methodBinding, true);
}
String string = CodeGeneration.getMethodComment(cu, binding.getName(), methodDecl, overridden, String.valueOf('\n'));
if (string != null) {
String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_method_description;
proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_METHOD, declaration.getStartPosition(), string));
}
} else if (declaration instanceof AbstractTypeDeclaration) {
String typeQualifiedName = Bindings.getTypeQualifiedName(binding);
String[] typeParamNames;
if (declaration instanceof TypeDeclaration) {
List<TypeParameter> typeParams = ((TypeDeclaration) declaration).typeParameters();
typeParamNames = new String[typeParams.size()];
for (int i = 0; i < typeParamNames.length; i++) {
typeParamNames[i] = (typeParams.get(i)).getName().getIdentifier();
}
} else {
typeParamNames = new String[0];
}
String string = CodeGeneration.getTypeComment(cu, typeQualifiedName, typeParamNames, String.valueOf('\n'));
if (string != null) {
String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_type_description;
proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_TYPE, declaration.getStartPosition(), string));
}
} else if (declaration instanceof FieldDeclaration) {
//$NON-NLS-1$
String comment = "/**\n *\n */\n";
List<VariableDeclarationFragment> fragments = ((FieldDeclaration) declaration).fragments();
if (fragments != null && fragments.size() > 0) {
VariableDeclaration decl = fragments.get(0);
String fieldName = decl.getName().getIdentifier();
String typeName = binding.getName();
comment = CodeGeneration.getFieldComment(cu, typeName, fieldName, String.valueOf('\n'));
}
if (comment != null) {
String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_field_description;
proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_FIELD, declaration.getStartPosition(), comment));
}
} else if (declaration instanceof EnumConstantDeclaration) {
EnumConstantDeclaration enumDecl = (EnumConstantDeclaration) declaration;
String id = enumDecl.getName().getIdentifier();
String comment = CodeGeneration.getFieldComment(cu, binding.getName(), id, String.valueOf('\n'));
String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_enumconst_description;
proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_ENUM, declaration.getStartPosition(), comment));
}
}
use of org.eclipse.jdt.core.dom.VariableDeclaration in project che by eclipse.
the class RenameLocalVariableProcessor method initAST.
private void initAST() {
if (!fIsComposite)
fCompilationUnitNode = RefactoringASTParser.parseWithASTProvider(fCu, true, null);
ISourceRange sourceRange = fLocalVariable.getNameRange();
ASTNode name = NodeFinder.perform(fCompilationUnitNode, sourceRange);
if (name == null)
return;
if (name.getParent() instanceof VariableDeclaration)
fTempDeclarationNode = (VariableDeclaration) name.getParent();
}
Aggregations