use of org.eclipse.jdt.core.dom.TypeDeclaration in project flux by eclipse.
the class ASTNodeFactory method newType.
public static Type newType(AST ast, String content) {
StringBuffer buffer = new StringBuffer(TYPE_HEADER);
buffer.append(content);
buffer.append(TYPE_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];
ASTNode type = methodDecl.getReturnType2();
ASTNode result = ASTNode.copySubtree(ast, type);
result.accept(new PositionClearer());
return (Type) result;
}
use of org.eclipse.jdt.core.dom.TypeDeclaration in project che by eclipse.
the class PromoteTempToFieldRefactoring method checkInitialConditions.
/*
* @see org.eclipse.jdt.internal.corext.refactoring.base.Refactoring#checkActivation(org.eclipse.core.runtime.IProgressMonitor)
*/
@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
RefactoringStatus result = Checks.validateModifiesFiles(ResourceUtil.getFiles(new ICompilationUnit[] { fCu }), getValidationContext());
if (result.hasFatalError())
return result;
initAST(pm);
if (fTempDeclarationNode == null)
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_select_declaration);
if (!Checks.isDeclaredIn(fTempDeclarationNode, MethodDeclaration.class))
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_only_declared_in_methods);
if (isMethodParameter())
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_method_parameters);
if (isTempAnExceptionInCatchBlock())
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_exceptions);
ASTNode declaringType = ASTResolving.findParentType(fTempDeclarationNode);
if (declaringType instanceof TypeDeclaration && ((TypeDeclaration) declaringType).isInterface())
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_interface_methods);
result.merge(checkTempTypeForLocalTypeUsage());
if (result.hasFatalError())
return result;
checkTempInitializerForLocalTypeUsage();
if (!fSelfInitializing)
initializeDefaults();
return result;
}
use of org.eclipse.jdt.core.dom.TypeDeclaration in project che by eclipse.
the class PromoteTempToFieldRefactoring method getAllConstructors.
private static MethodDeclaration[] getAllConstructors(AbstractTypeDeclaration typeDeclaration) {
if (typeDeclaration instanceof TypeDeclaration) {
MethodDeclaration[] allMethods = ((TypeDeclaration) typeDeclaration).getMethods();
List<MethodDeclaration> result = new ArrayList<MethodDeclaration>(Math.min(allMethods.length, 1));
for (int i = 0; i < allMethods.length; i++) {
MethodDeclaration declaration = allMethods[i];
if (declaration.isConstructor())
result.add(declaration);
}
return result.toArray(new MethodDeclaration[result.size()]);
}
return new MethodDeclaration[] {};
}
use of org.eclipse.jdt.core.dom.TypeDeclaration in project che by eclipse.
the class PromoteTempToFieldRefactoring method checkClashesInConstructors.
private RefactoringStatus checkClashesInConstructors() {
Assert.isTrue(fInitializeIn == INITIALIZE_IN_CONSTRUCTOR);
Assert.isTrue(!isDeclaredInAnonymousClass());
final AbstractTypeDeclaration declaration = (AbstractTypeDeclaration) getMethodDeclaration().getParent();
if (declaration instanceof TypeDeclaration) {
MethodDeclaration[] methods = ((TypeDeclaration) declaration).getMethods();
for (int i = 0; i < methods.length; i++) {
MethodDeclaration method = methods[i];
if (!method.isConstructor())
continue;
NameCollector nameCollector = new NameCollector(method) {
@Override
protected boolean visitNode(ASTNode node) {
return true;
}
};
method.accept(nameCollector);
List<String> names = nameCollector.getNames();
if (names.contains(fFieldName)) {
String[] keys = { BasicElementLabels.getJavaElementName(fFieldName), BindingLabelProvider.getBindingLabel(method.resolveBinding(), JavaElementLabels.ALL_FULLY_QUALIFIED) };
String msg = Messages.format(RefactoringCoreMessages.PromoteTempToFieldRefactoring_Name_conflict, keys);
return RefactoringStatus.createFatalErrorStatus(msg);
}
}
}
return null;
}
use of org.eclipse.jdt.core.dom.TypeDeclaration in project che by eclipse.
the class UnresolvedElementsSubProcessor method addNewTypeProposals.
public static void addNewTypeProposals(ICompilationUnit cu, Name refNode, int kind, int relevance, Collection<ICommandAccess> proposals) throws CoreException {
Name node = refNode;
do {
String typeName = ASTNodes.getSimpleNameIdentifier(node);
Name qualifier = null;
// only propose to create types for qualifiers when the name starts with upper case
boolean isPossibleName = isLikelyTypeName(typeName) || node == refNode;
if (isPossibleName) {
IPackageFragment enclosingPackage = null;
IType enclosingType = null;
if (node.isSimpleName()) {
enclosingPackage = (IPackageFragment) cu.getParent();
// don't suggest member type, user can select it in wizard
} else {
Name qualifierName = ((QualifiedName) node).getQualifier();
IBinding binding = qualifierName.resolveBinding();
if (binding != null && binding.isRecovered()) {
binding = null;
}
if (binding instanceof ITypeBinding) {
enclosingType = (IType) binding.getJavaElement();
} else if (binding instanceof IPackageBinding) {
qualifier = qualifierName;
enclosingPackage = (IPackageFragment) binding.getJavaElement();
} else {
IJavaElement[] res = cu.codeSelect(qualifierName.getStartPosition(), qualifierName.getLength());
if (res != null && res.length > 0 && res[0] instanceof IType) {
enclosingType = (IType) res[0];
} else {
qualifier = qualifierName;
enclosingPackage = JavaModelUtil.getPackageFragmentRoot(cu).getPackageFragment(ASTResolving.getFullName(qualifierName));
}
}
}
int rel = relevance;
if (enclosingPackage != null && isLikelyPackageName(enclosingPackage.getElementName())) {
rel += 3;
}
if (enclosingPackage != null && !enclosingPackage.getCompilationUnit(typeName + JavaModelUtil.DEFAULT_CU_SUFFIX).exists() || enclosingType != null && !enclosingType.isReadOnly() && !enclosingType.getType(typeName).exists()) {
// new member type
IJavaElement enclosing = enclosingPackage != null ? (IJavaElement) enclosingPackage : enclosingType;
//TODO NewCUUsingWizardProposal
if ((kind & SimilarElementsRequestor.CLASSES) != 0) {
// proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_CLASS, enclosing, rel+3));
}
if ((kind & SimilarElementsRequestor.INTERFACES) != 0) {
// proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_INTERFACE, enclosing, rel+2));
}
if ((kind & SimilarElementsRequestor.ENUMS) != 0) {
// proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_ENUM, enclosing, rel));
}
if ((kind & SimilarElementsRequestor.ANNOTATIONS) != 0) {
// proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_ANNOTATION, enclosing, rel + 1));
addNullityAnnotationTypesProposals(cu, node, proposals);
}
}
}
node = qualifier;
} while (node != null);
// type parameter proposals
if (refNode.isSimpleName() && (kind & SimilarElementsRequestor.VARIABLES) != 0) {
CompilationUnit root = (CompilationUnit) refNode.getRoot();
String name = ((SimpleName) refNode).getIdentifier();
BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(refNode);
int baseRel = relevance;
if (isLikelyTypeParameterName(name)) {
baseRel += 8;
}
while (declaration != null) {
IBinding binding = null;
int rel = baseRel;
if (declaration instanceof MethodDeclaration) {
binding = ((MethodDeclaration) declaration).resolveBinding();
if (isLikelyMethodTypeParameterName(name))
rel += 2;
} else if (declaration instanceof TypeDeclaration) {
binding = ((TypeDeclaration) declaration).resolveBinding();
rel++;
}
if (binding != null) {
AddTypeParameterProposal proposal = new AddTypeParameterProposal(cu, binding, root, name, null, rel);
proposals.add(proposal);
}
if (!Modifier.isStatic(declaration.getModifiers())) {
declaration = ASTResolving.findParentBodyDeclaration(declaration.getParent());
} else {
declaration = null;
}
}
}
}
Aggregations