/** * Creates a {@link JavaMethodId} from the given string, with a definition context of the given type. * Accepted types are {@link JavaId.Type#CLASS} and {@link JavaId.Type#ENUM}. * @param _s * @return */ public static JavaMethodId parseMethodQName(JavaId.Type _ctx_type, String _s) { if(_s == null || _s.equals("")) throw new IllegalArgumentException("String null or empty"); if(_ctx_type==null || (!_ctx_type.equals(JavaId.Type.CLASS) && !_ctx_type.equals(JavaId.Type.ENUM))) throw new IllegalArgumentException("Accepts context types CLASS or ENUM, got [" + _ctx_type + "]"); final int i = _s.indexOf('('); if(i==-1 || !_s.endsWith(")")) throw new IllegalArgumentException("String does not contain brackets (), as required for qualified names for Java methods"); final int j = _s.lastIndexOf('.', i); if(j==-1) throw new IllegalArgumentException("String does not contain dot (.), as required for qualified names for Java methods (as to separate class and method name)"); JavaId def_ctx = null; JavaMethodId mid = null; try { if(_ctx_type.equals(JavaId.Type.CLASS)) def_ctx = JavaId.parseClassQName(_s.substring(0, j)); else if(_ctx_type.equals(JavaId.Type.ENUM)) def_ctx = JavaId.parseEnumQName(_s.substring(0, j)); mid = new JavaMethodId(def_ctx, _s.substring(j+1, i), JavaId.parseParameterTypes(_s.substring(i+1, _s.length()-1))); } catch(StringIndexOutOfBoundsException e) { JavaId.log.error("Exception while parsing the string '" + _s + "'"); } return mid; }
/** * Returns all {@link JavaMethodId}s and {@link JavaConstructuctorId}s belonging to the given class. * @param _set * @param _context * @return */ public static Set<ConstructId> filterWithContext(Set<ConstructId> _set, JavaId _context) { final Set<ConstructId> set = new HashSet<ConstructId>(); JavaId jid = null; for(ConstructId c: _set) { if(c instanceof JavaMethodId) { if( ((JavaMethodId)c).getDefinitionContext().equals(_context) ) set.add(c); } else if(c instanceof JavaConstructorId) { if( ((JavaConstructorId)c).getDefinitionContext().equals(_context) ) set.add(c); } } return set; }
/** * Returns true if the method has the @Test annotation or both the method name and its class name have the postfix or suffix 'test'. * @return */ public boolean isTestMethod() { boolean is_test = this.hasAnnotation("Test"); // @Test annotation not found, let's check the qualified names of the class context and the method itself if(!is_test && this.context.getType().equals(JavaId.Type.CLASS)) { is_test = ((JavaClassId)this.context).isTestClass() && (this.methodId.toLowerCase().startsWith("test") || this.methodId.toLowerCase().endsWith("test")); } return is_test; } }
private boolean isTestConstruct(ConstructId _cid) { com.sap.psr.vulas.ConstructId c = JavaId.toCoreType(_cid); return c instanceof JavaMethodId && ((JavaMethodId)c).isTestMethod(); }
for(Annotation a: m.getAnnotations()) meth_cid.addAnnotation(a.annotationType().getName()); construct_ids.add(meth_cid); } catch (Exception e) {
/** * Returns true if the given {@link ConstructId} is neither part of the application nor a test method, false otherwise. * @param _jid * @return */ public boolean isLibConstruct(ConstructId _jid) { boolean is_lib_construct = false; // We only instrument clinit, methods and constructors if(ConstructIdUtil.isOfInstrumentableType(_jid)) { final ConstructId context = _jid.getDefinitionContext(); is_lib_construct = this.appConstructs!=null && !this.appConstructs.contains(context); // Not part of the app, now check that is not a JUnit test method of the app if(is_lib_construct) { if(_jid instanceof JavaMethodId) { is_lib_construct = !((JavaMethodId)_jid).isTestMethod(); } } } else { ConstructIdUtil.getLog().error("Expected <CLINIT>, method or constructor, got [" + _jid.toString() + "]"); } return is_lib_construct; }
@Override public void enterMethodDeclaration(@NotNull JavaParser.MethodDeclarationContext ctx) { // Peek JavaId and ensure it is a class or enum final JavaId class_ctx = (JavaId) this.contextStack.peek().getConstructId(); this.isOfExpectedType(class_ctx, new JavaId.Type[] { JavaId.Type.CLASS, JavaId.Type.ENUM }, true); // Build the identifier final JavaMethodId id = new JavaMethodId((JavaId) class_ctx, ctx.IDENTIFIER().getText(), this.getParameters(ctx.formalParameters().formalParameterList())); this.contextStack.push(id); this.saveConstruct(id, this.getConstructContent(ctx)); }
private ConstructId filterMethods(StackTraceElement _e, List<ConstructId> _methods) throws NotFoundException { final String class_qname = ((JavaMethodId)_methods.get(0)).getDefinitionContext().getQualifiedName();