use of org.drools.drl.ast.descr.ImportDescr in project drools by kiegroup.
the class TypeDeclarationUtils method lookupSimpleNameByImports.
public static String lookupSimpleNameByImports(String name, AbstractClassTypeDeclarationDescr typeDescr, PackageDescr packageDescr, ClassLoader loader) {
Class<?> typeClass = null;
if (isQualified(name)) {
typeClass = getClassForType(name, loader);
}
if (typeClass == null) {
for (ImportDescr id : packageDescr.getImports()) {
String imp = id.getTarget();
int separator = imp.lastIndexOf('.');
String tail = imp.substring(separator + 1);
if (tail.equals(name)) {
typeClass = getClassForType(imp, loader);
if (typeClass != null) {
return typeClass.getCanonicalName();
} else {
return imp;
}
} else if (tail.equals("*")) {
typeClass = getClassForType(imp.substring(0, imp.length() - 1) + name, loader);
if (typeClass != null) {
String resolvedNamespace = imp.substring(0, separator);
if (resolvedNamespace.equals(typeDescr.getNamespace())) {
// the class was found in the declared namespace, so stop here
break;
// here, the class was found in a different namespace. It means that the class was declared
// with no namespace and the initial guess was wrong, or that there is an ambiguity.
// So, we need to check that the resolved class is compatible with the declaration.
} else if (name.equals(typeDescr.getType().getName()) && !isCompatible(typeClass, typeDescr)) {
typeClass = null;
} else {
break;
}
}
}
}
}
return typeClass != null ? typeClass.getName() : name;
}
use of org.drools.drl.ast.descr.ImportDescr in project drools by kiegroup.
the class JavaDialect method postCompileAddFunction.
public void postCompileAddFunction(FunctionDescr functionDescr, TypeResolver typeResolver) {
final String functionClassName = this.pkg.getName() + "." + StringUtils.ucFirst(functionDescr.getName());
ImportDescr importDescr = new ImportDescr(functionClassName + "." + functionDescr.getName());
importDescr.setResource(functionDescr.getResource());
importDescr.setNamespace(functionDescr.getNamespace());
this.packageRegistry.addStaticImport(importDescr);
}
use of org.drools.drl.ast.descr.ImportDescr in project drools by kiegroup.
the class DialectUtil method findClassByName.
public static Class<?> findClassByName(RuleBuildContext context, String className) {
if (className == null) {
return null;
}
String namespace = context.getRuleDescr().getNamespace();
DroolsAssemblerContext packageBuilder = context.getKnowledgeBuilder();
Class<?> clazz = null;
try {
clazz = Class.forName(className.indexOf('.') < 0 ? namespace + "." + className : className, false, packageBuilder.getRootClassLoader());
} catch (ClassNotFoundException e) {
}
if (clazz != null) {
return clazz;
}
Set<String> imports = new HashSet<>();
List<PackageDescr> pkgDescrs = packageBuilder.getPackageDescrs(namespace);
if (pkgDescrs == null) {
return null;
}
for (PackageDescr pkgDescr : pkgDescrs) {
for (ImportDescr importDescr : pkgDescr.getImports()) {
imports.add(importDescr.getTarget());
}
}
return findClass(className, imports, packageBuilder.getRootClassLoader());
}
use of org.drools.drl.ast.descr.ImportDescr in project drools by kiegroup.
the class RuleParserTest method testCompilationUnit.
@Test
public void testCompilationUnit() throws Exception {
final String source = "package foo; import com.foo.Bar; import com.foo.Baz;";
PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
assertEquals("foo", pkg.getName());
assertEquals(2, pkg.getImports().size());
ImportDescr impdescr = pkg.getImports().get(0);
assertEquals("com.foo.Bar", impdescr.getTarget());
assertEquals(source.indexOf("import " + impdescr.getTarget()), impdescr.getStartCharacter());
assertEquals(source.indexOf("import " + impdescr.getTarget()) + ("import " + impdescr.getTarget()).length(), impdescr.getEndCharacter());
impdescr = pkg.getImports().get(1);
assertEquals("com.foo.Baz", impdescr.getTarget());
assertEquals(source.indexOf("import " + impdescr.getTarget()), impdescr.getStartCharacter());
assertEquals(source.indexOf("import " + impdescr.getTarget()) + ("import " + impdescr.getTarget()).length(), impdescr.getEndCharacter());
}
use of org.drools.drl.ast.descr.ImportDescr in project drools by kiegroup.
the class CompositePackageDescr method internalAdd.
private void internalAdd(Resource resource, PackageDescr packageDescr) {
for (ImportDescr descr : packageDescr.getImports()) {
addImport(descr);
descr.setResource(resource);
}
for (FunctionImportDescr descr : packageDescr.getFunctionImports()) {
addFunctionImport(descr);
descr.setResource(resource);
}
for (AccumulateImportDescr descr : packageDescr.getAccumulateImports()) {
addAccumulateImport(descr);
descr.setResource(resource);
}
for (AttributeDescr descr : packageDescr.getAttributes()) {
addAttribute(descr);
descr.setResource(resource);
}
for (GlobalDescr descr : packageDescr.getGlobals()) {
addGlobal(descr);
descr.setResource(resource);
}
for (FunctionDescr descr : packageDescr.getFunctions()) {
addFunction(descr);
descr.setResource(resource);
}
for (RuleDescr descr : packageDescr.getRules()) {
addRule(descr);
descr.setResource(resource);
}
// Avoid adding the same type declaration twice, see
// TypeDeclarationTest.testDuplicatedTypeDeclarationInDifferentResources
// IncrementalCompilationTest.testIncrementalCompilationWithAmbiguousRedeclares
// RHDM-1738
Set<TypeDeclarationDescr> typeDeclarationDescrs = new HashSet<>(getTypeDeclarations());
for (TypeDeclarationDescr descr : packageDescr.getTypeDeclarations()) {
if (!typeDeclarationDescrs.contains(descr)) {
addTypeDeclaration(descr);
descr.setResource(resource);
}
}
for (EnumDeclarationDescr enumDescr : packageDescr.getEnumDeclarations()) {
addEnumDeclaration(enumDescr);
enumDescr.setResource(resource);
}
for (EntryPointDeclarationDescr descr : packageDescr.getEntryPointDeclarations()) {
addEntryPointDeclaration(descr);
descr.setResource(resource);
}
for (WindowDeclarationDescr descr : packageDescr.getWindowDeclarations()) {
addWindowDeclaration(descr);
descr.setResource(resource);
}
packageDescr.getPreferredPkgUUID().ifPresent(pkgUUID -> {
if (getPreferredPkgUUID().isPresent() && !pkgUUID.equals(getPreferredPkgUUID().get())) {
throw new RuntimeException(String.format("Trying to overwrite preferredPkgUUID %s with a different value %s", getPreferredPkgUUID().get(), pkgUUID));
}
setPreferredPkgUUID(pkgUUID);
});
}
Aggregations