use of org.eclipse.jdt.core.dom.ASTParser in project bndtools by bndtools.
the class NewTypeWizardPage method typeNameChanged.
/**
* Hook method that gets called when the type name has changed. The method validates the type name and returns the
* status of the validation.
* <p>
* Subclasses may extend this method to perform their own validation.
* </p>
*
* @return the status of the validation
*/
protected IStatus typeNameChanged() {
StatusInfo status = new StatusInfo();
fCurrType = null;
String typeNameWithParameters = getTypeName();
// must not be empty
if (typeNameWithParameters.length() == 0) {
status.setError(NewWizardMessages.NewTypeWizardPage_error_EnterTypeName);
return status;
}
String typeName = getTypeNameWithoutParameters();
if (typeName.indexOf('.') != -1) {
status.setError(NewWizardMessages.NewTypeWizardPage_error_QualifiedName);
return status;
}
IJavaProject project = getJavaProject();
IStatus val = validateJavaTypeName(typeName, project);
if (val.getSeverity() == IStatus.ERROR) {
status.setError(Messages.format(NewWizardMessages.NewTypeWizardPage_error_InvalidTypeName, val.getMessage()));
return status;
} else if (val.getSeverity() == IStatus.WARNING) {
status.setWarning(Messages.format(NewWizardMessages.NewTypeWizardPage_warning_TypeNameDiscouraged, val.getMessage()));
// continue checking
}
// must not exist
if (!isEnclosingTypeSelected()) {
IPackageFragment pack = getPackageFragment();
if (pack != null) {
ICompilationUnit cu = pack.getCompilationUnit(getCompilationUnitName(typeName));
fCurrType = cu.getType(typeName);
IResource resource = cu.getResource();
if (resource.exists()) {
status.setError(NewWizardMessages.NewTypeWizardPage_error_TypeNameExists);
return status;
}
if (!ResourcesPlugin.getWorkspace().validateFiltered(resource).isOK()) {
status.setError(NewWizardMessages.NewTypeWizardPage_error_TypeNameFiltered);
return status;
}
URI location = resource.getLocationURI();
if (location != null) {
try {
IFileStore store = EFS.getStore(location);
if (store.fetchInfo().exists()) {
status.setError(NewWizardMessages.NewTypeWizardPage_error_TypeNameExistsDifferentCase);
return status;
}
} catch (CoreException e) {
status.setError(Messages.format(NewWizardMessages.NewTypeWizardPage_error_uri_location_unkown, BasicElementLabels.getURLPart(Resources.getLocationString(resource))));
}
}
}
} else {
IType type = getEnclosingType();
if (type != null) {
fCurrType = type.getType(typeName);
if (fCurrType.exists()) {
status.setError(NewWizardMessages.NewTypeWizardPage_error_TypeNameExists);
return status;
}
}
}
if (!typeNameWithParameters.equals(typeName) && project != null) {
if (!JavaModelUtil.is50OrHigher(project)) {
status.setError(NewWizardMessages.NewTypeWizardPage_error_TypeParameters);
return status;
}
//$NON-NLS-1$//$NON-NLS-2$
String typeDeclaration = "class " + typeNameWithParameters + " {}";
ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
parser.setSource(typeDeclaration.toCharArray());
parser.setProject(project);
CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null);
IProblem[] problems = compilationUnit.getProblems();
if (problems.length > 0) {
status.setError(Messages.format(NewWizardMessages.NewTypeWizardPage_error_InvalidTypeName, problems[0].getMessage()));
return status;
}
}
return status;
}
use of org.eclipse.jdt.core.dom.ASTParser in project bndtools by bndtools.
the class BaselineErrorHandler method findPackageInfoJavaVersionLocation.
ISourceRange findPackageInfoJavaVersionLocation(String packageName, ICompilationUnit compUnit) throws JavaModelException {
ISourceRange range = null;
IPackageDeclaration[] pkgDecls = compUnit.getPackageDeclarations();
if (pkgDecls != null) {
for (IPackageDeclaration pkgDecl : pkgDecls) {
if (packageName.equals(pkgDecl.getElementName())) {
IAnnotation[] annots = pkgDecl.getAnnotations();
for (IAnnotation annot : annots) {
String name = annot.getElementName();
if (ANNOTATION_VERSION_NO_PKG.equals(name) || ANNOTATION_VERSION_OSGI.equals(name) || ANNOTATION_VERSION_BND.equals(name)) {
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(compUnit);
parser.setResolveBindings(true);
CompilationUnit ast = (CompilationUnit) parser.createAST(null);
if (ast != null) {
MemberValuePairLocationRetriever mvpRetriever = new MemberValuePairLocationRetriever(annot, new Predicate<String>() {
@Override
public boolean test(String t) {
return ANNOTATION_VERSION_BND.equals(t) || ANNOTATION_VERSION_OSGI.equals(t);
}
}, "value");
ast.accept(mvpRetriever);
range = mvpRetriever.getMemberValuePairSourceRange();
}
}
}
}
}
}
return range;
}
Aggregations