/** * 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; }
def_ctx = JavaId.parseClassQName(_s.substring(0, i)); else if(_ctx_type.equals(JavaId.Type.ENUM)) def_ctx = JavaId.parseEnumQName(_s.substring(0, i));
return JavaId.parseEnumQName(_qname);
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); }
/** * Transforms an object with a given shared type (defined in vulas-share) into * an object having the corresponding core type (defined in vulas-core). * @param _cid * @return */ public static com.sap.psr.vulas.ConstructId toCoreType(com.sap.psr.vulas.shared.json.model.ConstructId _cid) { switch(_cid.getType()) { case METH: return JavaId.parseMethodQName(_cid.getQname()); case CONS: return JavaId.parseConstructorQName(_cid.getQname()); case PACK: return new JavaPackageId(_cid.getQname()); case INIT: return JavaId.parseClassInitQName(_cid.getQname()); case ENUM: return JavaId.parseEnumQName(_cid.getQname()); case CLAS: return JavaId.parseClassQName(_cid.getQname()); default: throw new IllegalArgumentException("Unknown type [" + _cid.getType() + "]"); } }