public ConstructId deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { // To be returned ConstructId cid = null; final JsonObject c = (JsonObject)json; final String t = c.getAsJsonPrimitive("type").getAsString(); final String qn = c.getAsJsonPrimitive("qname").getAsString(); if(JavaId.typeToString(JavaId.Type.PACKAGE).equals(t)) cid = new JavaPackageId(qn); else if(JavaId.typeToString(JavaId.Type.CLASS).equals(t)) cid = JavaId.parseClassQName(qn); else if(JavaId.typeToString(JavaId.Type.CLASSINIT).equals(t)) cid = JavaId.parseClassInitQName(qn); else if(JavaId.typeToString(JavaId.Type.METHOD).equals(t)) cid = JavaId.parseMethodQName(qn); else if(JavaId.typeToString(JavaId.Type.CONSTRUCTOR).equals(t)) cid = JavaId.parseConstructorQName(qn); //TODO: Add annotations return cid; } }
public ConstructType getSharedType() { return JavaId.toSharedType(this.getType()); }
@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 the fully qualified constructor name, including package and (unqualified) parameter types. * Example: test.package.TestClass(int) */ public String getQualifiedName() { return context.getQualifiedName() + JavaId.parameterTypesToString(this.parameterTypes, true); }
public String toString() { return this.getLanguage() + " " + JavaId.typeToString(this.type) + " [" + this.getQualifiedName() + "]"; }
final JavaId target_jid = (JavaId)JavaId.toCoreType(target); final JavaId comp_unit = target_jid.getCompilationUnit(); final boolean is_nested_class = comp_unit instanceof JavaClassId && ((JavaClassId)comp_unit).isNestedClass(); ClassPool cp = ClassPoolUpdater.getInstance().getCustomClassPool(); if(cp==null)cp=ClassPool.getDefault(); clazz = cp.get(comp_unit.getQualifiedName()); 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) {
return JavaId.parseConstructorQName(_qname); return JavaId.parseClassInitQName(_qname.substring(0, _qname.lastIndexOf('>')+1)); return JavaId.parseMethodQName(_qname); return JavaId.parseClassQName(_qname); return JavaId.parseEnumQName(_qname);
if (_method.isClinit()) { qname = _method.getSignature().substring(0, _method.getSignature().indexOf("<") - 1); cid = JavaId.toSharedType(JavaId.parseClassQName(qname).getClassInit()); } else { int p_size = _method.getNumberOfParameters(); cid = JavaId.toSharedType(JavaId.parseConstructorQName(qname)); } else { for (int i = (_method.isStatic() ? 0 : 1); i < p_size; i++) { qname = _method.getSignature().substring(0, _method.getSignature().indexOf("(")) + params; qname = ClassVisitor.removeParameterQualification(qname); cid = JavaId.toSharedType(JavaId.parseMethodQName(qname));
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; }
private JavaId getJavaId(String _type, String _qname) { JavaId.Type type = JavaId.typeFromString(_type); // Check params if(JavaId.Type.METHOD!=type && JavaId.Type.CONSTRUCTOR!=type) throw new IllegalArgumentException("Only types METH and CONS are supported, got [" + type + "]"); // Parse JavaId JavaId jid = null; if(JavaId.Type.CONSTRUCTOR==type) jid = JavaId.parseConstructorQName(_qname); else if(JavaId.Type.METHOD==type) jid = JavaId.parseMethodQName(_qname); return jid; }
source_code.append("} catch(Throwable e) { System.err.println(e.getClass().getName() + \" occurred during execution of instrumentation code in " + _jid.toString() + ": \" + e.getMessage()); }"); if(_jid.getType().equals(JavaId.Type.CONSTRUCTOR) || _jid.getType().equals(JavaId.Type.CLASSINIT)) _behavior.insertAfter(source_code.toString()); else Path p = null; try { p = Paths.get(VulasConfiguration.getGlobal().getTmpDir().toString(), _jid.getJavaPackageId().getQualifiedName().replace('.','/'), _jid.getDefinitionContext().getName() + "." + _jid.getName().replace('<', '_').replace('>','_') + ".java"); FileUtil.createDirectory(p.getParent()); FileUtil.writeToFile(p.toFile(), ClassVisitor.prettyPrint(source_code.toString()));
/** * Merges the constructs of the map into a single set. Returns a TreeSet if _ordered is equal to true, a HashSet otherwise. * @param _map * @return a single set containing all the constructs passed in _map */ public static final Set<ConstructId> merge(Map<String, Set<com.sap.psr.vulas.shared.json.model.ConstructId>> _map, boolean _ordered) { final Set<ConstructId> set = ( _ordered ? new TreeSet<ConstructId>() : new HashSet<ConstructId>() ); for(Object key: _map.keySet()) { for(com.sap.psr.vulas.shared.json.model.ConstructId cid: _map.get(key)) { set.add(JavaId.toCoreType(cid)); } } return set; } }
log.info("Determined compilation unit " + def_ctx + " for qname [" + qname + "]"); file = ClassDownloader.getInstance().getClass(doc.getLibId().getMvnGroup(), doc.getLibId().getArtifact(), doc.getLibId().getVersion(), def_ctx.getQualifiedName(), ClassDownloader.Format.JAVA); log.warn("Cannot retrieve class [" + def_ctx.getQualifiedName() + "]"); return new ResponseEntity<ASTConstructBodySignature>(HttpStatus.NOT_FOUND); SignatureFactory sf = CoreConfiguration.getSignatureFactory(JavaId.toSharedType(jid)); ASTConstructBodySignature sign = (ASTConstructBodySignature)sf.createSignature(JavaId.toSharedType(jid), file.toFile()); return new ResponseEntity<ASTConstructBodySignature>(sign, HttpStatus.OK); else { log.warn("Cannot construct signature for class [" + def_ctx.getQualifiedName() + "] in SHA1: " + sha1); return new ResponseEntity<ASTConstructBodySignature>(sign, HttpStatus.NOT_FOUND);
/** * Returns the fully qualified class name, i.e., including the name of the package in which the class is defined. */ @Override public String getQualifiedName() { if(this.declarationContext!=null) { if(!this.isNested()) return this.declarationContext.getQualifiedName() + "." + this.enumName; else return this.declarationContext.getQualifiedName() + "$" + this.enumName; } else { return this.enumName; } }
cons_cid = JavaId.parseConstructorQName(qname); for(Annotation a: con.getAnnotations()) cons_cid.addAnnotation(a.annotationType().getName()); construct_ids.add(cons_cid); final JavaClassId jcid = JavaId.getClassId(c); construct_ids.add(jcid.getClassInit()); meth_cid = JavaId.parseMethodQName(qname); for(Annotation a: m.getAnnotations()) meth_cid.addAnnotation(a.annotationType().getName()); construct_ids.add(meth_cid);
private synchronized URL collectArchiveInformation(com.sap.psr.vulas.shared.json.model.ConstructId tgt_node) { URL url = null; final JavaId jid = ((JavaId)JavaId.toCoreType(tgt_node)).getCompilationUnit(); // We should always have a Java ID, since packages are not part of the callgraph if(jid!=null) { // Not in cache -> put in cache if(!this.cachedJarUrls.containsKey(jid)) { url = jid.getJarUrl(); this.cachedJarUrls.put(jid, url); // Warn if we do not find a URL if(url==null) constructsWithoutJarUrl.add(tgt_node); } // Read from cache url = this.cachedJarUrls.get(jid); } return url; }
if(initializer != null && this.javaId.getType()==Type.CLASS) { jid = ((JavaClassId)this.javaId).getClassInit(); constructs.add(jid); jid = JavaId.parseConstructorQName(this.javaId.getType(), ClassVisitor.removeParameterQualification(constr.getLongName()), param_to_skip); constructs.add(jid);
/** * Creates a {@link JavaConstructorId} from the given string, whereby the definition context is defaulted to * type {@link JavaId.Type#CLASS}. * @param _s * @return */ public static JavaConstructorId parseConstructorQName(String _s) { return JavaId.parseConstructorQName(JavaId.Type.CLASS, _s, null); }
public ClassVisitor(CtClass _c) { // Build the JavaId if(_c.isInterface()) throw new IllegalArgumentException("[" + _c.getName() + "]: Interfaces are not supported"); else if(_c.isEnum()) this.javaId = JavaId.parseEnumQName(_c.getName()); else this.javaId = JavaId.parseClassQName(_c.getName()); this.qname = this.javaId.getQualifiedName(); this.c = _c; // Remember major/minor final ClassFile cf = _c.getClassFile(); this.major = cf.getMajorVersion(); this.minor = cf.getMinorVersion(); // For nested classes, get the declaring (outer) class: It is used to skip the first argument in non-static inner classes try { this.declaringClass = _c.getDeclaringClass(); } catch (NotFoundException e) { // Only a problem in case of non-static inner classes, because in that case the 1st argument of the constructor cannot be removed, cf. method visitConstructors(boolean) if(!Modifier.isStatic(this.c.getModifiers())) ClassVisitor.getLog().warn("No declaring class found for non-static inner class [" + this.javaId.getQualifiedName() + "]");//: " + e.getMessage()); } writeCodeToTmp = VulasConfiguration.getGlobal().getConfiguration().getBoolean(CoreConfiguration.INSTR_WRITE_CODE, false); }
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; }