use of org.eclipse.jdt.core.dom.TypeDeclaration in project processing by processing.
the class CompletionGenerator method getMembersForType.
public static ArrayList<CompletionCandidate> getMembersForType(PreprocessedSketch ps, ClassMember tehClass, String childToLookFor, boolean noCompare, boolean staticOnly) {
String child = childToLookFor.toLowerCase();
ArrayList<CompletionCandidate> candidates = new ArrayList<>();
log("getMemFoType-> Looking for match " + child + " inside " + tehClass + " noCompare " + noCompare + " staticOnly " + staticOnly);
if (tehClass == null) {
return candidates;
}
// tehClass will either be a TypeDecl defined locally
if (tehClass.getDeclaringNode() instanceof TypeDeclaration) {
TypeDeclaration td = (TypeDeclaration) tehClass.getDeclaringNode();
{
FieldDeclaration[] fields = td.getFields();
for (FieldDeclaration field : fields) {
if (staticOnly && !isStatic(field.modifiers())) {
continue;
}
List<VariableDeclarationFragment> vdfs = field.fragments();
for (VariableDeclarationFragment vdf : vdfs) {
if (noCompare) {
candidates.add(new CompletionCandidate(vdf));
} else if (vdf.getName().toString().toLowerCase().startsWith(child))
candidates.add(new CompletionCandidate(vdf));
}
}
}
{
MethodDeclaration[] methods = td.getMethods();
for (MethodDeclaration method : methods) {
if (staticOnly && !isStatic(method.modifiers())) {
continue;
}
if (noCompare) {
candidates.add(new CompletionCandidate(method));
} else if (method.getName().toString().toLowerCase().startsWith(child))
candidates.add(new CompletionCandidate(method));
}
}
ArrayList<CompletionCandidate> superClassCandidates;
if (td.getSuperclassType() != null) {
log(getNodeAsString(td.getSuperclassType()) + " <-Looking into superclass of " + tehClass);
superClassCandidates = getMembersForType(ps, new ClassMember(ps, td.getSuperclassType()), childToLookFor, noCompare, staticOnly);
} else {
superClassCandidates = getMembersForType(ps, new ClassMember(Object.class), childToLookFor, noCompare, staticOnly);
}
for (CompletionCandidate cc : superClassCandidates) {
candidates.add(cc);
}
return candidates;
}
// Or tehClass will be a predefined class
Class<?> probableClass;
if (tehClass.getClass_() != null) {
probableClass = tehClass.getClass_();
} else {
probableClass = findClassIfExists(ps, tehClass.getTypeAsString());
if (probableClass == null) {
log("Couldn't find class " + tehClass.getTypeAsString());
return candidates;
}
log("Loaded " + probableClass.toString());
}
for (Method method : probableClass.getMethods()) {
if (!Modifier.isStatic(method.getModifiers()) && staticOnly) {
continue;
}
StringBuilder label = new StringBuilder(method.getName() + "(");
for (int i = 0; i < method.getParameterTypes().length; i++) {
label.append(method.getParameterTypes()[i].getSimpleName());
if (i < method.getParameterTypes().length - 1)
label.append(",");
}
label.append(")");
if (noCompare) {
candidates.add(new CompletionCandidate(method));
} else if (label.toString().toLowerCase().startsWith(child)) {
candidates.add(new CompletionCandidate(method));
}
}
for (Field field : probableClass.getFields()) {
if (!Modifier.isStatic(field.getModifiers()) && staticOnly) {
continue;
}
if (noCompare) {
candidates.add(new CompletionCandidate(field));
} else if (field.getName().toLowerCase().startsWith(child)) {
candidates.add(new CompletionCandidate(field));
}
}
if (probableClass.isArray() && !staticOnly) {
// add array members manually, they can't be fetched through code
String className = probableClass.getSimpleName();
if (noCompare || "clone()".startsWith(child)) {
String methodLabel = "<html>clone() : " + className + " - <font color=#777777>" + className + "</font></html>";
candidates.add(new CompletionCandidate("clone()", methodLabel, "clone()", CompletionCandidate.PREDEF_METHOD));
}
if ("length".startsWith(child)) {
String fieldLabel = "<html>length : int - <font color=#777777>" + className + "</font></html>";
candidates.add(new CompletionCandidate("length", fieldLabel, "length", CompletionCandidate.PREDEF_FIELD));
}
}
return candidates;
}
use of org.eclipse.jdt.core.dom.TypeDeclaration in project bndtools by bndtools.
the class BaselineErrorHandler method generateRemovedMethodMarker.
List<MarkerData> generateRemovedMethodMarker(IJavaProject javaProject, final String className, final String methodName, final Delta requiresDelta) throws JavaModelException {
final List<MarkerData> markers = new LinkedList<MarkerData>();
final CompilationUnit ast = createAST(javaProject, className);
if (ast != null) {
ast.accept(new ASTVisitor() {
@Override
public boolean visit(TypeDeclaration typeDecl) {
ITypeBinding typeBinding = typeDecl.resolveBinding();
if (typeBinding != null) {
if (typeBinding.getBinaryName().equals(className)) {
Map<String, Object> attribs = new HashMap<String, Object>();
SimpleName nameNode = typeDecl.getName();
attribs.put(IMarker.CHAR_START, nameNode.getStartPosition());
attribs.put(IMarker.CHAR_END, nameNode.getStartPosition() + nameNode.getLength());
String message = String.format("The method '%s' was removed, which requires a %s change to the package.", methodName, requiresDelta);
attribs.put(IMarker.MESSAGE, message);
markers.add(new MarkerData(ast.getJavaElement().getResource(), attribs, false));
return false;
}
}
return true;
}
});
}
return markers;
}
use of org.eclipse.jdt.core.dom.TypeDeclaration in project bndtools by bndtools.
the class AbstractBuildErrorDetailsHandler method createTypeMarkerData.
/**
* Create a marker on a Java Type
*
* @param javaProject
* @param className
* - the fully qualified class name (e.g java.lang.String)
* @param markerAttributes
* @param hasResolutions
* - true if the marker will have resolutions
* @return Marker Data that can be used to create an {@link IMarker}, or null if no location can be found
* @throws JavaModelException
*/
public static final MarkerData createTypeMarkerData(IJavaProject javaProject, final String className, final Map<String, Object> markerAttributes, boolean hasResolutions) throws JavaModelException {
final CompilationUnit ast = createAST(javaProject, className);
if (ast == null)
return null;
ast.accept(new ASTVisitor() {
@Override
public boolean visit(TypeDeclaration typeDecl) {
ITypeBinding typeBinding = typeDecl.resolveBinding();
if (typeBinding != null) {
if (typeBinding.getBinaryName().equals(className)) {
SimpleName nameNode = typeDecl.getName();
markerAttributes.put(IMarker.CHAR_START, nameNode.getStartPosition());
markerAttributes.put(IMarker.CHAR_END, nameNode.getStartPosition() + nameNode.getLength());
return false;
}
}
return true;
}
});
if (!markerAttributes.containsKey(IMarker.CHAR_START))
return null;
return new MarkerData(ast.getJavaElement().getResource(), markerAttributes, hasResolutions);
}
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[] {};
}
Aggregations