List of usage examples for org.eclipse.jdt.core.dom MethodDeclaration getStructuralProperty
public final Object getStructuralProperty(StructuralPropertyDescriptor property)
From source file:org.gw4e.eclipse.facade.JDTManager.java
License:Open Source License
/** * @param project// www . ja v a 2 s .com * @param itype * @return * @throws JavaModelException */ public static Map<String, List<String>> findSetPathGeneratorInvocation(IProject project, IType itype) throws JavaModelException { Map<String, List<String>> ret = new HashMap<String, List<String>>(); ICompilationUnit cu = itype.getCompilationUnit(); CompilationUnit ast = parse(cu); ast.accept(new ASTVisitor() { public boolean visit(MethodDeclaration node) { List<?> modifiers = (List<?>) node.getStructuralProperty(MethodDeclaration.MODIFIERS2_PROPERTY); for (Object modifier : modifiers) { if (modifier instanceof org.eclipse.jdt.core.dom.Annotation) { IAnnotationBinding annotationBinding = ((org.eclipse.jdt.core.dom.Annotation) modifier) .resolveAnnotationBinding(); if (annotationBinding != null) { final String qualifiedName = annotationBinding.getAnnotationType().getQualifiedName(); if ("org.junit.Test".equalsIgnoreCase(qualifiedName)) { Map<String, String> variables = new HashMap<String, String>(); node.accept(new ASTVisitor() { public boolean visit(VariableDeclarationStatement node) { for (int i = 0; i < node.fragments().size(); ++i) { VariableDeclarationFragment frag = (VariableDeclarationFragment) node .fragments().get(i); if (isContext(node.getType().resolveBinding())) { Expression initializer = frag.getInitializer(); JDTManager.ExpressionVisitor ev = new ExpressionVisitor(variables); initializer.accept(ev); if (ev.getValue() != null) { variables.put(frag.getName().getFullyQualifiedName(), ev.getValue()); } } } return true; } public boolean visit(MethodInvocation node) { SimpleName simpleName = node.getName(); IBinding bding = simpleName.resolveBinding(); if (bding instanceof IMethodBinding) { IMethodBinding imb = (IMethodBinding) bding; if ("setPathGenerator".equalsIgnoreCase(imb.getName())) { ITypeBinding[] arguments = imb.getParameterTypes(); if (arguments.length == 1) { if (isContext(imb.getDeclaringClass()) && PathGenerator.class.getName() .equals(arguments[0].getQualifiedName()) && isContext(imb.getReturnType())) { int start = node.getStartPosition(); int end = node.getLength(); try { String code = cu.getSource().substring(start, start + end); // System.out.println(code); } catch (JavaModelException e) { ResourceManager.logException(e); } List args = node.arguments(); Expression argumentExpression = (Expression) args.get(0); ITypeBinding typeBinding = argumentExpression .resolveTypeBinding(); String parameterName = ""; if (typeBinding != null) { parameterName = argumentExpression.toString(); JDTManager.ExpressionVisitor ev = new ExpressionVisitor( variables); Expression expression = node.getExpression(); expression.accept(ev); if (ev.getValue() != null) { String contextClass = ev.getValue(); List<String> generators = ret.get(contextClass); if (generators == null) { generators = new ArrayList<String>(); ret.put(contextClass, generators); } if (!"null".equals(parameterName) && !generators.contains(parameterName)) { generators.add(parameterName); } } } } } } } return true; } }); } } } } return true; } }); return ret; }
From source file:org.jboss.tools.ws.jaxrs.core.jdt.JavaAnnotationLocator.java
License:Open Source License
/** * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.MethodDeclaration) *//*from ww w . ja v a2 s . c o m*/ @Override public boolean visit(MethodDeclaration declaration) { visitExtendedModifiers((List<?>) declaration.getStructuralProperty(MethodDeclaration.MODIFIERS2_PROPERTY)); return this.locatedAnnotation == null; }
From source file:org.jboss.tools.ws.jaxrs.core.jdt.JavaAnnotationsVisitor.java
License:Open Source License
/** * {@inheritDoc}/*from ww w .j a va 2s .com*/ * * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom. MethodDeclaration) */ @Override public final boolean visit(final MethodDeclaration node) { if (memberType == IJavaElement.METHOD && node.getName().getFullyQualifiedName().equals(memberName) && matchesLocation(node)) { visitExtendedModifiers((List<?>) node.getStructuralProperty(MethodDeclaration.MODIFIERS2_PROPERTY)); return false; } return true; }
From source file:processing.mode.java.pdex.ASTGenerator.java
License:Open Source License
public static CompletionCandidate[] checkForTypes(ASTNode node) { List<VariableDeclarationFragment> vdfs = null; switch (node.getNodeType()) { case ASTNode.TYPE_DECLARATION: return new CompletionCandidate[] { new CompletionCandidate((TypeDeclaration) node) }; case ASTNode.METHOD_DECLARATION: MethodDeclaration md = (MethodDeclaration) node; log(getNodeAsString(md));//from w w w. j a va 2 s . c om List<ASTNode> params = (List<ASTNode>) md.getStructuralProperty(MethodDeclaration.PARAMETERS_PROPERTY); CompletionCandidate[] cand = new CompletionCandidate[params.size() + 1]; cand[0] = new CompletionCandidate(md); for (int i = 0; i < params.size(); i++) { // cand[i + 1] = new CompletionCandidate(params.get(i).toString(), "", "", // CompletionCandidate.LOCAL_VAR); cand[i + 1] = new CompletionCandidate((SingleVariableDeclaration) params.get(i)); } return cand; case ASTNode.SINGLE_VARIABLE_DECLARATION: return new CompletionCandidate[] { new CompletionCandidate((SingleVariableDeclaration) node) }; case ASTNode.FIELD_DECLARATION: vdfs = ((FieldDeclaration) node).fragments(); break; case ASTNode.VARIABLE_DECLARATION_STATEMENT: vdfs = ((VariableDeclarationStatement) node).fragments(); break; case ASTNode.VARIABLE_DECLARATION_EXPRESSION: vdfs = ((VariableDeclarationExpression) node).fragments(); break; default: break; } if (vdfs != null) { CompletionCandidate ret[] = new CompletionCandidate[vdfs.size()]; int i = 0; for (VariableDeclarationFragment vdf : vdfs) { // ret[i++] = new CompletionCandidate(getNodeAsString2(vdf), "", "", // CompletionCandidate.LOCAL_VAR); ret[i++] = new CompletionCandidate(vdf); } return ret; } return null; }
From source file:processing.mode.java.pdex.CompletionCandidate.java
License:Open Source License
public CompletionCandidate(MethodDeclaration method) { // log("ComCan " + method.getName()); elementName = method.getName().toString(); type = LOCAL_METHOD;/*from ww w .j a va2 s . c o m*/ @SuppressWarnings("unchecked") List<ASTNode> params = (List<ASTNode>) method.getStructuralProperty(MethodDeclaration.PARAMETERS_PROPERTY); StringBuilder label = new StringBuilder(elementName + "("); StringBuilder cstr = new StringBuilder(method.getName() + "("); for (int i = 0; i < params.size(); i++) { label.append(params.get(i).toString()); if (i < params.size() - 1) { label.append(","); cstr.append(","); } } if (params.size() == 1) { cstr.append(' '); } label.append(")"); if (method.getReturnType2() != null) label.append(" : " + method.getReturnType2()); cstr.append(")"); this.label = label.toString(); this.completionString = cstr.toString(); wrappedObject = method; }
From source file:processing.mode.java.pdex.CompletionCandidate.java
License:Open Source License
public CompletionCandidate withRegeneratedCompString() { if (wrappedObject instanceof MethodDeclaration) { MethodDeclaration method = (MethodDeclaration) wrappedObject; @SuppressWarnings("unchecked") List<ASTNode> params = (List<ASTNode>) method .getStructuralProperty(MethodDeclaration.PARAMETERS_PROPERTY); StringBuilder label = new StringBuilder(elementName + "("); StringBuilder cstr = new StringBuilder(method.getName() + "("); for (int i = 0; i < params.size(); i++) { label.append(params.get(i).toString()); if (i < params.size() - 1) { label.append(","); cstr.append(","); }// www .jav a 2s.c o m } if (params.size() == 1) { cstr.append(' '); } label.append(")"); if (method.getReturnType2() != null) label.append(" : " + method.getReturnType2()); cstr.append(")"); return this.withLabelAndCompString(label.toString(), cstr.toString()); } else if (wrappedObject instanceof Method) { Method method = (Method) wrappedObject; StringBuilder label = new StringBuilder("<html>" + method.getName() + "("); StringBuilder cstr = 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(","); cstr.append(","); } } if (method.getParameterTypes().length == 1) { cstr.append(' '); } label.append(")"); if (method.getReturnType() != null) label.append(" : " + method.getReturnType().getSimpleName()); label.append(" - <font color=#777777>" + method.getDeclaringClass().getSimpleName() + "</font></html>"); cstr.append(")"); return this.withLabelAndCompString(label.toString(), cstr.toString()); /* * StringBuilder label = new StringBuilder("<html>"+method.getName() + "("); StringBuilder cstr = 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(","); cstr.append(","); } } if(method.getParameterTypes().length == 1) { cstr.append(' '); } label.append(")"); if(method.getReturnType() != null) label.append(" : " + method.getReturnType().getSimpleName()); label.append(" - <font color=#777777>" + method.getDeclaringClass().getSimpleName() + "</font></html>"); * */ } return this; }