/** Adds the onReceive method to the broadcast receiver class */ @SuppressWarnings("unchecked") private void addOnReceiveMethod() { MethodDeclaration onReceiveMethod = ast.newMethodDeclaration(); onReceiveMethod.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD)); onReceiveMethod.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID)); onReceiveMethod.setName(ast.newSimpleName(ONRECEIVE_METHOD_NAME)); addMethodParameter( onReceiveMethod, getName(CONTEXT_CLASS).toLowerCase(), ast.newSimpleType(ast.newSimpleName(getName(CONTEXT_CLASS)))); addMethodParameter( onReceiveMethod, getName(INTENT_CLASS).toLowerCase(), ast.newSimpleType(ast.newSimpleName(getName(INTENT_CLASS)))); addEmptyBlock(onReceiveMethod); classDecl.bodyDeclarations().add(onReceiveMethod); // Adds JavaDoc to the method addComment( onReceiveMethod, CodeUtilsNLS.MODEL_BroadcastReceiverClass_onReceiveMethodDescription); addMethodReference( onReceiveMethod, BROADCAST_RECEIVER_SUPERCLASS, ONRECEIVE_METHOD_NAME, new Type[] { ast.newSimpleType(ast.newSimpleName(getName(CONTEXT_CLASS))), ast.newSimpleType(ast.newSimpleName(getName(INTENT_CLASS))) }); }
/* * @see ASTVisitor#visit(MethodDeclaration) */ public boolean visit(MethodDeclaration node) { if (node.getJavadoc() != null) { node.getJavadoc().accept(this); } if (node.getAST().apiLevel() >= AST.JLS3) { printModifiers(node.modifiers()); if (!node.typeParameters().isEmpty()) { this.fBuffer.append("<"); // $NON-NLS-1$ for (Iterator it = node.typeParameters().iterator(); it.hasNext(); ) { TypeParameter t = (TypeParameter) it.next(); t.accept(this); if (it.hasNext()) { this.fBuffer.append(", "); // $NON-NLS-1$ } } this.fBuffer.append("> "); // $NON-NLS-1$ } } if (!node.isConstructor()) { if (node.getReturnType2() != null) { node.getReturnType2().accept(this); } else { // methods really ought to have a return type this.fBuffer.append("void"); // $NON-NLS-1$ } this.fBuffer.append(" "); // $NON-NLS-1$ } node.getName().accept(this); this.fBuffer.append("("); // $NON-NLS-1$ for (Iterator it = node.parameters().iterator(); it.hasNext(); ) { SingleVariableDeclaration v = (SingleVariableDeclaration) it.next(); v.accept(this); if (it.hasNext()) { this.fBuffer.append(", "); // $NON-NLS-1$ } } this.fBuffer.append(")"); // $NON-NLS-1$ for (int i = 0; i < node.getExtraDimensions(); i++) { this.fBuffer.append("[]"); // $NON-NLS-1$ } if (!node.thrownExceptions().isEmpty()) { this.fBuffer.append(" throws "); // $NON-NLS-1$ for (Iterator it = node.thrownExceptions().iterator(); it.hasNext(); ) { Name n = (Name) it.next(); n.accept(this); if (it.hasNext()) { this.fBuffer.append(", "); // $NON-NLS-1$ } } this.fBuffer.append(" "); // $NON-NLS-1$ } if (node.getBody() == null) { this.fBuffer.append(";"); // $NON-NLS-1$ } else { node.getBody().accept(this); } return false; }
private Modifier getPublicModifier(MethodDeclaration method) { List<?> list = method.modifiers(); for (Object o : list) { if (o.getClass().equals(Modifier.class)) { Modifier mdf = (Modifier) o; if (mdf.getKeyword().equals(PUBLIC_KEYWORD)) { return mdf; } } } return null; }
/** * Checks whether the given method is a JUnit 4 unit test. * * @param md Method declaration. * @return True if it is a unit test of JUnit 4. */ private boolean isJUnit4Test(MethodDeclaration md) { List modifiers = md.modifiers(); for (Iterator i = modifiers.iterator(); i.hasNext(); ) { IExtendedModifier modifer = (IExtendedModifier) i.next(); if (modifer.isAnnotation()) { Annotation annotation = (Annotation) modifer; if ("Test".equals(annotation.getTypeName().getFullyQualifiedName())) { return true; } } } return false; }
/* * @see ASTVisitor#visit(MethodDeclaration) */ @Override public boolean visit(MethodDeclaration node) { if (!isAffected(node)) { return false; } doVisitNode(node.getJavadoc()); doVisitChildren(node.modifiers()); doVisitChildren(node.typeParameters()); if (!node.isConstructor()) { doVisitNode(node.getReturnType2()); } // name not visited int apiLevel = node.getAST().apiLevel(); if (apiLevel >= AST.JLS8) { doVisitNode(node.getReceiverType()); } // receiverQualifier not visited: // Enclosing class names cannot be shadowed by an import (qualification is always redundant). doVisitChildren(node.parameters()); if (apiLevel >= AST.JLS8) { doVisitChildren(node.extraDimensions()); doVisitChildren(node.thrownExceptionTypes()); } else { Iterator<Name> iter = getThrownExceptions(node).iterator(); while (iter.hasNext()) { typeRefFound(iter.next()); } } if (!fSkipMethodBodies) { doVisitNode(node.getBody()); } return false; }
private List<ClassObject> parseAST(CompilationUnit compilationUnit, IFile iFile) { List<ClassObject> classObjects = new ArrayList<ClassObject>(); List<AbstractTypeDeclaration> topLevelTypeDeclarations = compilationUnit.types(); for (AbstractTypeDeclaration abstractTypeDeclaration : topLevelTypeDeclarations) { if (abstractTypeDeclaration instanceof TypeDeclaration) { TypeDeclaration topLevelTypeDeclaration = (TypeDeclaration) abstractTypeDeclaration; List<TypeDeclaration> typeDeclarations = new ArrayList<TypeDeclaration>(); typeDeclarations.add(topLevelTypeDeclaration); typeDeclarations.addAll(getRecursivelyInnerTypes(topLevelTypeDeclaration)); for (TypeDeclaration typeDeclaration : typeDeclarations) { final ClassObject classObject = new ClassObject(); classObject.setIFile(iFile); classObject.setName(typeDeclaration.resolveBinding().getQualifiedName()); classObject.setTypeDeclaration(typeDeclaration); if (typeDeclaration.isInterface()) { classObject.setInterface(true); } int modifiers = typeDeclaration.getModifiers(); if ((modifiers & Modifier.ABSTRACT) != 0) classObject.setAbstract(true); if ((modifiers & Modifier.PUBLIC) != 0) classObject.setAccess(Access.PUBLIC); else if ((modifiers & Modifier.PROTECTED) != 0) classObject.setAccess(Access.PROTECTED); else if ((modifiers & Modifier.PRIVATE) != 0) classObject.setAccess(Access.PRIVATE); else classObject.setAccess(Access.NONE); if ((modifiers & Modifier.STATIC) != 0) classObject.setStatic(true); Type superclassType = typeDeclaration.getSuperclassType(); if (superclassType != null) { ITypeBinding binding = superclassType.resolveBinding(); String qualifiedName = binding.getQualifiedName(); TypeObject typeObject = TypeObject.extractTypeObject(qualifiedName); classObject.setSuperclass(typeObject); } List<Type> superInterfaceTypes = typeDeclaration.superInterfaceTypes(); for (Type interfaceType : superInterfaceTypes) { ITypeBinding binding = interfaceType.resolveBinding(); String qualifiedName = binding.getQualifiedName(); TypeObject typeObject = TypeObject.extractTypeObject(qualifiedName); classObject.addInterface(typeObject); } FieldDeclaration[] fieldDeclarations = typeDeclaration.getFields(); for (FieldDeclaration fieldDeclaration : fieldDeclarations) { Type fieldType = fieldDeclaration.getType(); ITypeBinding binding = fieldType.resolveBinding(); List<VariableDeclarationFragment> fragments = fieldDeclaration.fragments(); for (VariableDeclarationFragment fragment : fragments) { String qualifiedName = binding.getQualifiedName(); TypeObject typeObject = TypeObject.extractTypeObject(qualifiedName); typeObject.setArrayDimension( typeObject.getArrayDimension() + fragment.getExtraDimensions()); FieldObject fieldObject = new FieldObject(typeObject, fragment.getName().getIdentifier()); fieldObject.setClassName(classObject.getName()); fieldObject.setVariableDeclarationFragment(fragment); int fieldModifiers = fieldDeclaration.getModifiers(); if ((fieldModifiers & Modifier.PUBLIC) != 0) fieldObject.setAccess(Access.PUBLIC); else if ((fieldModifiers & Modifier.PROTECTED) != 0) fieldObject.setAccess(Access.PROTECTED); else if ((fieldModifiers & Modifier.PRIVATE) != 0) fieldObject.setAccess(Access.PRIVATE); else fieldObject.setAccess(Access.NONE); if ((fieldModifiers & Modifier.STATIC) != 0) fieldObject.setStatic(true); classObject.addField(fieldObject); } } MethodDeclaration[] methodDeclarations = typeDeclaration.getMethods(); for (MethodDeclaration methodDeclaration : methodDeclarations) { String methodName = methodDeclaration.getName().getIdentifier(); final ConstructorObject constructorObject = new ConstructorObject(); constructorObject.setMethodDeclaration(methodDeclaration); constructorObject.setName(methodName); constructorObject.setClassName(classObject.getName()); int methodModifiers = methodDeclaration.getModifiers(); if ((methodModifiers & Modifier.PUBLIC) != 0) constructorObject.setAccess(Access.PUBLIC); else if ((methodModifiers & Modifier.PROTECTED) != 0) constructorObject.setAccess(Access.PROTECTED); else if ((methodModifiers & Modifier.PRIVATE) != 0) constructorObject.setAccess(Access.PRIVATE); else constructorObject.setAccess(Access.NONE); List<SingleVariableDeclaration> parameters = methodDeclaration.parameters(); for (SingleVariableDeclaration parameter : parameters) { Type parameterType = parameter.getType(); ITypeBinding binding = parameterType.resolveBinding(); String qualifiedName = binding.getQualifiedName(); TypeObject typeObject = TypeObject.extractTypeObject(qualifiedName); typeObject.setArrayDimension( typeObject.getArrayDimension() + parameter.getExtraDimensions()); if (parameter.isVarargs()) { typeObject.setArrayDimension(1); } ParameterObject parameterObject = new ParameterObject(typeObject, parameter.getName().getIdentifier()); parameterObject.setSingleVariableDeclaration(parameter); constructorObject.addParameter(parameterObject); } Block methodBody = methodDeclaration.getBody(); if (methodBody != null) { MethodBodyObject methodBodyObject = new MethodBodyObject(methodBody); constructorObject.setMethodBody(methodBodyObject); } if (methodDeclaration.isConstructor()) { classObject.addConstructor(constructorObject); } else { MethodObject methodObject = new MethodObject(constructorObject); List<IExtendedModifier> extendedModifiers = methodDeclaration.modifiers(); for (IExtendedModifier extendedModifier : extendedModifiers) { if (extendedModifier.isAnnotation()) { Annotation annotation = (Annotation) extendedModifier; if (annotation.getTypeName().getFullyQualifiedName().equals("Test")) { methodObject.setTestAnnotation(true); break; } } } Type returnType = methodDeclaration.getReturnType2(); ITypeBinding binding = returnType.resolveBinding(); String qualifiedName = binding.getQualifiedName(); TypeObject typeObject = TypeObject.extractTypeObject(qualifiedName); methodObject.setReturnType(typeObject); if ((methodModifiers & Modifier.ABSTRACT) != 0) methodObject.setAbstract(true); if ((methodModifiers & Modifier.STATIC) != 0) methodObject.setStatic(true); if ((methodModifiers & Modifier.SYNCHRONIZED) != 0) methodObject.setSynchronized(true); if ((methodModifiers & Modifier.NATIVE) != 0) methodObject.setNative(true); classObject.addMethod(methodObject); FieldInstructionObject fieldInstruction = methodObject.isGetter(); if (fieldInstruction != null) systemObject.addGetter(methodObject.generateMethodInvocation(), fieldInstruction); fieldInstruction = methodObject.isSetter(); if (fieldInstruction != null) systemObject.addSetter(methodObject.generateMethodInvocation(), fieldInstruction); fieldInstruction = methodObject.isCollectionAdder(); if (fieldInstruction != null) systemObject.addCollectionAdder( methodObject.generateMethodInvocation(), fieldInstruction); MethodInvocationObject methodInvocation = methodObject.isDelegate(); if (methodInvocation != null) systemObject.addDelegate(methodObject.generateMethodInvocation(), methodInvocation); } } classObjects.add(classObject); } } } return classObjects; }
/* * @see ASTVisitor#visit(MethodDeclaration) */ @Override public boolean visit(MethodDeclaration node) { if (node.getJavadoc() != null) { node.getJavadoc().accept(this); } if (node.getAST().apiLevel() >= JLS3) { printModifiers(node.modifiers()); if (!node.typeParameters().isEmpty()) { this.fBuffer.append("<"); // $NON-NLS-1$ for (Iterator<TypeParameter> it = node.typeParameters().iterator(); it.hasNext(); ) { TypeParameter t = it.next(); t.accept(this); if (it.hasNext()) { this.fBuffer.append(", "); // $NON-NLS-1$ } } this.fBuffer.append("> "); // $NON-NLS-1$ } } if (!node.isConstructor()) { if (node.getReturnType2() != null) { node.getReturnType2().accept(this); } else { // methods really ought to have a return type this.fBuffer.append("void"); // $NON-NLS-1$ } this.fBuffer.append(" "); // $NON-NLS-1$ } node.getName().accept(this); this.fBuffer.append("("); // $NON-NLS-1$ if (node.getAST().apiLevel() >= AST.JLS8) { Type receiverType = node.getReceiverType(); if (receiverType != null) { receiverType.accept(this); this.fBuffer.append(' '); SimpleName qualifier = node.getReceiverQualifier(); if (qualifier != null) { qualifier.accept(this); this.fBuffer.append('.'); } this.fBuffer.append("this"); // $NON-NLS-1$ if (node.parameters().size() > 0) { this.fBuffer.append(','); } } } for (Iterator<SingleVariableDeclaration> it = node.parameters().iterator(); it.hasNext(); ) { SingleVariableDeclaration v = it.next(); v.accept(this); if (it.hasNext()) { this.fBuffer.append(", "); // $NON-NLS-1$ } } this.fBuffer.append(")"); // $NON-NLS-1$ if (node.getAST().apiLevel() >= AST.JLS8) { List<Dimension> dimensions = node.extraDimensions(); for (Iterator<Dimension> it = dimensions.iterator(); it.hasNext(); ) { Dimension e = it.next(); e.accept(this); } } else { for (int i = 0; i < node.getExtraDimensions(); i++) { this.fBuffer.append("[]"); // $NON-NLS-1$ } } List<? extends ASTNode> thrownExceptions = node.getAST().apiLevel() >= AST.JLS8 ? node.thrownExceptionTypes() : getThrownExceptions(node); if (!thrownExceptions.isEmpty()) { this.fBuffer.append(" throws "); // $NON-NLS-1$ for (Iterator<? extends ASTNode> it = thrownExceptions.iterator(); it.hasNext(); ) { ASTNode n = it.next(); n.accept(this); if (it.hasNext()) { this.fBuffer.append(", "); // $NON-NLS-1$ } } this.fBuffer.append(" "); // $NON-NLS-1$ } if (node.getBody() == null) { this.fBuffer.append(";"); // $NON-NLS-1$ } else { node.getBody().accept(this); } return false; }