/** * Calculates (with varying degree of error) the memory used by a specific kind of object. It is * best to call this method multiple times to get an accurate reading. * * @param <E> the type of object * @param factory the object factory * @return the amount of memory (in bytes) one instance of a class uses */ public static <E> long calculateMemoryUsage(ObjectFactory<E> factory) { @SuppressWarnings("unused") // creating the first unused object pulls the class definition into memory if it already hasn't // been. E handle = factory.createObject(); long mem0 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); long mem1 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); handle = null; // basically force garbage collection to run System.runFinalization(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); mem0 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); handle = factory.createObject(); System.runFinalization(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); mem1 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); return mem1 - mem0; }
/** Loads a single Object or converts the literal into an Object. */ public Object getObject(Value value) throws RepositoryException { assert value != null; if (value instanceof Literal) return of.createObject((Literal) value); Resource resource = (Resource) value; RDFObject cached = cached(resource); if (cached != null) return cached; return cache(of.createObject(resource, types.getTypes(resource))); }
/** * Explicitly adds the types to the entity. * * @return the entity with new composed types */ public Object addDesignations(Object entity, URI... types) throws RepositoryException { if (entity instanceof RDFObjectBehaviour) { RDFObjectBehaviour support = (RDFObjectBehaviour) entity; Object delegate = support.getBehaviourDelegate(); if (delegate != entity) { return addDesignations(delegate, types); } } assert types != null && types.length > 0; Resource resource = findResource(entity); Set<URI> list = new HashSet<URI>(4); getTypes(entity.getClass(), list); boolean autoCommit = isAutoCommit(); if (autoCommit) { setAutoCommit(false); } try { for (URI type : types) { this.types.addTypeStatement(resource, type); list.add(type); } if (autoCommit) { setAutoCommit(true); } } finally { if (autoCommit && !isAutoCommit()) { rollback(); setAutoCommit(true); } } return cache(of.createObject(resource, list)); }
/** * Construct an XMLGrammarParser with the specified symbol table * * @param symbolTable */ protected XMLGrammarParser(SymbolTable symbolTable) { super( (XMLParserConfiguration) ObjectFactory.createObject( "com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration", "com.sun.org.apache.xerces.internal.parsers.XIncludeAwareParserConfiguration")); fConfiguration.setProperty( Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY, symbolTable); }
/** * Prints the memory used by an object. * * @param <E> the type of class * @param factory the object factory */ public static <E> void showMemoryUsage(ObjectFactory<E> factory) { long mem = calculateMemoryUsage(factory); System.out.println( factory.getClass().getName() + " produced " + factory.createObject().getClass().getName() + " which took " + mem + " bytes"); }
@Override public void configure(Object o) throws Exception { Class<?> type = o.getClass(); Field[] fields = type.getDeclaredFields(); for (Field field : fields) { if (field.isAnnotationPresent(Inject.class)) { ObjectFactory factory = ObjectFactory.getInstance(); Object object = factory.createObject(field.getType()); field.setAccessible(true); field.set(o, object); } } }
static { try { String VERSION = "org.apache.xerces.impl.Version"; Object version = ObjectFactory.createObject(VERSION, VERSION); java.lang.reflect.Field field = version.getClass().getField("fVersion"); String versionStr = String.valueOf(field.get(version)); XERCES_2_0_0 = versionStr.equals("Xerces-J 2.0.0"); XERCES_2_0_1 = versionStr.equals("Xerces-J 2.0.1"); XML4J_4_0_x = versionStr.startsWith("XML4J 4.0."); } catch (Throwable e) { // ignore } } // <clinit>()
/** * Explicitly adds the concept to the entity. * * @return the entity with new composed concept */ public <T> T addDesignation(Object entity, Class<T> concept) throws RepositoryException { if (entity instanceof RDFObjectBehaviour) { RDFObjectBehaviour support = (RDFObjectBehaviour) entity; Object delegate = support.getBehaviourDelegate(); if (delegate != entity) { return addDesignation(delegate, concept); } } Resource resource = findResource(entity); Set<URI> types = new HashSet<URI>(4); getTypes(entity.getClass(), types); addConcept(resource, concept, types); RDFObject bean = of.createObject(resource, types); assert assertConceptRecorded(bean, concept); return (T) cache(bean); }
/** Imports the entity into the RDF store using the given handle. */ public void addObject(Resource resource, Object entity) throws RepositoryException { if (entity instanceof RDFObjectBehaviour) { RDFObjectBehaviour support = (RDFObjectBehaviour) entity; Object delegate = support.getBehaviourDelegate(); if (delegate != entity) { addObject(resource, delegate); return; } } synchronized (merged) { merged.add(resource); } boolean autoCommit = isAutoCommit(); if (autoCommit) { setAutoCommit(false); } try { Class<?> proxy = entity.getClass(); Set<URI> list = getTypes(proxy, new HashSet<URI>(4)); for (URI type : list) { types.addTypeStatement(resource, type); } Object result = of.createObject(resource, list); if (result instanceof Mergeable) { ((Mergeable) result).merge(entity); } if (autoCommit) { setAutoCommit(true); } cachedObjects.remove(resource); } finally { if (autoCommit && !isAutoCommit()) { rollback(); setAutoCommit(true); } } }
public static void main(String[] args) throws Exception { Person person = ObjectFactory.createObject(Person.class); }
/** Default constructor. */ public HTMLConfiguration() { // add components addComponent(fDocumentScanner); addComponent(fTagBalancer); addComponent(fNamespaceBinder); // // features // // recognized features String VALIDATION = "http://xml.org/sax/features/validation"; String[] recognizedFeatures = { AUGMENTATIONS, NAMESPACES, VALIDATION, REPORT_ERRORS, SIMPLE_ERROR_FORMAT, BALANCE_TAGS, }; addRecognizedFeatures(recognizedFeatures); setFeature(AUGMENTATIONS, false); setFeature(NAMESPACES, true); setFeature(VALIDATION, false); setFeature(REPORT_ERRORS, false); setFeature(SIMPLE_ERROR_FORMAT, false); setFeature(BALANCE_TAGS, true); // HACK: Xerces 2.0.0 if (XERCES_2_0_0) { // NOTE: These features should not be required but it causes a // problem if they're not there. This will be fixed in // subsequent releases of Xerces. recognizedFeatures = new String[] { "http://apache.org/xml/features/scanner/notify-builtin-refs", }; addRecognizedFeatures(recognizedFeatures); } // HACK: Xerces 2.0.1 if (XERCES_2_0_0 || XERCES_2_0_1 || XML4J_4_0_x) { // NOTE: These features should not be required but it causes a // problem if they're not there. This should be fixed in // subsequent releases of Xerces. recognizedFeatures = new String[] { "http://apache.org/xml/features/validation/schema/normalized-value", "http://apache.org/xml/features/scanner/notify-char-refs", }; addRecognizedFeatures(recognizedFeatures); } // // properties // // recognized properties String[] recognizedProperties = { NAMES_ELEMS, NAMES_ATTRS, FILTERS, ERROR_REPORTER, }; addRecognizedProperties(recognizedProperties); setProperty(NAMES_ELEMS, "upper"); setProperty(NAMES_ATTRS, "lower"); setProperty(ERROR_REPORTER, fErrorReporter); // HACK: Xerces 2.0.0 if (XERCES_2_0_0) { // NOTE: This is a hack to get around a problem in the Xerces 2.0.0 // AbstractSAXParser. If it uses a parser configuration that // does not have a SymbolTable, then it will remove *all* // attributes. This will be fixed in subsequent releases of // Xerces. String SYMBOL_TABLE = "http://apache.org/xml/properties/internal/symbol-table"; recognizedProperties = new String[] { SYMBOL_TABLE, }; addRecognizedProperties(recognizedProperties); Object symbolTable = ObjectFactory.createObject( "org.apache.xerces.util.SymbolTable", "org.apache.xerces.util.SymbolTable"); setProperty(SYMBOL_TABLE, symbolTable); } } // <init>()
public static void main(String[] args) throws Exception { MyService myService = ObjectFactory.createObject(MyService.class); System.out.println("myService = " + myService); }