/** * Returns all {@link JavaId}s of the given type(s). */ public static Set<ConstructId> filter(Set<ConstructId> _set, JavaId.Type[] _filter) { final Set<ConstructId> set = new HashSet<ConstructId>(); JavaId jid = null; for(ConstructId c: _set) { if(c instanceof com.sap.psr.vulas.java.JavaId) { jid = (JavaId)c; for(JavaId.Type t: _filter) { if(jid.getType()==t) { set.add(jid); break; } } } } return set; }
private boolean isOfExpectedType(JavaId _jid, JavaId.Type[] _types, boolean _throw_exception) { boolean is = true; if(_jid==null || !Arrays.asList(_types).contains(_jid.getType())) { is = false; if(_throw_exception) { JavaFileAnalyzer2.log.error("Expected [" + _types[0] + "], got " + _jid); throw new IllegalStateException("Expected [" + _types[0] + "], got " + _jid); } else { JavaFileAnalyzer2.log.warn("Expected [" + _types[0] + "], got " + _jid); } is = false; } return is; }
/** * Returns true if the enum is not directly declared within a package, but within another construct like a class or interface. * @return */ public boolean isNested() { return this.declarationContext!=null && !this.declarationContext.getType().equals(JavaId.Type.PACKAGE); }
/** * Returns true if the interface is not directly declared within a package, but within another construct like a class or interface. * @return */ public boolean isNested() { return this.declarationContext!=null && !this.declarationContext.getType().equals(JavaId.Type.PACKAGE); }
/** * This method look into the custom ClassPool and return a string with the * physical path of the JAR if is present in the resources loaded into the * ClassPool. If is not found it returns null * PS: We can add to the ClassPool all the resources that we want using * the method {@ClassPoolUpdater#appendToClasspath} * @param _cid the ConstructId that we want to search in the ClassPool * @return the JAR path or null if is not found */ public URL getJarResourcePath(ConstructId _cid) { URL url = null; if(_cid instanceof JavaId) { final JavaId jid = (JavaId)_cid; if(jid.getType()==JavaId.Type.METHOD || jid.getType()==JavaId.Type.CONSTRUCTOR || jid.getType()==JavaId.Type.CLASSINIT) { url = this.customClassPool.find(_cid.getDefinitionContext().getQualifiedName()); } else if(jid.getType()==JavaId.Type.CLASS || jid.getType()==JavaId.Type.INTERFACE || jid.getType()==JavaId.Type.ENUM || jid.getType()==JavaId.Type.NESTED_CLASS) { url = this.customClassPool.find(_cid.getQualifiedName()); } } return url; }
public boolean isNestedClass() { return this.declarationContext!=null && !this.declarationContext.getType().equals(JavaId.Type.PACKAGE); }
public ConstructType getSharedType() { return JavaId.toSharedType(this.getType()); }
private String printDeclarationStack() { final StringBuilder out = new StringBuilder().append("\t\t\t"); for (ContextStackEntry jid : this.contextStack.all()) { out.append(((JavaId) jid.getConstructId()).getType().toString() + " "); } return out.toString(); }
@Override public JsonObject toGSON(){ final JsonObject jb = new JsonObject(); jb.addProperty("lang", this.getLanguage().toString()); jb.addProperty("type", JavaId.typeToString(this.getType())); jb.addProperty("qname", this.getQualifiedName()); jb.add("a", new Gson().toJsonTree(this.annotations)); return jb; }
/** * 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; } }
public JsonElement serialize(ConstructId src, Type typeOfSrc, JsonSerializationContext context) { final JsonObject c = new JsonObject(); c.addProperty("lang", src.getLanguage().toString()); c.addProperty("type", JavaId.typeToString(((JavaId)src).getType())); c.addProperty("qname", src.getQualifiedName()); final Set<String> annotations = ((JavaId)src).getAnnotations(); if(!annotations.isEmpty()) { final JsonArray anno = new JsonArray(); for(String a: annotations) { anno.add(new JsonPrimitive(a)); } c.add("a", anno); } return c; }
/** * Gets the top-most element <a>of a given types</a> (without removing it). * * @param _t * the type of context elements to consider * @return */ public ContextStackEntry peek(JavaId.Type[] _t) { final Iterator<ContextStackEntry> iterator = this.nestedDeclarationContexts.iterator(); ContextStackEntry entry = null, result = null; search: while (iterator.hasNext()) { entry = iterator.next(); for(int i=0; i<_t.length; i++ ){ if (((JavaId)entry.getConstructId()).getType().equals(_t[i])) { result = entry; break search; } } } return result; }
/** * * @param _jid * @return */ private JavaId getCompilationUnit(JavaId _jid) { // Got it --> return provided object if( (_jid.getType().equals(JavaId.Type.CLASS) && !((JavaClassId)_jid).isNestedClass()) || (_jid.getType().equals(JavaId.Type.INTERFACE) && !((JavaInterfaceId)_jid).isNested()) || (_jid.getType().equals(JavaId.Type.ENUM) && !((JavaEnumId)_jid).isNested()) ) { return _jid; } else { return this.getCompilationUnit((JavaId)_jid.getDefinitionContext()); } }
if(_jid.getType()==JavaId.Type.CONSTRUCTOR) _code.append("com.sap.psr.vulas.monitor.trace.TraceCollector.callbackConstructor"); else if(_jid.getType()==JavaId.Type.METHOD) _code.append("com.sap.psr.vulas.monitor.trace.TraceCollector.callbackMethod"); else if(_jid.getType()==JavaId.Type.CLASSINIT) _code.append("com.sap.psr.vulas.monitor.trace.TraceCollector.callbackClinit");
if(_jid.getType()==JavaId.Type.CONSTRUCTOR) _code.append("com.sap.psr.vulas.monitor.trace.TraceCollector.callbackConstructor"); else if(_jid.getType()==JavaId.Type.METHOD) _code.append("com.sap.psr.vulas.monitor.trace.TraceCollector.callbackMethod"); else if(_jid.getType()==JavaId.Type.CLASSINIT) _code.append("com.sap.psr.vulas.monitor.trace.TraceCollector.callbackClinit");
_code.append("(\"").append(_jid.getType()+"\",\"").append(ClassVisitor.removeParameterQualification(_behavior.getLongName())).append("\",vul_cls_ldr,vul_cls_res," + callee_in_app + ",vul_st,");
public synchronized Set<ConstructId> visitMethods(boolean _instrument) throws CannotCompileException { final Set<ConstructId> constructs = new HashSet<ConstructId>(); final CtMethod[] methods = this.c.getDeclaredMethods(); // Loop all methods JavaId jid = null; for(CtMethod meth : methods) { jid = JavaId.parseMethodQName(this.javaId.getType(), ClassVisitor.removeParameterQualification(meth.getLongName())); constructs.add(jid); // Instrument if requested, not yet done and possible (= not empty, as for abstract methods) final boolean is_native = Modifier.isNative(meth.getModifiers()); if(_instrument && !isInstrumented() && !meth.isEmpty() && !is_native && meth.getLongName().startsWith(this.qname)) { try { this.instrument(jid, meth); } // Can happen if dependencies of the class are not available in the ClassPool catch (CannotCompileException ex) { //ClassVisitor.getLog().info("Exception while instrumenting " + jid + ": " + ex.getMessage()); throw ex; } } } ClassVisitor.getLog().debug("Class '" + this.qname + "': " + methods.length + " methods"); return constructs; }
declaringClass = clazz.getDeclaringClass(); final String param_to_skip = ( declaringClass!=null && !Modifier.isStatic(clazz.getModifiers()) ? ClassVisitor.removePackageContext(declaringClass.getName()) : null ); com.sap.psr.vulas.shared.json.model.ConstructId result = JavaId.toSharedType(JavaId.parseConstructorQName(comp_unit.getType(), ClassVisitor.removeParameterQualification(target.getQname()), param_to_skip)); return result.getQname().contentEquals(target.getQname())? null : result; } catch (NotFoundException e) {
public final String toJSON() { final JsonBuilder jb = new JsonBuilder(); jb.startObject(); jb.appendObjectProperty("lang", this.getLanguage().toString()); jb.appendObjectProperty("type", JavaId.typeToString(this.getType())); jb.appendObjectProperty("qname", this.getQualifiedName()); if(!this.annotations.isEmpty()) { jb.startArrayProperty("a"); for(String a: this.annotations) { jb.appendToArray(a); } jb.endArray(); } jb.endObject(); return jb.getJson(); }