/** * Initialize from the given parsed bytecode. * For use by the owning project only. */ void read(BCClass orig) { try { ByteArrayInputStream in = new ByteArrayInputStream (orig.toByteArray()); read(in, orig.getClassLoader()); in.close(); } catch (IOException ioe) { throw new RuntimeException(ioe.toString()); } }
/** * Return the methods with the given name and parameter types, including * those of all superclasses, or an empty array if none. * Note that in bytecode, constructors are named <code><init></code> * and static initializers are named <code><clinit></code>. */ public BCMethod[] getMethods(String name, BCClass[] paramTypes) { if (paramTypes == null) return getMethods(name, (String[]) null); String[] paramNames = new String[paramTypes.length]; for (int i = 0; i < paramTypes.length; i++) paramNames[i] = paramTypes[i].getName(); return getMethods(name, paramNames); }
protected void declareClasses(BCClass bc) { super.declareClasses(bc); bc.declareInterface(DynamicPCData.class); bc.setSuperclass(AbstractPCData.class); }
/** * Add a field to this class. * * @return the added field */ public BCField declareField(String name, BCClass type) { String typeName = (type == null) ? null : type.getName(); return declareField(name, typeName); }
/** * Set the superclass of this class. */ public void setSuperclass(BCClass type) { if (type == null) setSuperclass((String) null); else setSuperclass(type.getName()); }
/** * Remove an interface declared by this class. * * @return true if the class had the interface, false otherwise */ public boolean removeDeclaredInterface(BCClass type) { if (type == null) return false; return removeDeclaredInterface(type.getName()); }
private void configureBCs() { if (!_bcsConfigured) { if (getRedefine()) { if (_managedType.getAttribute(REDEFINED_ATTRIBUTE) == null) _managedType.addAttribute(REDEFINED_ATTRIBUTE); else _isAlreadyRedefined = true; } if (getCreateSubclass()) { PCSubclassValidator val = new PCSubclassValidator( _meta, _managedType, _log, _fail); val.assertCanSubclass(); _pc = _managedType.getProject().loadClass( toPCSubclassName(_managedType.getType())); if (_pc.getSuperclassBC() != _managedType) { _pc.setSuperclass(_managedType); _pc.setAbstract(_managedType.isAbstract()); _pc.declareInterface(DynamicPersistenceCapable.class); } else { _isAlreadySubclassed = true; } } _bcsConfigured = true; } }
@SuppressWarnings("deprecation") public static void write(BCClass bc) throws IOException { if (bc.getMajorVersion() < Java7_MajorVersion) { bc.write(); } else { String name = bc.getName(); int dotIndex = name.lastIndexOf('.') + 1; name = name.substring(dotIndex); Class<?> type = bc.getType(); OutputStream out = new FileOutputStream( URLDecoder.decode(type.getResource(name + ".class").getFile())); try { writeJava7(bc, out); } finally { out.flush(); out.close(); } } }
/** * Write the class bytecode to the .class file in the proper directory of * the CLASSPATH. The file must exist already, so this method only works * on existing classes. */ public void write() throws IOException { String name = getName(); int dotIndex = name.lastIndexOf('.') + 1; name = name.substring(dotIndex); Class type = getType(); // attempt to get the class file for the class as a stream; // we need to use the url decoder in case the target directory // has spaces in it OutputStream out = new FileOutputStream(URLDecoder.decode (type.getResource(name + ".class").getFile())); try { write(out); } finally { out.close(); } }
/** * Generate a new class with the given name. If a non-null parent class * is given, it will be set as the superclass. */ public Class generateClass(String name, Class parent) { BCClass bc = _project.loadClass(name, null); if (parent != null) bc.setSuperclass(parent); bc.addDefaultConstructor(); try { return Class.forName(name, false, _loader); } catch (ClassNotFoundException cnfe) { throw new InternalException(cnfe.toString(), cnfe); } }
private static void write(BCClass bc, PCEnhancer enhancer, Map<Class, byte[]> map, Class cls, List subs, List ints) throws IOException { if (bc == enhancer.getManagedTypeBytecode()) { // if it was already defined, don't put it in the map, // but do set the metadata accordingly. if (enhancer.isAlreadyRedefined()) ints.add(bc.getType()); else if (JavaVersions.VERSION >= 5) map.put(bc.getType(), bc.toByteArray()); } else { if (!enhancer.isAlreadySubclassed()) { // this is the new subclass ClassLoader loader = GeneratedClasses.getMostDerivedLoader( cls, PersistenceCapable.class); subs.add(GeneratedClasses.loadBCClass(bc, loader)); } } }
public static void write(BCClass bc, File outFile) throws IOException { if (bc.getMajorVersion() < Java7_MajorVersion) { bc.write(outFile); } else { OutputStream out = new FileOutputStream(outFile); try { writeJava7(bc, out); } finally { out.flush(); out.close(); } } }
/** * Return source file information for the class. * Acts internally through the {@link Attributes} interface. * * @param add if true, a new source file attribute will be added * if not already present * @return the source file information, or null if none and the * <code>add</code> param is set to false */ public SourceFile getSourceFile(boolean add) { SourceFile source = (SourceFile) getAttribute(Constants.ATTR_SOURCE); if (!add || (source != null)) return source; return (SourceFile) addAttribute(Constants.ATTR_SOURCE); }
private static void writeJava7(BCClass bc, OutputStream out) throws IOException { byte[] java7Bytes = toJava7ByteArray(bc, bc.toByteArray()); out.write(java7Bytes); }
/** * Remove the annotation of the given type. * * @return true if an annotation was removed, false otherwise */ public boolean removeAnnotation(BCClass type) { return type != null && removeAnnotation(type.getName()); }
/** * Enhance constructor to initialize fields */ private void enhanceConstructor(BCClass bc) { BCMethod cons = bc.getDeclaredMethod("<init>", (String[]) null); Code code = cons.getCode(false); code.afterLast(); code.previous(); // private BitSet loaded = new BitSet(); BCField loaded = addBeanField(bc, "loaded", BitSet.class); loaded.setFinal(true); code.aload().setThis(); code.anew().setType(BitSet.class); code.dup(); code.constant().setValue(bc.getFields().length); code.invokespecial().setMethod(BitSet.class, "<init>", void.class, new Class[]{ int.class }); code.putfield().setField(loaded); code.calculateMaxStack(); code.calculateMaxLocals(); }
/** * Write the given class. */ private void record(BCClass bc) throws IOException { if (_writer != null) _writer.write(bc); else if (_dir == null) bc.write(); else { File dir = Files.getPackageFile(_dir, bc.getPackageName(), true); bc.write(new File(dir, bc.getClassName() + ".class")); } }
public BCField [] run() { return bcClass.getFields(fieldName); } };
/** * Return the methods with the given name and parameter types, including * those of all superclasses, or an empty array if none. * Note that in bytecode, constructors are named <code><init></code> * and static initializers are named <code><clinit></code>. */ public BCMethod[] getMethods(String name, Class[] paramTypes) { if (paramTypes == null) return getMethods(name, (String[]) null); String[] paramNames = new String[paramTypes.length]; for (int i = 0; i < paramTypes.length; i++) paramNames[i] = paramTypes[i].getName(); return getMethods(name, paramNames); }
/** * Load the given class name even if it does not exist. */ private static Class loadClass(Class context, String name, BCClassLoader bc) throws ClassNotFoundException { if (name.indexOf('.') == -1 && context.getName().indexOf('.') != -1) name = ClassUtil.getPackageName(context) + "." + name; // first try with regular class loader ClassLoader loader = AccessController.doPrivileged( J2DoPrivHelper.getClassLoaderAction(context)); if (loader == null) loader = AccessController.doPrivileged( J2DoPrivHelper.getContextClassLoaderAction()); try { return Class.forName(name, false, loader); } catch (Throwable t) { } // create class BCClass oid = bc.getProject().loadClass(name, null); oid.addDefaultConstructor(); return Class.forName(name, false, bc); }