/** * Move code-related attributes from one method to another. */ private static void transferCodeAttributes(BCMethod from, BCMethod to) { Code code = from.getCode(false); if (code != null) { to.addAttribute(code); from.removeCode(); } Exceptions exceps = from.getExceptions(false); if (exceps != null) to.addAttribute(exceps); }
private boolean setVisibilityToSuperMethod(BCMethod method) { BCMethod[] methods = _managedType.getMethods(method.getName(), method.getParamTypes()); if (methods.length == 0) throw new UserException(_loc.get("no-accessor", _managedType.getName(), method.getName())); BCMethod superMeth = methods[0]; if (superMeth.isPrivate()) { method.makePrivate(); return true; } else if (superMeth.isPackage()) { method.makePackage(); return true; } else if (superMeth.isProtected()) { method.makeProtected(); return true; } else if (superMeth.isPublic()) { method.makePublic(); return true; } return false; }
/** * Import the information in the given method as a new method of this class. * * @return the added method */ public BCMethod declareMethod(BCMethod method) { BCMethod newMethod = declareMethod(method.getName(), method.getReturnName(), method.getParamNames()); newMethod.setAccessFlags(method.getAccessFlags()); newMethod.setAttributes(method.getAttributes()); return newMethod; }
/** * Manipulate the method access flags. */ public void setSynchronized(boolean on) { if (on) setAccessFlags(getAccessFlags() | Constants.ACCESS_SYNCHRONIZED); else setAccessFlags(getAccessFlags() & ~Constants.ACCESS_SYNCHRONIZED); }
/** * Add a parameter type to this method. */ public void addParam(String type) { String[] origParams = getParamNames(); String[] params = new String[origParams.length + 1]; for (int i = 0; i < origParams.length; i++) params[i] = origParams[i]; params[origParams.length] = type; setParams(params); }
Code code; if (clinit != null) { code = clinit.getCode(true); if (replaceLast) { Code template = AccessController.doPrivileged( clinit.makePackage(); clinit.setStatic(true); clinit.setFinal(true); code = clinit.getCode(true); if (!replaceLast) { code.vreturn();
private void addGetEnhancementContractVersionMethod() { // public int getEnhancementContractVersion() BCMethod method = _pc.declareMethod(PRE + "GetEnhancementContractVersion", int.class, null); method.makePublic(); Code code = method.getCode(true); code.constant().setValue(ENHANCER_VERSION); code.ireturn(); code.calculateMaxStack(); code.calculateMaxLocals(); }
public void enterBCMethod(BCMethod obj) { openBlock("Method"); println("access=" + obj.getAccessFlags()); println("name=" + obj.getNameIndex() + " <" + obj.getName() + ">"); println("descriptor=" + obj.getDescriptorIndex()); println("return=" + obj.getReturnName()); String[] params = obj.getParamNames(); for (int i = 0; i < params.length; i++) println("param=" + params[i]); }
/** * Set the method this instruction operates on. * * @return this instruction, for method chaining */ public MethodInstruction setMethod(BCMethod method) { if (method == null) return setMethodIndex(0); return setMethod(method.getDeclarer().getName(), method.getName(), method.getReturnName(), method.getParamNames(), false); }
method.setStatic(true); method.makePackage(); method.setSynthetic(true); Code code = method.getCode(true); code.setMaxStack(3); code.setMaxLocals(2);
/** * Return the parameter index for the given local index, or -1 if * the given local does not reference a param. * * @see #getLocalsIndex */ public int getParamsIndex(int localIndex) { int pos = 0; if (!getMethod().isStatic()) pos = 1; String[] params = getMethod().getParamNames(); for (int i = 0; i < params.length; i++, pos++) { if (localIndex == pos) return i; if (params[i].equals(long.class.getName()) || params[i].equals(double.class.getName())) pos++; } return -1; }
/** * Set the field this instruction operates on, for fields that are * declared by the current class. * * @param name the field name * @param type the class of the field type * @return this instruction, for method chaining */ public FieldInstruction setField(String name, Class type) { BCClass owner = getCode().getMethod().getDeclarer(); String typeName = (type == null) ? null : type.getName(); return setField(owner.getName(), name, typeName); }
/** * Return true if the given instruction accesses a field that is a backing * field of another property in this property-access class. */ private boolean isBackingFieldOfAnotherProperty(String name, Code code) { String methName = code.getMethod().getName(); return !"<init>".equals(methName) && _backingFields != null && !name.equals(_backingFields.get(methName)) && _backingFields.containsValue(name); }
/** * Removes a method from this class. After this method, the removed method * will be invalid, and the result of any operations on it is undefined. * * @return true if this class contained the method, false otherwise */ public boolean removeDeclaredMethod(BCMethod method) { if (method == null) return false; return removeDeclaredMethod(method.getName(), method.getParamNames()); }
/** * Return the declared method with the given name and signature, * or null if none. * Note that in bytecode, constructors are named <code><init></code> * and static initializers are named <code><clinit></code>. */ public BCMethod getDeclaredMethod(String name, String returnType, String[] paramTypes) { if (paramTypes == null) paramTypes = new String[0]; BCMethod[] methods = getDeclaredMethods(); for (int i = 0; i < methods.length; i++) { if (methods[i].getName().equals(name) && methods[i].getReturnName().equals(_project.getNameCache(). getExternalForm(returnType, false)) && paramsMatch(methods[i], paramTypes)) return methods[i]; } return null; }
private void addGetIDOwningClass() throws NoSuchMethodException { BCMethod method = _pc.declareMethod(PRE + "GetIDOwningClass", Class.class, null); Code code = method.getCode(true); code.classconstant().setClass(getType(_meta)); code.areturn(); code.calculateMaxStack(); code.calculateMaxLocals(); }
/** * Return the checked exceptions information for the method. * Acts internally through the {@link Attributes} interface. * * @param add if true, a new exceptions attribute will be added * if not already present * @return the exceptions information, or null if none and the * <code>add</code> param is set to false */ public Exceptions getExceptions(boolean add) { Exceptions exceptions = (Exceptions) getAttribute (Constants.ATTR_EXCEPTIONS); if (!add || (exceptions != null)) return exceptions; if (exceptions == null) exceptions = (Exceptions) addAttribute(Constants.ATTR_EXCEPTIONS); return exceptions; }
/** * Manipulate the method access flags. */ public boolean isStrict() { return (getAccessFlags() & Constants.ACCESS_STRICT) > 0; }
/** * Return the bytecode for all the parameter types for this method. * * @see BCMember#getDescriptor */ public BCClass[] getParamBCs() { String[] paramNames = getParamNames(); BCClass[] params = new BCClass[paramNames.length]; for (int i = 0; i < paramNames.length; i++) params[i] = getProject().loadClass(paramNames[i], getClassLoader()); return params; }
/** * Set the return type of this method. */ public void setReturn(String name) { setDescriptor(getProject().getNameCache().getDescriptor(name, getParamNames())); }