/** * Returns the Java package in the context of which the construct is defined. * @return */ @Override public JavaPackageId getJavaPackageId() { if(this.isNestedClass()) return this.declarationContext.getJavaPackageId(); else return (JavaPackageId)this.declarationContext; }
public static JavaClassInit parseClassInitQName(String _s) { if(_s == null || _s.equals("")) throw new IllegalArgumentException("String null or empty"); final int i = _s.indexOf(JavaClassInit.NAME); if(i==-1) throw new IllegalArgumentException("String does not contain brackets " + JavaClassInit.NAME + ", as required for qualified names for Java class initializers"); JavaClassInit clinit = null; try { final JavaClassId cid = JavaId.parseClassQName(_s.substring(0, i-1)); clinit = cid.getClassInit(); } catch(StringIndexOutOfBoundsException e) { JavaId.log.error("Exception while parsing the string '" + _s + "'"); } return clinit; }
if(j!=-1) { if(cid==null) cid = new JavaClassId(pid, class_name.substring(k, j)); else cid = new JavaClassId(cid, class_name.substring(k, j)); k = j+1; cid = new JavaClassId(pid, class_name.substring(k)); else cid = new JavaClassId(cid, class_name.substring(k)); k = j+1;
public String getQualifiedName() { return classContext.getQualifiedName() + "." + JavaClassInit.NAME; }
public JavaPackageId getJavaPackageId() { return this.classContext.getJavaPackageId(); } }
/** * 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; } }
/** * Reads the bytecode of the given {@link CtClass} and writes it to a temporary file. * @param _cid * @param _class * @return the temporary file to which the byte code has been written * @throws IOException */ private Path writeBytesToTmpFile(JavaClassId _cid, CtClass _class) throws IOException { final Path class_file = Files.createTempFile(VulasConfiguration.getGlobal().getTmpDir(), _cid.getQualifiedName(), ".class", new FileAttribute[] {}); FileUtil.writeToFile(class_file.toFile(), this.readBytes(_class)); return class_file; }
/** * Returns a class name that is unique within the package in which the class is defined. * In case of nested classes, the names of parent classes will be included (e.g., OuterClass$InnerClass). * @returns the class name including the names of parent classes (if any) */ @Override public String getName() { if(this.declarationContext!=null) { if(!this.isNestedClass()) return this.className; else return this.declarationContext.getName() + "$" + this.className; } else { return this.className; } }
construct_ids.add(jcid.getClassInit());
final CtClass ctclass = cp.get(class_id.getQualifiedName()); class_file = this.writeBytesToTmpFile(class_id, ctclass); signature = this.createSignature(_cid, class_file.toFile()); } catch (NotFoundException nfe) { throw new IllegalStateException("Error while searching class [" + class_id.getQualifiedName() + "]: " + nfe.getMessage()); } catch (IOException ioe) { throw new IllegalStateException("Error while writing temp file for construct [" + _cid.getQname() + "]: " + ioe.getMessage());
final ConstructId id = new JavaClassId(context, class_name.toString()); return id;
/** * Returns the fully qualified class name, i.e., including the name of the package in which the class is defined. */ @Override public String getQualifiedName() { final StringBuilder builder = new StringBuilder(); if(this.declarationContext!=null) { final String prefix = this.declarationContext.getQualifiedName(); builder.append(prefix); // Empty string in case of default package // Outer class if(!this.isNestedClass()) { if(!prefix.equals("")) // Could also use JavaPackageId.isDefaultPackage builder.append("."); } // Inner class else { if(!prefix.equals("")) // Should probably never happen builder.append("$"); } } builder.append(this.className); return builder.toString(); }
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();
private boolean isNestedClassConstruct() { final com.sap.psr.vulas.ConstructId c = JavaId.toCoreType(this.constructId).getDefinitionContext(); if(c instanceof JavaClassId) return ((JavaClassId)c).isNestedClass(); else return false; }
jid = ((JavaClassId)this.javaId).getClassInit(); constructs.add(jid);
/** * Returns true if the signature creation is supported for the given {@link ConstructId}. This depends * on whether the ID's definition context can be obtained, and whether the latter is a nested class. * @param _cid */ static final boolean isSupported(ConstructId _cid, boolean _throw_exception) throws IllegalArgumentException { boolean supported = true; // Get and check the definition context of the construct whose signature we're about to create final JavaClassId class_id = (JavaClassId)JavaId.toCoreType(_cid).getDefinitionContext(); // Cannot get the def context if(class_id==null) { supported = false; if(_throw_exception) throw new IllegalArgumentException("No definition context for construct [" + _cid.getQname() + "]"); } // Nested class else if(class_id.isNestedClass()) { supported = false; JavaSignatureFactory.log.error("Nested classes are not yet supported, cannot create signature for [" + _cid.getQname() + "]"); if(_throw_exception) throw new IllegalArgumentException("Nested classes are not yet supported, cannot create signature for [" + _cid.getQname() + "]"); } return supported; }
final boolean is_nested_class = comp_unit instanceof JavaClassId && ((JavaClassId)comp_unit).isNestedClass();
/** * * @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()); } }