private URL computeResourceURL(Method method) throws NotFoundException, URISyntaxException { List<ResourceFileEntry> filesSimpleNames = new ArrayList<ResourceFileEntry>(); boolean computeExtensions = false; CtMethod m = ctClass.getMethod(method.getName(), getDescriptor(method)); MethodInfo minfo = m.getMethodInfo2(); AnnotationsAttribute attr = (AnnotationsAttribute) minfo.getAttribute(AnnotationsAttribute.invisibleTag); if (attr != null) { Annotation an = attr.getAnnotation(Source.class.getName()); if (an != null) { MemberValue[] mvArray = ((ArrayMemberValue) an.getMemberValue("value")).getValue(); if (mvArray != null) { for (MemberValue mv : mvArray) { StringMemberValue smv = (StringMemberValue) mv; filesSimpleNames.add(new ResourceFileEntry(smv.getValue(), m)); } } } } if (filesSimpleNames.isEmpty()) { // no @Source annotation detected filesSimpleNames.add(new ResourceFileEntry(method.getName(), m)); computeExtensions = true; } List<URL> existingFiles = new ArrayList<URL>(); for (ResourceFileEntry resourceEntry : filesSimpleNames) { String resourceName = resourceEntry.resourceName; CtClass declaringClass = resourceEntry.resourceMethod.getDeclaringClass(); String baseDir = declaringClass.getPackageName().replaceAll("\\.", "/") + "/"; String fileName = (resourceName.startsWith(baseDir)) ? resourceName : baseDir + resourceName; if (computeExtensions) { String[] extensions = getResourceDefaultExtensions(method); for (String extension : extensions) { String possibleFile = fileName + extension; URL url = GwtPatcher.class.getClassLoader().getResource(possibleFile); if (url != null) { existingFiles.add(url); } } } else { URL url = GwtPatcher.class.getClassLoader().getResource(fileName); if (url != null) { existingFiles.add(url); } } } if (existingFiles.isEmpty()) { throw new RuntimeException( "No resource file found for method " + ctClass.getSimpleName() + "." + method.getName() + "()"); } else if (existingFiles.size() > 1) { throw new RuntimeException( "Too many resource files found for method " + ctClass.getSimpleName() + "." + method.getName() + "()"); } return existingFiles.get(0); }
/** * Given a bean-style interface, generate an instance of the interface by implementing getters and * setters for each property. It will also add implementations to support the {@link * SupportsRdfId} interface and generate simple, default equals, toString and hashCode methods. * * <p>If there are other non-bean style (getter and/or setter's for properties) methods on the * interface, this will likely fail to generate the instance. * * @param theInterface the interface to build an instance of * @param <T> the type of the interface * @return New dynamically generated bytecode of a class that implements the given interface. * @throws Exception if there is an error while generating the bytecode of the new class. */ public static synchronized <T> Class<T> generateInstanceClass(Class<T> theInterface) throws Exception { processedMethods.clear(); // TODO: can we use some sort of template language for this? ClassPool aPool = ClassPool.getDefault(); aPool.appendClassPath(new LoaderClassPath(theInterface.getClassLoader())); CtClass aInterface = aPool.get(theInterface.getName()); CtClass aSupportsRdfIdInterface = aPool.get(SupportsRdfId.class.getName()); CtClass aEmpireGeneratedInterface = aPool.get(EmpireGenerated.class.getName()); if (!Arrays.asList(aInterface.getInterfaces()).contains(aSupportsRdfIdInterface) && !SupportsRdfId.class.isAssignableFrom(theInterface)) { throw new IllegalArgumentException( "Class '" + theInterface.getName() + "' does not implement SupportsRdfId, cannot generate Empire suitable implementation."); } String aName = aInterface.getPackageName() + ".impl." + aInterface.getSimpleName() + "Impl"; CtClass aClass = null; try { // i had a good reason for doing this, but i dont remember what it is. when i do, i'll // explain it here =) aClass = aPool.get(aName); return (Class<T>) BeanReflectUtil.loadClass(aName); } catch (NotFoundException e) { aClass = aPool.makeClass( aInterface.getPackageName() + ".impl." + aInterface.getSimpleName() + "Impl"); } catch (ClassNotFoundException e) { throw new Exception("Previously created class cannot be loaded.", e); } if (aClass.isFrozen()) { aClass.defrost(); } if (aInterface.isInterface()) { aClass.addInterface(aInterface); } else { aClass.setSuperclass(aInterface); } aClass.addInterface(aSupportsRdfIdInterface); aClass.addInterface(aEmpireGeneratedInterface); CtField aInterfaceField = new CtField(aPool.get(Class.class.getName()), "mInterfaceClass", aClass); aClass.addField( aInterfaceField, CtField.Initializer.byExpr(theInterface.getName() + ".class;")); CtField aAllTriplesField = new CtField(aPool.get(Graph.class.getName()), "mAllTriples", aClass); aClass.addField( aAllTriplesField, CtField.Initializer.byExpr("new com.clarkparsia.openrdf.ExtGraph();")); CtField aInstanceTriplesField = new CtField(aPool.get(Graph.class.getName()), "mInstanceTriples", aClass); aClass.addField( aInstanceTriplesField, CtField.Initializer.byExpr("new com.clarkparsia.openrdf.ExtGraph();")); aClass.addConstructor(CtNewConstructor.defaultConstructor(aClass)); generateMethods(theInterface, aPool, aClass); generateMethodsForSuperInterfaces(theInterface, aPool, aClass); CtField aIdField = new CtField(aPool.get(SupportsRdfId.class.getName()), "supportsId", aClass); aClass.addField( aIdField, CtField.Initializer.byExpr("new com.clarkparsia.empire.annotation.SupportsRdfIdImpl();")); if (!hasMethod(aClass, "getRdfId")) { aClass.addMethod( CtNewMethod.make( "public com.clarkparsia.empire.SupportsRdfId.RdfKey getRdfId() { return supportsId.getRdfId(); } ", aClass)); } if (!hasMethod(aClass, "setRdfId")) { aClass.addMethod( CtNewMethod.make( "public void setRdfId(com.clarkparsia.empire.SupportsRdfId.RdfKey theURI) { supportsId.setRdfId(theURI); } ", aClass)); } if (!hasMethod(aClass, "getAllTriples")) { aClass.addMethod( CtNewMethod.make( "public org.openrdf.model.Graph getAllTriples() { return mAllTriples; } ", aClass)); } if (!hasMethod(aClass, "setAllTriples")) { aClass.addMethod( CtNewMethod.make( "public void setAllTriples(org.openrdf.model.Graph theGraph) { mAllTriples = theGraph; } ", aClass)); } if (!hasMethod(aClass, "getInstanceTriples")) { aClass.addMethod( CtNewMethod.make( "public org.openrdf.model.Graph getInstanceTriples() { return mInstanceTriples; } ", aClass)); } if (!hasMethod(aClass, "setInstanceTriples")) { aClass.addMethod( CtNewMethod.make( "public void setInstanceTriples(org.openrdf.model.Graph theGraph) { mInstanceTriples = theGraph; } ", aClass)); } String equalsMethodBody = "public boolean equals(Object theObj) {\n" + " if (theObj == this) return true;\n" + " if (!(theObj instanceof com.clarkparsia.empire.SupportsRdfId)) return false;\n" + " if (!(mInterfaceClass.isAssignableFrom(theObj.getClass()))) return false;\n" + " return getRdfId().equals( ((com.clarkparsia.empire.SupportsRdfId) theObj).getRdfId()) && super.equals(theObj);\n" + "}\n"; aClass.addMethod(CtNewMethod.make(equalsMethodBody, aClass)); if (theInterface.isInterface()) { aClass.addMethod( CtNewMethod.make( "public String toString() { return getRdfId() != null ? getRdfId().toString() : super.toString(); } ", aClass)); aClass.addMethod( CtNewMethod.make( "public int hashCode() { return getRdfId() != null ? getRdfId().hashCode() : 0; } ", aClass)); } aClass.freeze(); Class<T> aResult = (Class<T>) aClass.toClass(); try { // make sure this is a valid class, that is, we can create instances of it! aResult.newInstance(); } catch (Exception ex) { // TODO: log this? throw ex; } return aResult; }